Note 1: The answers do not use more technical buzz words. It is done to understand the concept very clearly so that a novice can put it in his own words in interviews.
OOPS Concepts
A variable declared inside a method is known as "local variable" and its usage is limited within the method in which it is declared only. A "global variable" (known as instance variable in Java) is declared outside of any method but wihtin the class. Its scope is for the entire class; that is, any method can use it.
"An instance of a class is known as an object" like an instance (object) of wood is a door or table. Without object (like door or table), the wood is mere waste and similarly without object the class is waste. Remember, with object only we access a constructor, call a method etc.
A class is structure which holds the code and delimits (separates) the code from other classes. A class demands the programmer to write any code within the braces of the class (not outside the braces) and for this reason the main() is put within the class.
Creating an object of another class in our class and calling other class methods is known as "Composition". Composition increases reusability (and the other style is "inheritance"). Composition establishes "has-a" relationship.
Whenever an instance (global) variable is called with an object, they are binded together so that the variable is accessible to that object only. Now, one location is created in the memory and the location is private to that object only; only that object can access or modfiy the value.
The result of data binding is data hiding. When two objects are calling the same instance variable, two locations are created in the memory. Eventhough, the two objects belong to the same class and the instance variable is only one, they are not known to each other. One object cannot access other object's variable. They are hidden from each other. This is known as data hiding. It is possible only with OOPS languages.
The data binding leads to data hiding and data hiding leads to encasulation. The data hiding can be compared to encasulation. Encapsulation is a pharmaceutical term where a drug is placed inside a gelatine capsule. By putting the drug inside the capsule, the Pharmacist hides the properties of the drug like its color and taste. Similarly in data hiding, the value associated with one object is hidden from other objects. This is known as "encapsulation" where the data is encapsulated with object, one of the OOPS concepts and the other two are inheritance and polymorphism. One final word: "the result of encapsulation is one instance (global) variable can be given multiple values (of course, by calling with different objects) which is not possible with procedural languages like C".
A method (known familiarly as function is C-lang) is a container that holds a block of statements delimited by braces. Java categorizes the methods into static, non-static (known as concrete), final, abstract and synchronized depending on the coding needs.
Declaring the same method with different parameters in the same class is known as "method overloading", a concept supported by C++ and Java but not by C-lang. Compiler differentiates which overloaded method is to be called depending on the parameter list – their number and sequence of parameters. Method overloading leads to "static polymorphism"
In method overloading, which overloaded method is to be called is decided at compile-time itself. As decided at compile-time (and not at runtime), the performance increases because at runtime, simply methods are called (as already decided at compile-time). This is known as "static binding".
The static binding leads to static polymorphism. The same method called at different times gives different outputs – the same calculateArea() method my give the area of different geometrical figures like square and circle etc. This is known as poly (many) morphism (forms). As polymorphism is achieved with static binding, this known as static polymorphism and the other is known as "dynamic polymorphism" which Java supports.
Inheritance is an OOPS concept where an object one class is permitted to call other class methods. This will not come free of cost. These two classes should be linked together. The linking keyword is "extends" incase of concrete and abstract clases or "implements" incase of interfaces. When linked, the two classes establih a relation known as "inheritance". Inheritance increases code reusability (the other way is composition).
The inheritance relation results in two new names to classes involved. The class which extends is known as "subclass" and the other which is getting extended is known as "super class". By virtue of of inheritance, a subclass object can call super call properties (like variables, methods and constructors) but superclass cannot call subclass properites. This is one-way inheritance (one-way traffic).
If the super class and the subclass have the same method, we say the super class method is overridden by subclass. This is known as "method overriding".
In case of method overriding, the subclass object prefers to call its own method. That is, as along as subclass class has the same method, the super class method cannot be called by the subclass object. Otherway, we can say, the superclass method is hidden by subclass method. If the subclass also wants the superclass method, it can use "super" keyword. super keyword can be applied to variables and methods but not to classes.
"super" keyword cannot be used from static context. That is, it cannot be used from static methods like main().
"final" is a keyword used as an access modifier (other popular modifiers are static and abstract; remember, access specifiers and access modifiers are different). "final" can be applied to variables, methods and classes and in each place it works (behaves) very differently.
A final variable works like a constant of C-lang. Java does not support "const" keyword and in its place it uses final. A final variable cannot be reassigned.
A final method in the super class cannot be overridden by the subclass.
A final class cannot be extended or inherited by any other class.
A constructor looks like a method but without return type. Moreover, the name of the constructor and class name should be the same.
1. Constructor is called implicitly when an object is created. Regarding method, it should be called explicitly. 2. Constructor gives properties to an object at the time of creation itself (if not, methods should be used to set the properties). 3. Programmer uses constructor to initialize variables, creating objects, setting background and foreground colors etc.
Just like a method can be overloaded, a constructor also can be overloaded. Depending on the parameter list, appropriate overloaded constructor is created.
These two are used with respect to constructors only. "this()" is used to access one constructor from another within the same class. "super()" is used to access super class constructor from subclass constructor.
A method that does not contain a body.
A class that cannot be instantiated permitting the presence of concrete and abstract methods.
An interface is a special type of abstract class where all methods should be abstract. The special feature of interface is it supports multiple inheritance.
Both are keywords used with inheritance. "extends" is used with concrete and abstract classes and "implements" is used with interfaces.
Basically, Java does not support multiple inheritance but supports partially through interfaces.
Pass your comments and suggestions for the improvement of this tutorial "OOPS Java Interview Questions".
hello sir
I need to do java certification ..im based in london,united kingdom…please suggest any websites or materials…thank you
Once you Google, you get lot of web sites. Choose easier one at the beginning. Anyhow do this at the beginning.
http://way2java.com/java-questions/test-your-java-1/
Please get me information about how to get passed in OCA examination
Keep doing mock tests available in numerous web sites.
Hi, i am preparing for oca java7 exam, please guide me from where can i prepare for MCQ , and from
where can i get discount vouchers while i live in Pakistan. thanks for your guide in advance.
I can advise if you are in Hyderabad, India. To pass the exam, best source if web sites.
why class name and constructor name should be same?