Inheritance Interview Questions in Java
Interview Questions on Inheritance in Java
Java Interview Question for Freshers
In Java Programming Inheritance is used for reuse features of one class into another class, Using this concept enhance (improve or increase) performance of application.
What is Inheritance ?
Inheritance is one of the main features of any object oriented programming. Using inheritance concept, a class (Sub Class) can inherit properties of another class (Super Class). So it is used for reuse features.
Main advantage of using Inheritance ?
Using this feature you can reuse features, less memory wastage, less time required to develop and application and improve performance of application.
Types of Inheritance supported by Java ?
Java support only four types of inheritance in java.
- Single Inheritance
- Multi level Inheritance
- Hybrid Inheritance
- Hierarchical Inheritance
Why use Inheritance ?
- For Code Re usability.
- For Method Overriding.
What is multilevel inheritance ?
Getting the properties from one class object to another class object level wise with different priorities.
Which of these keyword must be used to inherit a class ?
- a) super
- b) this
- c) extent
- d) extends
d. extends
What is Multiple Inheritance ?
The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
How Inheritance can be implemented in java ?
Inheritance can be implemented in Java using two keywords, they are;
- extends
- Implements
Which of these keywords is used to refer to member of base class from a sub class ?
- a) upper
- b) super
- c) this
- d) None of these
Answer: b
extends is used for developing inheritance between two classes and two interfaces.Implements keyword is used to developed inheritance between interface and class.
What is syntax of inheritance ?
Syntax
public class subclass extends superclass { //all methods and variables declare here }
Which inheritance are not supported bb Java ?
Multi Inheritance is not supported by java.
How Inheritance can be implemented in java ?
Inheritance can be implemented in JAVA using these two keywords; extends and implements
How do you implement multiple inheritance in java ?
Using interfaces java can support multiple inheritance concept in java.
Syntax
interface A { .... .... } interface B { .... .... } class C implements A,B { .... .... }
How do you implement multiple inheritance in java ?
Using interfaces java can support multiple inheritance concept in java. in java can not extend more than one classes, but a class can implement more than one interfaces.
Why we need to use Inheritance ?
It is used for code re-usability and for Method Overriding.
Can a class extend itself ?
No, A class can't extend itself.
What is syntax of Inheritance ?
Syntax
public class subclass extends superclass { //all methods and variables declare here }
Example of Single Inheritance in Java
Example
class Faculty { float salary=30000; } class Science extends Faculty { float bonous=2000; public static void main(String args[]) { Science obj=new Science(); System.out.println("Salary is:"+obj.salary); System.out.println("Bonous is:"+obj.bonous); } }
why Java Doesn't Support multiple Inheritance ?
Due to ambiguity problem.
What happens if super class and sub class having same field name ?
Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.
How do you implement multiple inheritance in Java ?
Example
interface A { ..... ..... } interface B { ..... ..... } class C implements A,B { }
Can we reduce the visibility of the inherited or overridden method ?
Ans. No.
Which of the following is tightly bound ? Inheritance or Composition ?
Ans. Inheritance.
Does a class inherit the constructor of its super class ?
Ans. No