Java OOPs Concepts Interview Questions

OOPs concepts are very important in any java interview. This page contains the most important and commonly asked OOPs concepts related interview questions with proper answers. For more questions on various topics of core java navigate thorough the links privided in below table or at bottom of page.

Overview : Most Imp Questions Java Basic Interview Questions
OOPS Concepts Interview Questions Exception Handling Questions
MultiThreading & Concurrency API Java API : String, File I/O
Collection Framework Questions Advanced Questions

OOPs stands for Object Oriented Programming Structure. There are 4 main OOPs concepts:

Abstraction

Showing only what is relevent to one by hiding unncessary compalixities.

Encapsulation

To wrap up data the the functions that operate on this data into one single unit.

Inheritance

Child class inherite the properties of Parent/base class.

Polymorphism

Poly (many) + Morphs (forms). There are two types of Polymorphism in Java:
Method Overloading (or compile time ploymorphism) and Method Overriding(or runtime polymorphism).
public, protected, default, private.
If you assign object of subclass to the super class reference variable. Now when you invoke an overridden method with reference of super class, it invokes the subclass implementation of the method. This resolution happens at runtime and the process is called dynamic dispatch. It's same as runtime polymorphism.
Note: Class variable binding happen at compile time. So variables don't take part in dynamic dispatch.
Overloading Overriding
Overloading happens between method of same class. Overriding happens between method of parent and child class.
Signature (number, type or order of parameters) of overloaded methods must be different. Signature must be same in order to override a method.
Return type doesn't matter for overloaded methods. Return type must be same or subclass of overridden method return type.
It is immaterial what exception is thrown by method. Overridden method can throw same exception or child of it.
Access specifier are irrelavant. You can't narrow the visibility of overridden method. It must have either same or higher access specifier.
Interface Abstract class
All methods of Interface must be abstract. Abstract class can have abstract methods as well as concrete methods.
All method of interface are abstract and public by default. No such default behaviour for methods of Abstract class.
All variables of Interface are public static final by default. This is not case with Abstract class
Yes, abstract class can have constructor, and infact it always has one. If you don't define explicitly it will have default constructor.
The construcotr in abstract class is used for inheritance. When you extend a class, you need constructor in that class.
Using new() operator.
Usinng newInstance() method.
Static means something which is same for all objects of the class. Hence its not associated with any object of the class but with class itself. In Java member variables and methods can be static.
No. Static method can't be overridden. Though a subclass can have same static method as super class, but it is not called overriding its called method hiding.
The static methods don't follow runtime polymorphism as well. The reason for that is static method is associated with class and not object. Alos the binding happens at compiletime.
No. We can't declare a class as static. Only class members can be declared as static.
Note: An inner class can be declared static, because its member of a class.
No. You can't make a class private. It gives compilation error - 'Illegal modifier for the class...'.
Note: Only public, abstract, final modifier are allowed before class. So a class can have either public or default access specifier.

super

super is a keyword which you can use in subclass to refer to members (methods, variables) of superclass.

super()

super() refers to default(no-arg) constructor of superclass. If you call super() inside your constructor it must be first statement. If you don't call it explicitly, compiler will insert it, in that case make sure your superclass has no-arg constructor otherwise you will get compilation error.
Final means something which can't be changed. Java class, method and variables can be declared as final.

Final class means the class can't be extended. A final class can't have abstract methods.
Final method can't be overridden. A method can't be final and abstract at same time.
You can't change value of final variable. A final instance (or static) variable must be initialized and declared in same statment. Local final variable can be initialized later, but once initialized its value can't be changed.
If you have a parent class which has method display. Now the parent class is inherited by two classes child1 and child2 which both give different implementation to the display method. Now when you declared child3 which extend chaild1 and child2 both, then it is not possible to resolve which implementation of print method it will get. That's why multiple inheritance (via classes) is not allowed in Java. This problem is also called 'deadly diamond of death'.



<< Previous Page : 1 2 3 4 5 6 7 8 Next >>