Home > Java > javaTutorial > body text

Single Inheritance in Java

WBOY
Release: 2024-08-30 15:25:43
Original
1142 people have browsed it

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.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

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
}
Copy after login

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();
}
Copy after login

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
}
Copy after login

The flow diagram for Single Inheritance is given below:

Single Inheritance in Java

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.

Note: I have used Main as a class name in all these examples. You need to save the file name as Main.java while executing.

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);
}
}
Copy after login

Output:

Single Inheritance in Java

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));
}
}
Copy after login

Output:

Single Inheritance in Java

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());
}
}
Copy after login

Output:

Single Inheritance in Java

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();
}
}
Copy after login

Output:

Single Inheritance in Java

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();
}
}
Copy after login

Output:

Single Inheritance in Java

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.

The above is the detailed content of Single Inheritance in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!