Prev Next

Java / Inheritance

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

Composition Inheritance has-a relationship between classes. is-a relationship between classes....

Read full answer

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.

Read full answer

3. Types of inheritance.

Single Inheritance. One class is extended by only one class....

Read full answer

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.

Read full answer

5. How Inheritance is implemented in java?

Inheritance is implemented in JAVA using below two keywords, extends. implements.

Read full answer

6. Are static members inherited to sub classes?

No. Static members cannot be inherited.

Read full answer

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.

Read full answer

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.

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

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

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

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

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.

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

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

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

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.

Read full answer

«
»

Comments & Discussions