Inheritance
Inheritance is the process of reusing the code used in
similar class, or a class that is derived from another class is called a subclass (also
a derived class, extended class, or child class). The class from
which the subclass is derived is called a superclass (also a base
class or a parent class).
Every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Example:
class User
{
String name;
int age;
long ph;
void salary()
{
System.out.println("salary");
}
}
class Employee extends User
{
String specialization;
}
class Manager extends User
{
String department;
}
class Main
{
public static void main(String[] args) {
Employee e1=new Employee();
e1.name="Candid";
e1.age=22;
e1.ph=123456789l;
e1.specialization="Java";
Manager m1=new Manager();
m1.name="java";
m1.age=25;
m1.ph=345789l;
m1.department="HR";
System.out.println(e1.name);
System.out.println(e1.age);
System.out.println(e1.ph);
System.out.println(e1.specialization);
System.out.println(m1.name);
System.out.println(m1.age);
System.out.println(m1.ph);
System.out.println(m1.department);
}}
Inheritance not supported by Java
Multiple inheritance(a class cannot has more than one super
class) is not supported in java
Example of multiple inheritance(not supported in java)
class Vehicle { } class Car { } class Truck extends Vehicle, Car { }
In the above example class Truck tries to occur the property of
both Vehicle and Car Which makes the code to fail at compilation in java.
If we need the both the property of Vehicle and Car in Truck
class we can redesign the code as
class Vehicle { } class Car extends Vehicle { // occurs the property of class Vehicle } class Truck extends Car { // occurs the property of both car and vehicle }
Default super class in Java
Every class in java by defaults extends the class Object from
java.lang package
Example:
class A { } class B extends A { }
Here B is the subclass of A, and A is the subclass of Object.
In java, Object is the only class that doesn?t have any super class.
Inheritance with constructor and super()
Constructor cannot be inherited, Only the member of the class
and methods can be inherited.
But, subclass constructor can access the super class constructor
by using super() in its first line.
class A { A() { } } class B extends A { B() { super(); } }
Inheritance with static variables
There is no need for inheriting static variable, static variable
are shared values they can be accessed in any class by specifying its class
name. A subclass inherits all of the public and protected members of its
parent, no matter what package the subclass is in. In the below example
Example
class A { static int a = 10; } class B extends A { } public class StaticVariableInheritance { public static void main(String[] args) { System.out.println(B.a); System.out.println(A.a); } }
The above code compiles fine and produces the output
10
10
Inheritance with static methods
Same as variable there is no need for inheriting the static method, simply static method cannot be inherited.
Inheritance with private variables & methods
Since private variable and methods are accessed only within the
class, they cannot be inherited
Inheritance with abstract class & methods
When a class extends the abstract class all its abstract methods
must be implemented in the subclass unless the subclass is also abstract.
Abstract class are made to be inherited.
Inheritance with final variable and methods
Final variable and method can be inherited but
final method cannot be changed or overridden in its subclass.