Inheritance in Java
Inheritance in Java
import java.io.*;
class Employee {
// Driver Class
class Gfg {
Output
Salary : 60000
Benefits : 10000
In practice, inheritance, and polymorphism are used together in Java to achieve fast
performance and readability of code.
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the image
below, class A serves as a base class for the derived class B.
// Parent class
class one {
public void print_geek()
{
System.out.println("Geeks");
}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well
as the derived class also acts as the base class for other classes. In the below image,
class A serves as a base class for the derived class B, which in turn serves as a base
class for the derived class C. In Java, a class cannot directly access the grandparent’s
members.
import java.io.*;
import java.lang.*;
import java.util.*;
class one {
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();
}
}
. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more
than one subclass. In the below image, class A serves as a base class for the derived
classes B, C, and D.
Java
// Java program to illustrate the
import java.io.*;
import java.lang.*;
import java.util.*;
interface one {
interface two {
System.out.println("Geeks");
}
// Drived class
c.print_geek();
c.print_for();
c.print_geek();
5. Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t
support multiple inheritances with classes, hybrid inheritance involving multiple
inheritance is also not possible with classes. In Java, we can achieve hybrid
inheritance only through Interfaces if we want to involve multiple inheritance to
implement Hybrid inheritance.
However, it is important to note that Hybrid inheritance does not necessarily require
the use of Multiple Inheritance exclusively. It can be achieved through a
combination of Multilevel Inheritance and Hierarchical Inheritance with classes,
Hierarchical and Single Inheritance with classes. Therefore, it is indeed possible to
implement Hybrid inheritance using classes alone, without relying on multiple
inheritance type.
Java Constructors
Java constructors or constructors in Java is a terminology used to construct something
in our programs. A constructor in Java is a special method that is used to initialize
objects. The constructor is called when an object of a class is created. It can be used to
set initial values for object attributes.
What are Constructors in Java?
In Java, Constructor is a block of codes similar to the method. It is called when an
instance of the class is created. At the time of calling the constructor, memory for the
object is allocated in the memory. It is a special type of method that is used to
initialize the object. Every time an object is created using the new() keyword, at least
one constructor is called.
// Java Program to demonstrate
// Constructor
import java.io.*;
class Geeks {
// Constructor
Geeks()
{
super();
System.out.println("Constructor Called");
}
// main function
public static void main(String[] args)
{
Geeks geek = new Geeks();
}
}
How Java Constructors are Different From Java Methods?
Constructors must have the same name as the class within which it is
defined it is not necessary for the method in Java.
Constructors do not return any type while method(s) have the return type
or void if does not return any value.
Constructors are called only once at the time of Object creation while
method(s) can be called any number of times.
Now let us come up with the syntax for the constructor being invoked at the time of
object or instance creation.
class Geek
{
.......
// A Constructor
Geek() {
}
.......
}
Geek obj = new Geek();
The first line of a constructor is a call to super() or this(), (a call to a constructor of a
super-class or an overloaded constructor), if you don’t type in the call to super in
your constructor the compiler will provide you with a non-argument call to super at
the first line of your code, the super constructor must be called to create an object: