Prev Next

Java / Inheritance

1. What are the differences between composition and inheritance in Java? 2. Are static members inherited to sub classes? 3. What happens if the parent and the child class have a field with same identifier? 4. Are constructors and initializers also inherited to sub classes? 5. How do you restrict a member of a class from inheriting by its sub classes? 6. How do you implement multiple inheritance in java? 7. How Inheritance is implemented in java? 8. Types of inheritance. 9. Can a class extend by itself in Java? 10. Can we reduce the visibility of the inherited or overridden method? 11. How do you override a private method in java? 12. When to overload a method in Java and when to override it? 13. What is the order of extends and implements keyword on Java class declaration? 14. How do you prevent overriding a Java method without using the final modifier? 15. Explain covariant method overriding in Java. 16. What is covariant return type in Java? 17. Benefits of covariant return type in Java. 18. What are the rules of method overriding in Java? 19. Difference between method overriding and overloading in Java. 20. What is Virtual method/function in Java? 21. How to prevent using overloading instead of overriding by mistake? 22. Difference between Inheritance and Encapsulation. 23. What happens when a class implements two interfaces and both have a method with same name and signature? 24. What happens when a class implements two interfaces and both declare field (variable) with same name? 25. Can a subclass instance method override a superclass static method? 26. Can a subclass static method hide superclass instance method? 27. What is method hiding in Java? 28. Can we pass an object of a subclass to a method expecting an object of the superclass? 29. Can a superclass access subclass members?
Could not find what you were looking for? send us the question and we would be happy to answer your question.

What are the differences between composition and inheritance in Java?

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.

Are static members inherited to sub 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.

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.

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.

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.

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.

How Inheritance is implemented in java?

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.

Types of inheritance.

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

Can a class extend by itself in Java?

No. A class can not be self inheriting.

Can we reduce the visibility of the inherited or overridden method?

No.

How do you override a private method in java?

Private methods cannot be overridden as it is not visible outside of the class.

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.

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 bytecode of the parent class - which holds the relevant methods and fields.

How do you prevent overriding a Java method without using the final modifier?

Declare that method using private or static keyword to prevent overriding.

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.

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.

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 object type.

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.

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.

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. 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.

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.

These methods are also known as dynamic binding or dynamic dispatch.

How to prevent using overloading instead of overriding by mistake?

Always use @override annotation to avoid such mistakes.

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.

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.

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.

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
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.

Can a subclass instance method override a superclass static method?

No. It results in compilation error at the subclass.

Can a subclass static method hide superclass instance method?

No. It results in compilation error in the subclass.

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 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.

Can we pass an object of a subclass to a method expecting an object of the superclass?

Yes.

Can a superclass access subclass members?

No. Parent class cannot access the child class members. Super class reference variable cannot see subclass object members.

«
»
Java Identifiers

Comments & Discussions