Inheritance in Java with Example
Inheritance in Java with Example
Inheritance
It has five types
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hybrid inheritance
5. Hierachical inheritance
Single inheritance
import java.io.*;
class arun
{
public string name;
public int age;
void getdata() throws IOException
{
DataInputStream in = new DataInputStream(System.in);
{
System.out.println("Enter the name and age is:");
name=in.readLine();
age=Integer.parseInt(in.readLine());
}
}
void putdata()
{
System.out.println("The given name is:"+name);
System.out.println("The given age is:"+age);
}
}
class kumar extends arun
{
public int mark1,mark2;
void getmark() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putmark()
{
System.out.println("The given mark1 is:"+mark1);
System.out.println("The given mark2 is:"+mark2);
}
}
class single
{
public Static void main(String args[]) throws IOException
{
kumar k=new kumar();
k.getdata();
k.putdata();
k.getmark();
k.putmark();
}
}
Multilevel inheritance
import java.io.*;
class arun
{
public string name;
public int age;
void getdata() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the name and age is:");
name=in.readLine();
age=Integer.parseInt(in.readLine());
}
}
void putdata()
{
System.out.println("The given name is:"+name);
System.out.println("The given age is:"+age);
}
}
class kumar extends arun
{
public int mark1,mark2;
void getmark() throws IOException
{
DataInputStream in=new DataInputStream(System.in);
{
System.out.println("Enter the mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putmark()
{
System.out.println("The given mark1 is:"+mark1);
System.out.println("The given mark2 is:"+mark2);
}
}
class selva extends kumar
{
public int total,average;
void getresult()
{
total=mark1+mark2;
average=total/2;
System.out.println("The given total is:"+total);
System.out.println("The given average is:"+average);
}
}
class single
{
public Static void main(String args[]) throws IOExeception
{
selva k=new selva();
k.getdata();
k.putdata();
k.getmark();
k.putmark();
k.getresult();
}
}
Multiple inheritance
import java.io.*;
interface arun
{
void getdata() throws IOException;
}
interface kumar
void getdata1() throws IOException;
}
class selva implements arun,kumar
{
public string name;
public int age;
public void getdata() throws IOException
{
DataInputStream in=new DataInputStream(System.in);
{
System.out.println("Enter the name and age :");
name=in.readLine();
age=Integer.parseInt(in.readLine());
}
}
void putdata()
{
System.out.println("The given name is:"+name);
System.out.println("The given age is:"+age);
}
public int mark1,mark2;
public void getdata1() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putmark()
{
System.out.println("The given mark1 is:"+mark1);
System.out.println("The given mark2 is:"+mark2);
}
public int total,average;
void getresult()
{
total=mark1+mark2;
average=total/2;
System.out.println("The given total is:"+total);
System.out.println("The given average is:"+average);
}
}
class single
{
public Static void main(String args[]) throws IOException
{
selva k=new selva();
k.getdata();
k.putdata();
k.getmark();
k.putmark();
k.getresult();
}
}
Hybrid inheritance
import java.io.*;
class selva
{
public string name;
public int age;
public void getdata() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the name and age is:");
name=in.readLine();
age=Integer.parseInt(in.readLine());
}
}
void putdata()
{
System.out.prinltn("The given name is:"+name);
System.out.println("The given age is :"+age);
}
}
class kumar extends selva
{
public int mark1,mark2;
public void getdata1() throws IOException
{
DataInputStream in =newDataInputStream(System.in);
{
System.out.println("Enter the mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
void putmark()
{
System.out.println("The given mark1 is:"+mark1);
System.out.prinltn("The given mark2 is:"+mark2);
}
}
interface arun
{
void getsport() throws IOException;
}
class ajith extends kumar implements arun
{
public int total,average,sport;
public void getsport() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter sport mark is:");
sport=Integer.parseInt(in.readLine());
System.out.prinltn("The given sport mark is:"+sport);
}
}
void getresult()
{
total=mark1+mark2+sport;
average=total/3;
System.out.println("The given total is:"+total);
System.out.println("The given average is:"+average);
}
}
class hybrid
{
public Static void main(String args[]) throws IOException
{
ajith a=new ajith();
a.getdata();
a.getdata1();
a.putdata();
a.putmark();
a.getsport();
a.getresult();
}
}
Hierachical inheritance
import java.io.*;
class college
{
public string name;
public int age;
void getdata() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the name and age is:");
name=in.readLine();
age=Integer.parseInt(in.readLine());
}
}
void putdata()
{
System.out.println("The given name is:"+name);
System.out.println("The given age is:"+age);
}
}
class arts extends college
{
public int mark1,mark2;
void getmark() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter the arts mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putamark()
{
System.out.println("The given arts mark1 is:"+mark1);
System.out.println("The given arts mark2 is:"+mark2);
}
}
class science extends college
{
public int mark1,mark2;
void getsmark() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter science mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putsmark()
{
System.out.println("The given science mark1 is:"+mark1);
System.out.println("The given science mark2 is:"+mark2);
}
}
class commerce extends college
{
public int mark1,mark2;
void getcmark() throws IOException
{
DataInputStream in =new DataInputStream(System.in);
{
System.out.println("Enter commerce mark1 and mark2 is:");
mark1=Integer.parseInt(in.readLine());
mark2=Integer.parseInt(in.readLine());
}
}
void putcmark()
{
System.out.println("The given commerce mark1:"+mark1);
System.out.println("the given commerce mark2 is:"+mark2);
}
}
class sans
{
public Static void main(String args[]) throws IOException
{
arts a=new arts();
a.getdata();
a.putdata();
a.getamark();
a.putamark();
science s=new science();
s.getsmark();
s.putsmark();
commerce c =new commerce();
c.getcmark();
c.putcmark();
}
}
0 coment�rios:
Post a Comment