Inheritance In the Java programming language

Inheritance in the java programming language

In this tutorial, we will discuss Inheritance In Java programming language

Knowledge Area

What is inheritance?
Type of Inheritance
extends and super keyword
Method overriding
Abstract Class & Methods

what is inheritance?
Inheritance is one of the feature or mechanism of Object-Oriented programming Languages(OOPs). Inheritance allows a class to use the properties or data members and methods of another class.so The derived class inherits or acquire the states and behaviours from the base class. The derived class is also called subclass and the base class also called superclass(parent class). it is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces.

Type of inheritance
Simple Inheritance
Multi-Level inheritance

extends keyword
extends is the keyword used to inherit or acquire the properties of a class

syntax

class superorparent{

}
class suporchild{

}

Simple inheritance

 

Single level

Example 1

class base
{
void display()
{
System.out.println(“parent display”);
}
}

class derived extends base
{
void display1()
{
System.out.println(“child display”);
}
}
public class simpleinherits
{
public static void main(String args[])
{
derived obj=new derived();
obj.display();
obj.display1();
}
}

Example

Multi-Level

class base
{
void display()
{
System.out.println(“parent display”);
}
}
class inter extends base
{
void display1()
{
System.out.println(“intermediate display”);
}
}
class derived extends inter
{
void display2()
{
System.out.println(“derived display”);
}
}
public class multiinherits
{
public static void main(String args[])
{
derived obj=new derived();
obj.display();
obj.display1();
obj.display2();
}
}

 

When the above code is executed, it produces the following result

Example

 

implement (Multiple inheritance)  in java

Example 1

interface shape
{
double pi=3.14;
void calculate(float f1,float f2);
}
class circle implements shape
{
public void calculate(float x, float y)
{
System.out.println(pi*x*y);
}
}
class rectangle implements shape
{
public void calculate(float x, float y)
{
System.out.println(x*y);
}
}
class multipleinheri
{
public static void main(String args[])
{
shape s1;
circle c1=new circle();
rectangle r1=new rectangle();
s1=c1;
s1.calculate(10,1);
s1=r1;
s1.calculate(10,2);
}
}

Example
Suggested post
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.