Introduction on 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 exists here. Basically, java only uses a single inheritance as a subclass cannot extend more superclass.
Inheritance is the basic properties of object-oriented programming. Inheritance tends to make use of the properties of a class object into another object. Java uses inheritance for the purpose of code-reusability to reduce time by then enhancing reliability and to achieve 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, hybrid. This article shall go through on basic understanding of the single inheritance concept briefly 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 make use of extending keyword to derive a class. An extend keyword is declared after the class name 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 that is derived from the existing class. The inherited class is termed as a base class or superclass, and the newly created class is called derived or subclass. The class which gives data members and methods known as the base class and the class which takes the methods is known as the child class.
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. To go with it is necessary to read the concept of access specifiers, namely private, public, protected. When private is declared, all the data members are accessed within the class. The public can be accessed inside any class. The protection is done with the same package; that too, it 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 to the above code: In the above sample code, the statement ob.=3; cannot be assigned as the private members of the base class cannot be accessed by the child class. 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, which means it extends 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 extend 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, 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. Super Keyword is very similar to this keyword and represents the near parent class of an object and also calls the 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 and also you will get to know the working implementation using extend keyword. I hope this article is quite informative and adds knowledge to the beginners.
Recommended Articles
This is a guide to Single Inheritance in Java. Here we discuss an introduction, how Single Inheritance in Java works, along with the examples to implement single inheritance. You can also go through our other related articles to learn more –