Wednesday, August 23, 2017

Java Programming Interview Questions Series 02 - OOP Concepts





1.      What are the relationships in OOP?                     
                   There two types of relationships in Java.
·  IS-A relationship - Inheritance is an example of IS-A relationship.
·  HAS-A relationship - Aggregation is an example of HAS-A relationship.

2.     What is the difference between StringBuffer and String?
The String is an immutable class. You cannot modify its content once created. If we change the content of the String object, it creates a new string and refers to that new String. But StringBuffer is a mutable class. That means you can change/replace the content later.

3.     Can we use multiple inheritances in Java?
There are 4 types of inheritances that we can find in Java.
1.     Single Inheritance
2.     Multilevel Inheritance
3.     Hierarchical Inheritance
4.     Multiple Inheritance

But Multiple Inheritance is not supported in Java. Because,
·        To remove ambiguity.

·        To provide more maintainable and clear design.

4.     What is the difference between super() and this()?
Basically super() keyword comes from Inheritance. It is used to refer,
·        Immediate parent class constructor,
·        Immediate parent class variable,
·        Immediate parent class method.
   This() keyword is usually used to refer the current object whose method is being invoked. From this() keyword, we can refer any member in the current object.

5.     Can we use both super() and this() in a constructor?
           We can't use. Because both super() and this() must be the first statement inside a constructor. Once we call super() or this() constructor, it doesn’t work as method calling. It just map the super() or this() objects. Therefore we can’t use it.

6.     What is aggregation in java and why we need this?
Aggregation is a special form of association (HAS-A relationship). But it is not the main feature in Java. It has a one-way association. For an example, consider two classes called Student class and Address class, each student must have an address but each address may not have a student and it doesn’t make any sense. That is why it has a one-way association. Below example shows the how aggregation can be used.

  


You can see that in Student class, Address class is used. From that way, we obtain student address. This relationship is called Aggregation.
To maintain code reusability we need Aggregation. Let’s think that we have two another class of School and staff. In order to maintain all the classes’ addresses, we don’t just have to use the reference of Address class by defining inside each class.

7.     When to use Inheritance and Aggregation?
Using the Aggregation is the better option when you need to use property and behaviour of a class without modifying it inside your class. On the other hand, Inheritance is better to use when you need to use and modify property and behaviour of a class inside your class.

8.     What is Overriding?
Overriding occurs when we use Inheritance. In this feature, that allows the sub class to give a particular implementation of a method. But that method is already provided by one of its super class. If that method in the sub class has the same name, same return type, same signature and same parameters as a method in its super class, at that point the method in the sub class is said to override the method in the super class.

9.     Why Overriding phenomena happen?
If we use a sub class object using sub class reference and invoke a method, the compiler will check the sub class first and if it cannot find such method then it will check the super class.
If we use a sub class object using super class reference and invoke a method, the compiler will check the super class. But if it cannot find such method it will not check in sub class. Because the super class is not extended the sub class. Therefore it will create a compilation error.

10. What is the difference between Overloading and Overriding?

Method Overloading
Method Overriding
1.     The name must be same but parameter must be different.
2.     Increase readability of the code.
3.     Compile time polymorphism.
4.     Access specifier can be changed.
1.     Both name and parameter must be same.
2.     Increase reusability of the code.
3.     Runtime polymorphism.
4.     Access specifier cannot be more restrictive than the original method.


11. Can we override static method?
No, we cannot override static methods. Because static methods exist in the place called Class area.  On the other hand method overriding is associated with the object at runtime and they exist in the place called Heap.

12. What are Upcasting and Downcasting?
When a child class object is referenced by parent class reference variable is called Upcasting.
When a parent class object is referenced by child class reference variable is called Downcasting.

 
Parent p = new Parent();         
Child c = new Child();


Parent p = new Child();  //This is Upcasting.
          Child c = new Parent(); //This is Downcasting. 




But in Downcasting it gives a compilation error saying ‘incompatible type’. Therefore we can cast the Parent object to Student object.
                   Child c = (Child) new Parent();
This will be compiled successfully but throws ‘ClassCastException’ at runtime.

13. Can we override static methods exist in the Super class?
No. We cannot override static methods. Because overriding happens only for the instances that are created temporally when they are invoked. But once we static a method it exists in the Class area and we can directly access it at any time without any object.

14. Why static methods do not need (but can access) any object of a class?
Because all the static variables and static methods exist in the place ‘Class Area’. Therefore without any instance of a class, we can access any method or variable which are static.

15. Why a final class cannot be inherited but a final method can be inherited?
‘Final’ keyword has a different meaning when it uses for classes, methods and variables.
     a.  A final class cannot be extended.
     b. A final method cannot be overridden.
     c.  A final variable cannot be assigned after it has been initialized.   

That is because the language designers chose to design the language in that way.









0 comments:

Post a Comment

Copyright © iTecTricks | Powered By Blogger

Design by Shehan Vanderputt