Inheritance in java is one of the key features of OOP that allows us to create a new class from an existing class. The new class that is created is known as subclass (child or derived class) and the existing class from where the child class is derived is known as superclass (parent or base class).
Here are the different types of inheritance in Java:
- Single Inheritance: In Single Inheritance one class extends another class (one class only). …
- Multiple Inheritance:
- Multilevel Inheritance:
- Hierarchical Inheritance:
- Hybrid Inheritance:
class Base{
private int id;
String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Derived extends Base{
int salary;
public void setSalary(int salary) {
this.salary = salary;
}
public int getSalary() {
return salary;
}
}
public class tut10 {
public static void main(String[] args) {
Derived d1 = new Derived();
Base b1 = new Base();
d1.setId(1);
d1.setName("Rohan");
d1.setSalary(100000);
System.out.println(d1.getId());
System.out.println(d1.getName());
System.out.println(d1.getSalary());
}
}