Advanced OOP Concepts
‘this’ Pointer
A keyword that refers to the current instance of the class.
- Used to pass current object as parameter to another method.
- Used to refer to current class instance variable.
- Used to declare indexers.
Friend Function
A non-member function that acts as a friend of the class and can access its private and protected members.
- Must be listed in class definition.
- Cannot access private members directly; must use object name and dot operator.
- Uses objects as arguments.
Aggregation
A process in which one class defines another class as an entity reference. It represents a HAS-A relationship and is another way to reuse classes.
Virtual Functions
Used to replace implementation provided by base class. Redefined by derived class and declared with ‘virtual’ keyword in base class.
- Determined at run-time based on type of object pointed by base class pointer.
- Cannot be static.
- A class can have virtual destructor but not virtual constructor.
- Pure Virtual Function: Has no definition in base class, serves as placeholder. Makes the class an Abstract Base Class.
Abstract Classes
A class made abstract by declaring at least one function as a pure virtual function (= 0). It cannot be used to declare objects of its own; implementation must be provided by derived classes.
Interface
An interface is a blueprint of a class. It has static constants and abstract methods. It is used to achieve fully abstraction and multiple inheritance in Java.
- Abstract Class vs Interface: Abstract class can have abstract and non-abstract methods, whereas interface has only abstract methods (until Java 8). Abstract class doesn’t support multiple inheritance, but interface does. Abstract class can have final, non-final, static and non-static variables, whereas interface has only static and final variables.
Inline Function
An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.
Important Keywords
Keywords used to define specific behaviors in OOP:
- Static Keyword: Used for memory management. It can be applied to variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.
- Final Keyword: Used to restrict the user. It can be used in many contexts: final variable (cannot change value), final method (cannot be overridden), final class (cannot be inherited).
- Super Keyword: A reference variable which is used to refer immediate parent class object.
Exception Handling
The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
- try: The “try” keyword is used to specify a block where we should place exception code.
- catch: The “catch” block is used to handle the exception.
- finally: The “finally” block is used to execute the important code of the program. It is executed whether an exception is handled or not.
- throw: The “throw” keyword is used to throw an exception.
- throws: The “throws” keyword is used to declare exceptions.
Memory Management
Concepts related to memory in OOP:
- Garbage Collection: The process of reclaiming the runtime unused memory automatically. In C/C++, programmer is responsible for both creation and destruction of objects. In Java, it is performed automatically.
- Memory Leak: A memory leak occurs when programmers create a memory in heap and forget to delete it.
Shallow Copy vs Deep Copy
Different ways of copying objects:
- Shallow Copy: An object is created by simply copying the data of all variables of the original object. If some variables are dynamically allocated memory, then the copied object variable will also reference the same memory location.
- Deep Copy: An object is created by copying data of all variables, and it also allocates similar memory resources with the same value to the object.
Early Binding vs Late Binding
Binding refers to the linking of a procedure call to the code to be executed in response to the call.
- Early Binding (Static Binding): The binding which can be resolved at compile time by compiler. E.g., method overloading.
- Late Binding (Dynamic Binding): The binding which can be resolved at run time. E.g., method overriding.
Method Hiding & Constructor Overloading
Additional OOP concepts:
- Method Hiding: If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
- Constructor Overloading: A technique of having more than one constructor with different parameter lists.
Common OOP Questions
Frequently asked questions regarding overriding and overloading:
- Can we overload main method? Yes, we can overload the main method.
- Can we override static method? No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time.
- Can we override private method? No, we cannot override private methods because they are not accessible outside the class.
- Can we override final method? No, we cannot override final methods.
- Can we override constructor? No, we cannot override constructors.
- Can we inherit constructor? No, constructors are not inherited.
- Can we inherit private members? No, private members are not inherited.
- Can we inherit final class? No, final classes cannot be inherited.
Item 27 of 28 · updated 2026-10-12