Java / Inheritance
Composition | Inheritance |
has-a relationship between classes. | is-a relationship between classes. |
Composing object holds a reference to composing classes and hence relationship is loosely bound. | Derived object carries the base class definition in itself and hence its tightly bound. |
Single class objects can be composed within multiple classes. | Single class can only inherit one Class. |
Its the relationship between objects. | Its the relationship between classes. |
No. Static members cannot be inherited. However super class and the sub class can have static method with same signature.
Super class static member will be hidden at the sub class.
Super class field member will be hidden at the sub class and super class field could be accessed using super keyword.
No, Constructors and initializers (Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.
Inheritance is implemented in JAVA using below two keywords,
- extends.
- implements.
extends inherits between two classes and two interfaces.
implements inheritance between an interface and class.
Single Inheritance.
One class is extended by only one class.
Multilevel Inheritance.
One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.
Hierarchical Inheritance.
One class is extended by many classes.
Hybrid Inheritance.
It is a combination of above types of inheritance.
Multiple Inheritance.
It is a combination of above types of inheritance.
One class extends more than one classes. (Java does not support multiple inheritance.)
Through interfaces, we can implement multiple inheritance in java. As classes in java can not extend more than one classes, but a class can implement more than one interfaces
By declaring that member as a private. Because, private members are not inherited to sub classes.
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.
No. A class can not be self inheriting.
No.
Private methods cannot be overridden as it is not visible outside of the class.
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.
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 bytecode of the parent class - which holds the relevant methods and fields.
Declare that method using private or static keyword to prevent overriding.
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.
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.
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 object type.
private, final and static methods cannot be overridden.
The overriding method must have same argument list.
The overriding method must have same return type or covariant return type.
The overriding method cannot reduce the method visibility.
The overriding method must not throw new or broader checked exceptions.
Overriding method can increase access of overridden method.
Use the super keyword to invoke the overridden method from a subclass.
Constructors cannot be overridden.
The synchronized and the strictfp modifier has no effect on the rules of overriding.
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. One of them would exist in the parent class, while another will be in the derived, or child class.
Calls to overloaded methods are realized at compile time. Thats why it is static. Calls to overridden methods are realized at run-time, based on the type on which the method is invoked.
In Java, all non-static methods are by default virtual functions. Methods that are marked with the keyword final are non-virtual.
These methods are also known as dynamic binding or dynamic dispatch.
Always use @override
annotation to avoid such mistakes.
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.
Encapsulation is an object oriented concept which is used to hide the internal details of a class, for example, HashMap encapsulate how it store elements and calculate hash values.
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.
The below snippet compiles and runs.
public interface InterfaceA { void method1(); } public interface InterfaceB { void method1(); } public class ClassImplementing2Interface implements InterfaceA,InterfaceB { @Override public void method1() { System.out.println("hello from method1"); } public static void main(String[] args) { new ClassImplementing2Interface().method1(); } }
Output:
hello from method1
The field becomes ambiguous and we get compile time error.
No. It results in compilation error at the subclass.
No. It results in compilation error in the subclass.
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 will get invoked but if you call the same method from superclass then the one in superclass will be invoked. This is known as method hiding in Java.
Yes.
No. Parent class cannot access the child class members. Super class reference variable cannot see subclass object members.