Core Java - OOPs Concepts: Inheritance Interview Questions
1. Why use inheritance in java?
- For Method Overriding (so runtime polymorphism can be achieved).
- For Code Reusability.
Terms used in Inheritance
Class:
–A class is a group of objects which have common properties.
– It is a template or blueprint from which objects are created.
Sub Class/Child Class:
– Subclass is a class which inherits the other class.
– It is also called a derived class, extended class, or child class.
Super Class/Parent Class:
– Superclass is the class from where a subclass inherits the features.
– It is also called a base class or a parent class.
Reusability:
– reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class.
– You can use the same fields and methods already defined in the previous class.
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that a new class that derives from an existing class. “extends” is to increase the functionality.
2.What are types of inheritance in java?
On the basis of class, there can be three types of inheritance in java: single
, multilevel
and hierarchical
.
3.Why is multiple inheritance not supported in java?
-
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
-
Consider a scenario where A, B, and C are three classes.
The C class inherits A and B classes.
If A and B classes have the same method and you call it from child class object
there will be ambiguity to call the method of A or B class. -
Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes.
So whether you have same method or different, there will be compile time error.
4.What is aggregation?
- Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns.
- Aggregation is best described as a has-a relationship.
- For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class. Consider the following example.
public class Address {
String city,state,country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
}
public class Emp implements Cloneable{
int id;
String name;
Address address;
public Emp(int id, String name,Address address) {
this.id = id;
this.name = name;
this.address=address;
}
void display(){
System.out.println(id+" "+name);
System.out.println(address.city+" "+address.state+" "+address.country);
}
public static void main(String[] args) {
Address address1=new Address("gzb","UP","india");
Address address2=new Address("gno","UP","india");
Emp e=new Emp(111,"varun",address1);
Emp e2=new Emp(112,"arun",address2);
e.display();
e2.display();
}
}
111 varun
gzb UP india
112 arun
gno UP india
5.What is composition?
- Holding the reference of a class within some other class is called composition.
- an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
- composition is the particular case of aggregation which represents a stronger relationship between two objects.
- example: A class contains students. A student cannot exist without a class. There exists composition between class and students.
6. What is the difference between aggregation and composition?
- Aggregation represents the weak relationship whereas composition represents the strong relationship.
- For example, the bike has an indicator (aggregation), but the bike has an engine (composition).
7.Why does Java not support pointers?
- the pointer is a variable that refers to the memory address.
- they are not used in Java because it unsafe(unsecured) and complex to understand.
8.What is super in java?
-
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
-
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
Usage of Java super Keyword
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
1) super is used to refer immediate parent class instance variable.
If we print color property, it will print the color of current class by default.
To access the parent property, we need to use super keyword.
public class Animal {
String color="white";
}
public class Dog extends Animal{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
}
}
black
white
2)super can be used to invoke parent class method
The super keyword can also be used to invoke parent class method.
It should be used if subclass contains the same method as parent class.
In other words, it is used if method is overridden.
public class Animal {
void eat(){System.out.println("eating...");}
}
public class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){
super.eat();
bark();
}
public static void main(String args[]){
Dog d=new Dog();
d.work();
}
}
eating...
barking...
- Animal and Dog both classes have eat() method, if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.
- To call the parent class method, we need to use super keyword.
3) super is used to invoke parent class constructor.
- default constructor is provided by compiler automatically if there is no constructor.
- it also adds super() as the first statement.
public class Animal {
Animal(){System.out.println("animal is created");}
}
public class Dog extends Animal{
Dog(){
super();//have no super()is ok
//default constructor is provided by compiler automatically
System.out.println("dog is created");
}
public static void main(String args[]){
Dog d=new Dog();
}
}
animal is created
dog is created
Example:
public class Person {
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}
}
public class Emp extends Person{
float salary;
Emp(int id,String name,float salary){
super(id,name);//reusing parent constructor
this.salary=salary;
}
void display(){System.out.println(id+" "+name+" "+salary);}
public static void main(String[] args){
Emp e1=new Emp(1,"ankit",45000f);
e1.display();
}
}
1 ankit 45000.0
9. What are the differences between this and super keyword?
There are the following differences between this and super keyword.
- The super keyword always points to the parent class contexts whereas this keyword always points to the current class context.
- The super keyword is primarily used for initializing the base class variables within the derived class constructor whereas this keyword primarily used to differentiate between local and instance variables when passed in the class constructor.
- The super and this must be the first statement inside constructor otherwise the compiler will throw an error.
10. Can you use this() and super() both in a constructor?
No, because this() and super() must be the first statement in the class constructor.
public class Test{
Test()
{
super();
this();
System.out.println("Test class object is created");
}
public static void main(String []args){
Test t = new Test();
}
}
Test.java:5: error: call to this must be first statement in constructor