Updated June 14, 2023
Introduction to Single Inheritance in Java
Single inheritance can be defined as a derived class to inherit the basic methods (data members and variables) and behavior from a superclass. It’s a basic is-a relationship concept that exists here. Java only uses a single inheritance as a subclass and cannot extend more superclasses.
Inheritance is the basic properties of object-oriented programming. Inheritance tends to use the properties of a class object in another object. Java uses inheritance for code-reusability to reduce time by enhancing reliability and achieving run-time polymorphism. As the codes are reused, it makes less development cost and maintenance. Java has different types of inheritance, namely single inheritance, multilevel, multiple, and hybrid. This article shall briefly go through a basic understanding of the single inheritance concept in java with a programming example. Here we shall have a complete implementation in java.
Syntax:
The general syntax for this is given below. The inheritance concepts use the keyword ‘extend’ to inherit a specific class. Here you will learn how to use extending keywords to derive a class. After the class name, we declare an extended keyword followed by another class name.
Code:
class base class
{…. methods
}
class derived class name extends base class
{
methods … along with this additional feature
}
Java uses the keyword ‘extends’ to make a new class derived from the existing class. We call the inherited class a base class or superclass, and we call the newly created class a derived or subclass. The base class provides data members and methods, whereas the child class inherits those methods.
How Single Inheritance Works in Java?
Single inheritance specifies child-parent class relationships when they extend and the simplest type of all the methods, such as pear and apple inheriting from the fruits. In Inheritance mechanisms, objects are treated in a top-down manner. Previously we learned about syntax and its declaration. It is necessary to read the concept of access specifiers, namely private, public, and protected. The class can only access all data members once it declares private. The public can be accessed any class. The protection is done with the same package; that, too, is applicable through inheritance only.
Code:
class fruits
{private int f;
int g;
private void mmm ()
{
System.out.println(“….”);
}
}
class M extends fruits
{void method ();
………
}}
class Main
{
public static void main (String [] args)
{
M ob= new M ();
Ob.f=3; // here the variable cannot be accessed
Ob.mmm();
}
Explanation of the above code: In the above sample code, the statement ob.=3, the child class cannot access private members of a base class, making it impossible to assign them. Therefore, it throws an error that cannot find a symbol (compile-time error). To work with it is necessary to make the data members of the parent class has public.
Using Protected
In the below example, we have declared protected in the superclass, which the subclass can directly access.
Code:
class pgm
{
protected int I,k;
method ( int m,int n)
{
…
}
class R extends pgm
{ private int f;
// methods
}
public class protected Main
{
public static void main()
{
// methods and objects access
}
The flow diagram for Single Inheritance is given below:
Class Y inherits Class X, extending only a single class.
Examples to Implement Single Inheritance in Java
This section shall see the implementation of single inheritance where child class refers to parent properties and behaviors using extended keywords.
Example #1
Calculating the salary of an employee using Single Inheritance with the object class.
Code:
class Employee
{
float sal=60000;
}
class Main extends Employee
{
float b=1500;
float temp= sal + b;
public static void main(String args[])
{
Main ob=new Main();
System.out.println("Salary amount is:"+ob.sal);
System.out.println(" Extra Bonous is:"+ob.temp);
}
}
Output:
Example #2
Implementation of calculator using sum, subtraction, and divide multiplication methods.
Code:
class Calc{
int sum(int i , int j)
{
return i+j;
}
int subract(int i , int j)
{
return i-j;
}
}
public class Main extends Calc {
int mul(int xx , int yy)
{
return xx*yy;
}
int divide(int xx , int yy)
{
return xx/yy;
}
public static void main(String args[]) {
Main c= new Main();
System.out.println(c.sum(2,2));
System.out.println(c.subract(2,6));
System.out.println(c.mul(8,9));
System.out.println(c.divide(2,2));
}
}
Output:
Example #3
Calculating the Rectangle and triangle area using Single Inheritance.
Code:
class Rectdemo
{
int le,be;
void Sval(int a,int b)
{
le=a;
be=b;
}
int GetR()
{
return le*be;
}
}
class Tri extends Rectdemo
{
int b,h;
float t;
void Sdata(int q,int r)
{
b=r;
h=q;
}
float GetT()
{
t=(float)le/2*b*h;
return (t);
}
}
class Main
{
public static void main(String args[])
{
Tri Tr=new Tri();
Tr.Sval(40,8);
Tr.Sdata(10,6);
System.out.println("Area of Rectangle is calculated as :" +Tr.GetR());
System.out.println("Area of Triangle is calculated As :"+Tr.GetT());
}
}
Output:
Example #4
Using Super Keyword in Single Inheritance. The Super Keyword refers to the parent class of an object and acts as its constructor.
Code:
class Library
{
String lname;
public Library(String m)
{
lname = m;
}
}
public class Main extends Library {
String lname;
public Main(String x1, String x2)
{
super(x1); //passing argument to parent class constructor
this.lname = x2;
}
public void display()
{
System.out.println(super.lname+" and "+lname);
}
public static void main(String[] args)
{
Main c = new Main("library name","library id");
c.display();
}
}
Output:
Example #5
Over Ridden method called by subclass using inheritance.
Code:
class even {
void display()
{
System.out.println(" Even Nos,4 2");
}
}
class odd extends even {
void display()
{
super.display();
System.out.println(" Odd Nos ,1 3");
}
}
class Main {
public static void main(String[] args)
{
even e = new odd();
e.display();
}
}
Output:
Conclusion
Thus, getting to the end, this article guides on various inheritance concepts and how to work with single inheritance available in java. You will also get to know the working implementation using extend keyword. I hope this article is quite informative and adds knowledge to beginners.
Recommended Articles
This is a guide to Single Inheritance in Java. Here we discuss an introduction, how Single Inheritance in Java works, and examples of implementing single inheritance. You can also go through our other related articles to learn more –