OOPS Concept






  • What is Inheritance?
Inheritance is a way by which child class can obtain the properties of parent class. The main advantage of inheritance is that we can reuse the features.

  • How is Inheritance achieved?
Child class uses the “extend” keyword to include parent class in class definition, due to which all the variables and methods of parent class (except Private) are accessible in child class.

  • What is encapsulation?
Encapsulation can be explained as wrapping up of data into a single unit. It refers to data hiding to prevent any misuse of information. Encapsulation is achieved by the use of access modifiers.

  • Explain access modifiers?
In encapsulation we hide the data by using access modifiers.
Following are the access modifiers -
Public - Variables and methods declared public can be accessed by any class.
Private - Variables and methods declared private can be accessed within that class.
Default - Variables and methods declared default can be accessed by subclass in the same package.
Protected - Variables and methods declared protected can be access by classes in the same package and by subclasses from other packages.

  • What is polymorphism?
Polymorphism is a way to take many forms. There are 2 types of polymorphism-
    • Static Polymorphism
    • Dynamic Polymorphism

  • What is static polymorphism?
Static polymorphism is another word for method overloading, as explained in next question.

  • What is method overloading?
Method overloading is done in the same class. In method overloading, method name should be same but arguments passed in method should not be same.

  • What is dynamic polymorphism?
Dynamic polymorphism is another word for method overriding, as explained in next question.

  • What is method overriding?
Method overriding is done in between sub class and superclass. In method overriding, method name, arguments passed in method and return type should be same.

  • What is abstraction?
Abstraction is a way by which only essential details are visible to the user. Abstraction is achieved by the use of interfaces and abstract class.

  • What is an interface?
An interface contains abstract methods (methods without implementation). By default, all the methods are public in an interface. 
Since Java 8, we can also have default and static methods with implementation in an interface.

  • What in an abstract class?
If a class is declared as “abstract”, then it is an abstract class. It can contain abstract or non abstract method or both. It is created if we do not want to provide implementation of all the methods. An abstract class cannot be instantiated.

  • When should we use interface and abstract class?
In java, we can extend only one class but we can implement more than one interface.
If we have to extend multiple classes in one class, then we should use interface and instead of extending we can implement the interfaces.
If we implement an interface, we have to override all the methods present in an interface but in abstract class we only override abstract methods.