Java / Inheritance
1. What are the differences between composition and inheritance in Java?
Composition Inheritance has-a relationship between classes. is-a relationship between classes....
2. How do you restrict a member of a class from inheriting by its sub classes?
By declaring that member as a private. Because, private members are not inherited to sub classes.
3. Types of inheritance.
Single Inheritance. One class is extended by only one class....
4. How do you implement multiple inheritance in java?
Using interface, we can implement multiple inheritance in java. Classes in java can not extend more than one classes, but a class can implement more than one interfaces.
5. How Inheritance is implemented in java?
Inheritance is implemented in JAVA using below two keywords, extends. implements.
6. Are static members inherited to sub classes?
No. Static members cannot be inherited.
7. Are constructors and initializers also inherited to sub classes?
No, Constructors and initializers (Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.
8. What happens if the parent and the child class have a field with same identifier?
Super class field member will be hidden at the sub class and super class field could be accessed using super keyword.
9. Can a class extend by itself in Java?
No. A class can not be self inheriting.
10. Can we reduce the visibility of the inherited or overridden method?
No.
11. How do you override a private method in java?
Private methods cannot be overridden as it is not visible outside of the class.
12. When to overload a method in Java and when to override it?
If a class has a better implementation or most appropriate method implementation then override the method while overloading is performing the same functionality however with different input datatypes/object.
13. What is the order of extends and implements keyword on Java class declaration?
The extends always precedes the implements keyword in any Java class declaration. When the Java compiler compiles a class into bytecode, it must first look to a parent class because the underlying implementation of classes is to point to the...
14. How do you prevent overriding a Java method without using the final modifier?
Declare that method using private or static keyword to prevent overriding.
15. Explain covariant method overriding in Java.
Covariant method overriding, introduced in Java 5, helps to remove type casting on client side, by allowing you to return subtype of actually return type of overridden method.
16. What is covariant return type in Java?
When a method is overriden, the return type of the overriding method is allowed to be a subtype of the overridden method's return type is referred as covariant return type.
17. Benefits of covariant return type in Java.
It eliminates unnecessary downcasting from the super type to the subtype. For example object clone method can be overriden by implementing Clonable interface and the implementing class return the object of its own type while the object clone method returns...
18. What are the rules of method overriding in Java?
private, final and static methods cannot be overridden. The overriding method must have same argument list....
19. Difference between method overriding and overloading in Java.
Method overloading deals with the notion of having two or more methods in the same class with the same name but different arguments. Method overriding means having two methods with the same arguments, but different implementations....
20. What is Virtual method/function in Java?
In Java, all non-static methods are by default virtual functions. Methods that are marked with the keyword final are non-virtual.
21. How to prevent using overloading instead of overriding by mistake?
Always use @override annotation to avoid such mistakes.
22. Difference between Inheritance and Encapsulation.
Inheritance is an object oriented concept which creates a parent-child relationship. It is one of the ways to reuse the code written for parent class but it also forms the basis of Polymorphism....
23. What happens when a class implements two interfaces and both have a method with same name and signature?
This is a valid scenario. If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable....
24. What happens when a class implements two interfaces and both declare field (variable) with same name?
The field becomes ambiguous and we get compile time error.
25. Can a subclass instance method override a superclass static method?
No. It results in compilation error at the subclass.
26. Can a subclass static method hide superclass instance method?
No. It results in compilation error in the subclass.
27. What is method hiding in Java?
Static methods cannot be overridden in Java, but if you declare the same static method in subclass then that would hide the method from its superclass. So, if you call that method from subclass then the one in the subclass...
28. Can we pass an object of a subclass to a method expecting an object of the superclass?
Yes.
29. Can a superclass access subclass members?
No. Parent class cannot access the child class members.