Implementation of Single Inheritance using Java
Single inheritance is a feature found in some
object-oriented programming languages where a class can inherit properties and
behaviors from a single base class. In single inheritance, a derived class
(also known as a subclass or child class) can inherit the characteristics of a
single parent class (also known as a base class or superclass).
When a class inherits from another class using single
inheritance, it gains access to all the public and protected members (methods
and variables) of the base class. It can use these inherited members directly
as if they were defined within the derived class itself. This allows for code
reuse and promotes a hierarchical structure in object-oriented programming.
Program:-
class Sinherit
{
public static void main(String args[])
{
student s1=new
student("gopi",22,"IT","eswar","devi","markapur");
s1.display();
}
}
class parent
{
String fname;
String mname;
String city;
}
class student extends parent
{
String sname;
int sid;
String branch;
student(String sname,int sid,String
branch,String fname,String mname,String city)
{
this.sname=sname;
this.sid=sid;
this.branch=branch;
this.fname=fname;
this.mname=mname;
this.city=city;
}
void display()
{
System.out.println("student name "+sname);
System.out.println("student id "+sid);
System.out.println("student branch
"+branch);
System.out.println("father name "+fname);
System.out.println("mothe name "+mname);
System.out.println("city name "+city);
}
}
Output:-
0 Comments