Q1.What is inheritance in java?
Inheritance is the oops concept and in java inheritance means re-usability. By using this inheritance concept a child class can inherit its parent class so that the child class can reuse all the field and methods of its parent class.
Q2.What are the types of inheritance?
There are 5 types of inheritance. Suppose there is a class A, B, C, and D.
1) Single level inheritance.
B extends A
2) Multilevel inheritance.
C extends B and B extends A
3) Multiple inheritance.
C extends B and A
4) Hierarchical inheritance.
C and B extends A
5) Hybrid inheritance.
D extends C and B and C and B extends A
Q3.Use of inheritance in java?
There are many reasons for using inheritance in java and these are given below.
To achieve dynamic binding or method overriding.
For code re-usability.
To save time.
Q4.Does java support multiple inheritance?
No, Java doesn't support multiple inheritance because of complexity and ambiguity in a program. Java does not support multiple inheritance through the class but it is possible through the interface.
Q5. What is the syntax of inheritance?
There is the syntax of inheritance.
//Single levele inheritance
class B extends A
Q6.How to restrict a member of a class from inheriting by its sub classes?
Private members are not used by anything. Therefore, the Programmer can restrict a member of a class from inheriting its subclasses by using a private access specifier.
Download Free : Java inheritance interview questions PDF
Q7. Explain covariant method overriding in Java.
Covariant method overriding helps to remove typecasting on the client-side. It allows you to return a subtype of the overridden method.
Q8.Explain aggregation in java?
In Java, Aggregation is described as a "has-a" and "whole/part" relationship that shows the relationship between two classes. The main purpose of Aggregation is reusability. For instance, A car object is an aggregation of an engine, seat, wheels, and other objects.
Q9. Why Multiple inheritance is not supported by Java?
Multiple Inheritance is a mechanism by which a subclass acquires the properties of more than one class. A class cannot be derived from more than one class in JAVA. For instance, consider a case where class C extends class A and class B and both class A and B have the same method display() because the java compiler cannot decide which display method it should inherit. In Java, The main reason behind the impossibility of multiple inheritances is to prevent ambiguity. To overcome this problem, The concept of interface is adopted in Java by which a class can implement one or more interfaces to achieve multiple inheritances.
Q10. What is multi level inheritance in Java?
Multilevel inheritance refers to a mechanism by which you can easily acquire the properties and behaviour of a class into some other classes. In multilevel inheritance, a class C can acquire the properties and behaviour of a derived class B that is derived from class A. Now the derived class B becomes the base class and the new class C becomes the derived class. When a class extends a class, which extends another class then this mechanism is called multilevel inheritance. For instance, class C extends class B and class B extends class A then this type of inheritance is known as multilevel inheritance.
Q11. Enlist few advantages of inheritance?
Few advantages of inheritance are listed below:
Inheritance reduces code redundancy.
It provides code reusability.
It reduces source code size and improves code readability.
It generates a Manageable code.
Q12.What are the differences between ‘this’ and ‘super’ keyword?
Using ‘this’ from within one class constructor, a call can be made to another constructor of the same class. Whereas, using ‘super’ keyword superclass constructor is called from sub-class constructor.
Q13.What are the rules to be followed while overriding a method?
The following are the rules to be followed while overriding a method:
The overriding method has the same name, number, and type of parameters, and return type as the method it overrides.
For the child method accessibility should not be reduced than that of the parent class method, equal is ok or higher is ok.
In the child method exception specification, extra checked exception classes should not be listed then that of parent class method, equal is ok or less is ok.
Q14.What is the base class of all classes?
The java.lang.An object is the base class of all classes.
Q15.Does Java support multiple inheritances?
Java doesn’t support multiple inheritances.
Q16.How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can’t be changed also. static final int PI = 2.14; is an example for constant.
Q17.What is the purpose of declaring a variable as ‘final’?
A final variable’s value can’t be changed. The ‘final’ variables should be initialized before using them.
Q18.What is the impact of declaring a method as final?
A method declared as final can’t be overridden. A sub-class can’t have the same method signature with a different implementation.
Q19.I don’t want my class to be inherited by any other class. What should I do?
You should declare your class as final. But you can’t define your class as final if it is an abstract class. A class declared as final can’t be extended by any other class.
Q20.Can you give a few examples of final classes defined in Java API?
java.lang.String, java.lang.Math is the ‘final’ classes.
Q21.How is the final different from finally and finalize()?
The ‘final’ is a modifier that can be applied to a class or a method or a variable. ‘final’ class can’t be inherited, the final method can’t be overridden and final variable can’t be changed. The ‘finally’ is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment. The ‘finalize()’ is a method of the Object class that will be executed by the JVM just before garbage collecting objects to give a final chance for resource releasing activity.