Inheritance is
a process by which one class acquires properties (data members and methods) of
another class. It is very essential feature of object oriented programming.
Most important aspect of inheritance is reusability. We have to only unique
features in derived class, other common features is extended from the base
class.
Derived Class: The
class that inherits the properties (data members and methods) of base class.
Base Class: The
class whose properties (data members and methods) are extended by derived
class.
Uses of Java Inheritance
- Code Reusability
- Method overriding (runtime polymorphism)
- Save time for writing same code over and over.
Syntax of Java Inheritance
class Derivedclass-name extends Baseclass-name
{
//data members and methods
}
The “extends” keyword is used for
inheritance in java. The “extends” means increase the features.
Java Inheritance Example
In this example, Student class extends Person class.
Person class has two data members and one parametrized constructor. Student
class inherits the properties of person class. All students have a name, age
and roll no. For storing name and age of students, data members of person class
are used. And age of student is stored in data member of student class. In the
Student class constructor, we call the Person class constructor using super keyword.
class Person
{
String name;
int age;
Person(String n, int a)
{
name = n;
age = a;
}
}
class Student extends Person
{
int rollno;
Student(String n, int a, int r)
{
super(n, a);
rollno = r;
}
void showDetails()
{
System.out.println("Name : "+super.name);
System.out.println("Age : "+super.age);
System.out.println("Roll no : "+rollno);
}
}
class Inheritance
{
public static void main(String[] args) {
Student s = new Student("John", 18, 30);
s.showDetails();
}
}
Output:
Name : John
Age : 18
Roll no : 30
Types
of Inheritance in Java
Following are the types of inheritance supported by java.
Single Inheritance
Single Inheritance |
In
single inheritance, there is only one base class and derived. This type of inheritance
is very simple and easy to understand.
Multilevel Inheritance
Multilevel Inheritance |
In
multilevel inheritance, derived inherits the properties of base class and
further another class inherits properties of that derived class and so on. For
example, class B inherits properties of class A and class C inherits properties
of class B.
Hierarchical Inheritance
Hierarchical Inheritance |
In
hierarchical inheritance, multiple derived classes inherits the properties of
single base class. For example, class B and class C inherits properties of
class A.
Multiple Inheritance
Multiple Inheritance |
Multiple inheritance cannot be achieved by class, it is only
achieved by interfaces. In multiple inheritance, an interface inherits the
properties of two or more two interfaces. For example, interface C inherits
properties of interface A and B
Hybrid Inheritance
It is
combination of more than one type of inheritance. For example, a combination of
single and hierarchical inheritance.
0 comments:
Post a Comment