0% found this document useful (0 votes)
153 views

OOPS Cheat Sheet

The document is a cheat sheet on Object-Oriented Programming (OOP), covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains the differences between various programming constructs like classes and interfaces, method overloading and overriding, and discusses design patterns and the advantages and disadvantages of OOP. Additionally, it addresses concepts like dependency injection, encapsulation implementation, and exception handling in OOP languages.

Uploaded by

ansumaanpanda9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views

OOPS Cheat Sheet

The document is a cheat sheet on Object-Oriented Programming (OOP), covering key concepts such as classes, objects, encapsulation, inheritance, polymorphism, and abstraction. It explains the differences between various programming constructs like classes and interfaces, method overloading and overriding, and discusses design patterns and the advantages and disadvantages of OOP. Additionally, it addresses concepts like dependency injection, encapsulation implementation, and exception handling in OOP languages.

Uploaded by

ansumaanpanda9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OOPS Cheat Sheet

 What is Object-Oriented Programming? Explain its main principles.


Ans : Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
objects, which can contain data (attributes) and code (methods). The four main principles of OOP are
encapsulation, inheritance, polymorphism, and abstraction.

 What is a class and an object? How do they differ?


Ans : A class is a blueprint or template for creating objects. An object is an instance of a class,
representing a specific implementation of that class's attributes and methods.
 What is encapsulation? Provide an example.
Ans : Encapsulation is the bundling of data (attributes) and methods that operate on the data into a
single unit or class. It also involves restricting access to some components using access specifiers like
private, protected, and public. Example: A Car class with private attributes like speed and public
methods like accelerate().
 Explain inheritance and how it is used in OOP.
Ans : Inheritance allows a class (child or subclass) to inherit properties and behavior (attributes and
methods) from another class (parent or superclass). This promotes code reuse. For example, a Dog
class can inherit from an Animal class.
 What is polymorphism? Describe its types.
Ans : Polymorphism is the ability of different objects to respond in a unique way to the same message
(method call). The two types are:

1. Compile-time polymorphism (method overloading): Methods have the same name but different
parameters.
2. Runtime polymorphism (method overriding): A subclass provides a specific implementation of a
method that is already defined in its superclass.

 What is abstraction? How does it differ from encapsulation?


Ans : Abstraction is the concept of hiding complex implementation details and showing only the
necessary features of an object. Encapsulation is about bundling the data and methods that
manipulate the data, while abstraction is about hiding the complexity.

 What are access specifiers? Explain the difference between private, protected, and public.
Ans : Access specifiers define the visibility and accessibility of classes, methods, and attributes:

Private: Accessible only within the class.

Protected: Accessible within the class and by derived classes.

Public: Accessible from any other class.

 What is a constructor? How is it different from a method?


Ans : A constructor is a special method used to initialize objects when they are created. It has the
same name as the class and no return type. Unlike regular methods, constructors are automatically
called when an object is instantiated.

 What is method overloading and method overriding? How are they different?
Ans : Method overloading is defining multiple methods with the same name but different parameters
in the same class. Method overriding is when a subclass provides a specific implementation of a
method that is already defined in its superclass.

 What is the difference between a class and an interface?


Ans : A class can have both concrete (implemented) methods and abstract methods, while an
interface can only have abstract methods (in some languages, it can have default methods). A class
can inherit from only one superclass but can implement multiple interfaces.

 What is multiple inheritance? Does your programming language support it?


Ans : Multiple inheritance is when a class can inherit from more than one class. Not all languages
support it directly (e.g., Java uses interfaces to achieve this). C++ supports multiple inheritance.

 What is the difference between a shallow copy and a deep copy?


Ans : A shallow copy creates a new object but does not create copies of nested objects, whereas a
deep copy creates a new object and recursively copies all objects it references.

 Explain the concept of dynamic binding.


Ans : Dynamic binding, also known as late binding, occurs when the method to be invoked is
determined at runtime rather than at compile-time. It is used in polymorphism.

 What are abstract classes? How are they different from interfaces?\
Ans : An abstract class can have both abstract methods (without implementation) and concrete
methods. An interface only has abstract methods (with some exceptions like default methods in Java).
A class can inherit only one abstract class but can implement multiple interfaces.

 What is a static method and how does it differ from an instance method?
Ans : A static method belongs to the class rather than any object of the class and can be called
without creating an instance of the class. An instance method requires an object of the class to be
invoked and can access instance variables.

 What are design patterns? Name a few commonly used ones.


Ans : Design patterns are typical solutions to common problems in software design. Some commonly
used patterns include:

 Singleton: Ensures a class has only one instance.


 Factory: Creates objects without specifying the exact class.
 Observer: Allows objects to be notified of state changes in other objects.

 How does OOP enhance code reusability? Provide examples.


Ans : OOP enhances code reusability through inheritance, where classes can inherit features from
other classes, and through polymorphism, where a single interface can be used for different
underlying forms (e.g., different subclasses implementing the same method).

 Explain the concept of dependency injection.


Ans : Dependency injection is a design pattern where an object receives other objects it depends on,
rather than creating them internally. This helps in decoupling the components and makes the system
easier to test.
 What are the advantages and disadvantages of using OOP?

Ans :  Advantages: Encapsulation, code reusability, scalability, and ease of maintenance.


 Disadvantages: Can lead to complex hierarchies, increased memory usage, and a
steeper learning curve.

 What is the difference between composition and inheritance? When would you use one over the
other?
Ans : Inheritance is a "is-a" relationship (e.g., a Dog is an Animal), while composition is a "has-a"
relationship (e.g., a Car has an Engine). Composition is often preferred when you want to use
functionalities from different classes without the tight coupling of inheritance.

 How do you implement encapsulation in [specific language]?


Ans : In languages like Java or C++, encapsulation is implemented by using access specifiers like
private, protected, and public to restrict access to class members. You can also use getter
and setter methods to access and modify private variables.

 What are the differences between abstract classes and interfaces in [specific language]?
Ans : In Java, an abstract class can have a mixture of abstract and concrete methods, whereas an
interface (prior to Java 8) only had abstract methods. Interfaces are used to implement multiple
inheritance, whereas a class can extend only one abstract class.

 Can you provide an example of polymorphism in [specific language]?


Ans : In Java, polymorphism can be seen with method overriding. For example :

class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
Animal myDog = new Dog();
myDog.sound(); // Outputs: Bark

 How is exception handling managed in an OOP language like [specific language]?


Ans : In Java, exception handling is managed using try, catch, and finally blocks. You can throw
exceptions using the throw keyword and define custom exceptions by extending the Exception
class.

 What is the role of a destructor in OOP, and how is it implemented in [specific language]?
Ans : In C++, a destructor is a special method that is automatically called when an object goes out of
scope or is deleted. It is used to release resources like memory or file handles. In Java, destructors are
not used; instead, the garbage collector handles memory management.

You might also like