Prev Next

Java / Inheritance

1. Are constructors and initializers also inherited to sub classes? 2. How Inheritance is implemented in java? 3. How do you implement multiple inheritance in java? 4. How do you restrict a member of a class from inheriting by its sub classes? 5. Are static members inherited to sub classes? 6. What happens if the parent and the child class have a field with same identifier? 7. What are the differences between composition and inheritance 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?

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

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

Read full answer

9. Can a class extend by itself in Java?

No. A class can not be self inheriting.

Read full answer

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

11. How do you override a private method in java?

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

Read full answer

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.

Read full answer

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

Read full answer

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.

Read full answer

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.

Read full answer

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.

Read full answer

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

Read full answer

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

Read full answer

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. One of them would exist in the parent class, while another will b...

Read full answer

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. These methods are also known as dynamic binding or dynamic dispatch.

Read full answer

21. How to prevent using overloading instead of overriding by mistake?

Always use @override annotation to avoid such mistakes.

Read full answer

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. Encapsulation is an object oriented concept which is used to hide the internal details of a class, for...

Read full answer

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. The below snippet compiles and runs. public interface InterfaceA { void method1(); } public in...

Read full answer

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.

Read full answer

25. Can a subclass instance method override a superclass static method?

No. It results in compilation error at the subclass.

Read full answer

26. Can a subclass static method hide superclass instance method?

No. It results in compilation error in the subclass.

Read full answer

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 will get invoked but if you call the same method from superclass then the ...

Read full answer

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?

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

Read full answer

«
»

Comments & Discussions