OOPs FAQ's

OOPs stands for Object Oriented Programming Structure. It is a method of implementation in which programs are organized as a collection of objects, classes, and methods.

OOPs principles are:

  • Class
  • Method
  • Object
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Class – A collection of methods or objects.
  • Method – A set of actions to be performed.
  • Object – An instance of a class. It creates memory for the class at runtime and is used to call methods of the class.
 ClassName refName = new ClassName(); 

Here:

  • ClassName → Name of the class whose object is being created.
  • refName → Reference variable (used to access the object).
  • new → Keyword that allocates memory for the object.
  • ClassName() → Constructor call (can be default or parameterized).

Notes:

  • new keyword always allocates memory at runtime on the heap.
  • Every object creation must call a constructor.
  • If no constructor is defined, Java provides a default constructor automatically.

Encapsulation is the process of binding data and code together into a single entity.

Inheritance is the process of accessing one class's properties and methods in another class using the extends keyword. The main purpose of using inheritance is code reusability, and it also helps in reducing memory usage by avoiding the creation of duplicate objects or code.

  • Single Inheritance – One parent class is directly extended by one child class using the extends keyword.
  • Multilevel Inheritance – One child class and more than one parent class, forming a hierarchy like Grandparent → Parent → Child.
  • Multiple Inheritance – A child class inherits more than one parent class in parallel. Java does not support multiple inheritance with classes due to priority issues and compilation errors. To overcome this, interfaces are used.
  • Hybrid Inheritance – A combination of single and multiple inheritance types.
  • Hierarchical Inheritance – One parent class is inherited by multiple child classes.
  • Sub Class (Child class or Derived class) – A class that extends another class.
  • Super Class (Parent class or Base class) – A class that is inherited by another class.

Poly means many, Morphism means forms. Taking more than one form is called polymorphism, or completing one task in many ways. It has 2 types:

  • Method Overloading – Also called static binding or compile-time polymorphism.
  • Method Overriding – Also called dynamic binding or run-time polymorphism.

When the method name is the same within the same class but the arguments are different, it is called method overloading. Arguments can differ by data type, number of parameters, or the order of parameters.

If we are not satisfied with the parent class logic, we can extend the parent class into a child class and override the same method with the same method name and same arguments — this is called method overriding.

Hiding the implementation part is called abstraction. It has 2 types:

  • Abstract Class – Partially abstraction.
  • Interface – Fully abstraction in Java.
  • An abstract class can support both abstract and non-abstract methods (Partially Abstraction).
  • An abstract method contains only the method name or signature and does not have any business logic.
  • We cannot create an object of an abstract class.
  • Using the public abstract keyword is mandatory to define an abstract method.
  • We use the extends keyword to access the methods of an abstract class in a subclass.
  • An interface supports only abstract methods, allowing us to achieve 100% abstraction in Java.
  • In an interface, methods are abstract by default and do not contain any business logic.
  • We cannot create an object of an interface.
  • The keywords public and abstract are optional.
  • We use the implements keyword to implement the interface methods in a class.
  • Interfaces help to overcome the limitation of multiple inheritance in Java by allowing a class to implement multiple interfaces.

No. An abstract method needs to be overridden in a subclass, but a static method cannot be overridden. These two concepts conflict, so Java doesn’t allow abstract static methods.

Yes, we can declare static variables and methods in an abstract class.

Yes, we can overload the methods by just applying the static keyword to them.

Yes, we can declare the main method as: public static final void main(String[] args) {}

No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition. If you try to do so, the compiler will show an error.

No, because we need to override the abstract method to provide its implementation, whereas we cannot override a final method.

Yes, overloaded methods can also be overridden, but overloading and overriding are separate concepts:

  • Overloading: Same method name, different parameters, same class.
  • Overriding: Same method name and parameters, different classes (with inheritance).

So, you can override a method that is also overloaded.

No, Java is not fully object-oriented because it uses primitive data types like int, char, etc., along with static methods and operators. It supports OOP features but also mixes in procedural concepts.

No, static methods cannot be overridden in Java.