Inheritance

  • Use to avoid redundancy when Classes have many common features
  • Idea is to develop a subclass from a superclass through inheritance.

Java does not support multiple inheritance, that’s why can’t extend a class from two different classes at the same time.

Keyword - extends

To declare a subclass, we use extends keyword.

  • A subclass extends properties and methods from the superclass.
  • Then you can add new properties, methods to subclass
  • Or even override the methods of the superclass (see super keyword)

Lets say an example A (subclass) -> B (superclass)

Now class A :

  • have all the properties, methods of B
  • can declare new properties, methods which B doesn’t have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class A extends B { 

public static void main(String[] args)
{
new A(); // Invoke A constructor
}

public A()
{
// do something
}

// some extra methods
}

class B {
public B()
{
//do something
}

// some methods
}

The Object Class and Its Methods

  • Every class in Java is descended from the java.lang.Object class.
  • Class Object is the root of the class hierarchy
  • If no inheritance is specified when a class is defined, the superclass of the class is Object.
    • you don’t have to specify in code. class C{} is equivalent to class C extends Object{}.

Keyword - super

The keyword super refers to the superclass of the class in which super appears.

keyword super can be used in two ways:

  • To call a superclass constructor
  • To call a superclass method

Superclass’s Constructor Is Always Invoked

  • A constructor may invoke an overloaded constructor or its superclass’s constructor.
  • If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor.

Lets say an example A (subclass) -> B (superclass)

1
2
3
4
public A(){

//do something
}

is equivalent to

1
2
3
4
5
public A(){
super();

//do something
}

Calling Superclass Methods

  • A subclass inherits methods from a superclass.
  • static method can be inherited

Lets say an example A (subclass) -> B (superclass)

  • B has a method called saySomething()
  • A can call B’s method without declaring in A
1
2
3
4
5
6
7
8
9
10
11
12
13
public class A extends B { 

public A()
{
// do something
}

public void print()
{
super.saySomething(); // Calling B's method
}
// some extra methods
}

Overriding Superclass Methods

  • A subclass inherits methods from a superclass.
  • Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass.
  • An instance method can be overridden only if it is accessible.
  • a private method cannot be overridden, because it is not accessible outside its own class
  • If a method defined in a subclass is private in its superclass, the two methods are completely unrelated
  • a static method can be inherited. However, a static method cannot be overridden.

Lets say an example A (subclass) -> B (superclass)

  • B has a public method called saySomething()
  • A can have own version of B’s method by declaring the same method name (also same parameter) in A
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class A extends B { 

public A()
{
// do something
}

public void saySomething()
{
//Do other stuffs instead of super.saySomething();
System.out.println("A has a own version of the method!!");
}
// some extra methods
}

Constructor Chaining

  • Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain.

Example : Faculty(subclass of Employee) -> Employee (subclass of Person) -> Person (superclass)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Faculty extends Employee { 

public static void main(String[] args)
{
new Faculty(); // Invoke Faculty constructor
}

public Faculty() // Faculty constructor
{
System.out.println("(4) Faculty's no-arg constructor is invoked");
}// Now Invoke Employee() constructor

}

class Employee extends Person {
public Employee() //Employee’s no-arg constructor
{
//Invoke Employee(String) constructor
this("(2) Invoke Employee’s overloaded constructor");

System.out.println("(3) Employee's no-arg constructor is invoked");
}// Now Invoke Person() constructor

public Employee(String s) //Employee’s overloaded constructor
{
System.out.println(s);
}
}

class Person {
public Person() //Person constructor
{
System.out.println("(1) Person's no-arg constructor is invoked");
}
}

Output:

1
2
3
4
(1) Person's no-arg constructor is invoked
(2) Invoke Employee’s overloaded constructor
(3) Employee's no-arg constructor is invoked
(4) Faculty's no-arg constructor is invoked