Core Java – OOPs Concepts: OOPs Interview Questions
- What are the principle concepts of OOPS?
There are four principle concepts upon which object oriented design and programming rest. They are:
- What is Abstraction?
Abstraction is process of representing essential features or functions without including the background details or explanations.
Abstraction is achieved by using interface and abstract class in Java.
3. What is Encapsulation?
Encapsulation is process of hiding the properties and behaviors of an object and. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.
- What is the difference between abstraction and encapsulation?
- Abstraction focuses on the outside view of an object; Encapsulation (information hiding) prevents clients from seeing it’s inside view.
- Abstraction solves the problem in the design side while Encapsulation is the Implementation.
- Abstraction is implemented using interface and abstract class , where as while Encapsulation is implemented using private, package-private and protected access modifier.
- What is Inheritance?
Inheritance is the process by which objects of one class acquire the properties of objects of another class. Inheritance allows a class to inherit property of another class.
- A class that is inherited is called a super class.
- The class that inherits a super class is called a subclass.
- Inheritance is done by using the keyword extends.
- The two most common reasons to use inheritance are:
- For code reuse
- To use polymorphism
- What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
- How does Java implement polymorphism?
Inheritance, Overloading and Overriding are used to achieve Polymorphism in java.
- In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods).
- In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods).
- Explain the different forms of Polymorphism.
There are two types of polymorphism one is Compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface.
- Method overloading
- Method overriding through inheritance
- Method overriding through the Java interface
- What is runtime polymorphism or dynamic method dispatch?
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.
- What is method overloading?
If two or more method in a class has same name but different parameters, it is known as method overloading.
Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing number of arguments or by changing the data type of arguments. If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.
- Overloaded methods MUST change the argument list
- Overloaded methods CAN change the return type
- Overloaded methods CAN change the access modifier
- Overloaded methods CAN declare new or broader checked exceptions
11.What is method overriding?
Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its super class. The key benefit of overriding is the ability to add behavior that’s specific to a particular subclass type.
- The overriding method cannot have a more restrictive access modifier than the method being overridden Eg: We can’t override a method marked public and make it protected.
- We cannot override a method marked final
- We cannot override a method marked static
12.Is it possible to override the main method?
NO, because main is a static method. A static method can’t be overridden in Java.