Inheritance & Types in Java
Inheritance & Types in Java
filter_none
edit
play_arrow
brightness_4
//Java program to illustrate the
// concept of single inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
// Driver class
public class Main
{
public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
Geeks
for
Geeks
filter_none
edit
play_arrow
brightness_4
// Java program to illustrate the
// concept of Multilevel inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
class three extends two
{
public void print_geek()
{
System.out.println("Geeks");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output:
Geeks
for
Geeks
filter_none
edit
play_arrow
brightness_4
// Java program to illustrate the
// concept of Multiple inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
interface one
{
public void print_geek();
}
interface two
{
public void print_for();
}
interface three extends one,two
{
public void print_geek();
}
class child implements three
{
@Override
public void print_geek() {
System.out.println("Geeks");
}
public void print_for()
{
System.out.println("for");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
child c = new child();
c.print_geek();
c.print_for();
c.print_geek();
}
}
Output:
Geeks
for
Geeks
5. Hybrid Inheritance(Through Interfaces) : It is a mix of two
or more of the above types of inheritance. Since java doesn’t
support multiple inheritance with classes, the hybrid inheritance is
also not possible with classes. In java, we can achieve hybrid
inheritance only through Interfaces.
Important facts about inheritance in Java
Default superclass: Except Object class, which has no superclass, 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 class.
Superclass can only be one: A superclass can have any number of
subclasses. But a subclass can have only one superclass. This is because
Java does not support multiple inheritance with classes. Although with
interfaces, multiple inheritance is supported by java.
Inheriting Constructors: A subclass inherits all the members (fields,
methods, and nested classes) from its superclass. Constructors are not
members, so they are not inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.
Private member inheritance: A subclass does not inherit the private
members of its parent class. However, if the superclass has public or protected
methods(like getters and setters) for accessing its private fields, these can also
be used by the subclass.
What all can be done in a Subclass?
In sub-classes we can inherit members as is, replace them, hide them, or
supplement them with new members:
The inherited fields can be used directly, just like any other fields.
We can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
We can write a new instance method in the subclass that has the same
signature as the one in the superclass, thus overriding it (as in example
above, toString() method is overridden).
We can write a new static method in the subclass that has the same signature
as the one in the superclass, thus hiding it.
We can declare new methods in the subclass that are not in the superclass.
We can write a subclass constructor that invokes the constructor of the
superclass, either implicitly or by using the keyword super.
Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another. With the use of inheritance
the information is made manageable in a hierarchical order.
extends Keyword
extends is the keyword used to inherit the properties of a class.
Following is the syntax of extends keyword.
Syntax
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
Sample Code
Following is an example demonstrating Java inheritance. In this example,
you can observe two classes namely Calculation and My_Calculation.
Example
Live Demo
class Calculation {
int z;
z = x + y;
}
public void Subtraction(int x, int y) {
z = x - y;
z = x * y;
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
Output
The sum of the given numbers:30
The difference between the given numbers:10
The product of the given numbers:200
The Superclass reference variable can hold the subclass object, but using
that variable you can access only the members of the superclass, so to
access the members of both classes it is recommended to always create
reference variable to the subclass.
If you consider the above program, you can instantiate the class as given
below. But using the superclass reference variable ( cal in this case) you
cannot call the method multiplication(), which belongs to the subclass
My_Calculation.
demo.addition(a, b);
demo.Subtraction(a, b);
Note − A subclass inherits all the members (fields, methods, and nested
classes) from its superclass. Constructors are not members, so they are
not inherited by subclasses, but the constructor of the superclass can be
invoked from the subclass.
Sample Code
This section provides you a program that demonstrates the usage of
the super keyword.
Example
Live Demo
class Super_class {
// Instantiating subclass
sub.display();
super.display();
obj.my_method();
Compile and execute the above code using the following syntax.
javac Super_Demo
java Super
Sample Code
The program given in this section demonstrates how to use the super
keyword to invoke the parametrized constructor of the superclass. This
program contains a superclass and a subclass, where the superclass
contains a parameterized constructor which accepts a integer value, and
we used the super keyword to invoke the parameterized constructor of
the superclass.
Copy and paste the following program in a file with the name
Subclass.java
Example
Live Demo
class Superclass {
int age;
Superclass(int age) {
this.age = age;
System.out.println("The value of the variable named age in super class is: "
+age);
}
}
Subclass(int age) {
super(age);
s.getAge();
Compile and execute the above code using the following syntax.
javac Subclass
java Subclass
Output
The value of the variable named age in super class is: 24