Inheritance:
i) Single Inheritance:
class base //Base class
{
void disp1()
{ cout<<"Base class"; }
};
class deri:: public base //Derived class
{
void disp2()
{ cout<<"Derived class"; }
};
void main()
{
deri obj;
obj.disp1();
obj.disp2();
}
ii)Multiple Inheritance:
class base1 //Base class
{
void disp1()
{ cout<<"Base class 1"; }
};
class base2 //Base class 2
{
void disp2()
{ cout<<"Base class 2"; }
};
class deri::public base1,public base2 // Derived class
{
void disp2()
{ cout<<" Deri class"; }
};
void main()
{
deri o;
o.disp1();
o.disp2();
o.disp3();
}
iii) Multi-level Inheritance:
class base //Base class
{
void disp1()
{ cout<<"Base class"; }
};
class deri::public base //Derived class
{
void disp2()
{ cout<<"Derived class"; }
};
class dderi::public deri //Derived of derived class
{
void disp3()
{ cout<<"Derived of derived class"; }
};
void main()
{
dderi o;
o.disp1();
o.disp2();
o.disp3();
}
iv) Hierarchical Inheritance::
class base //Base class
{
void disp1()
{ cout<<"Base class"; }
};
class deri1:public base //Derived class1
{
void disp2()
{ cout<<"Derived class 1"; }
};
class deri2::public base //Derived class2
{
void disp2()
{ cout<<"Derived class 2"; }
};
void main()
{
deri1 o1; //Object declaration of derived class 1
o1.disp1();
o1.disp2();
deri2 o2; //Object declaration of derived class 2
o2.disp1();
o2.disp3();
}
v) Hybrid Inheritance:
class base1 //Base class 1
{
void disp1()
{ cout<<"Base class 1"; }
};
class deri1::public base //Derived class 1
{
void disp2()
{ cout<<"Derived class"; }
};
class base2 //Base class 2
{
void disp3()
{ cout<<"Base class 2"; }
};
class deri::public deri1,public base2 //Derived class 2
{
void disp4()
{ cout<<"Main derived class"; }
};
void main()
{
deri o;
o.disp1();
o.disp2();
o.disp3();
o.disp4();
}
**************************************************
Virtual Base class:
#include<iostream.h>
class stud //Base class
{ int id;
char name[50];
void getdata1()
{ cout<<"ENter name,id : ";
cin>>name>>id;
}
void disp1()
{ cout<<"Name: "<<name<<"Id: "<<id; }
};
class test::public stud //Derived class 1
{
int m1,m2,m3;
void getdata2()
{ cout<<"ENter m1,m2,m3: ";
cin>>m1>>m2>>m3;
}
void disp2()
{ cout<<"Mark1:"<<m1<<"Mark2:"<<m2<<"Mark3:"<<m3; }
};
class sport::public stud //Derived class 2
{ int score;
char game[50];
void getdata3()
{ cout<<"ENter game and score :";
cin>>game>>score;
}
void disp3()
{ cout<<"Game:"<<game<<"Score:"<<score; }
};
class result::public test, public sport //Main derived class
{
void disp()
{ cout<<"THE END"; }
};
void main()
{
result o;
o.getdata1();
o.getdata2();
o.getdata3();
o.disp1();
o.disp2();
o.disp3();
o.disp();
}
Interface:
********
interface printable //Interface declaration 1
{ void print(); }
interface showable //Interface declaration 2
{ void show(); }
class a implements printable,showable
{
public void print()
{ System.out.println("Hello"); }
public void show()
{ System.out.println("World"); }
}
class base
{
public static void main(String args[])
{
a obj=new a();
obj.print();
obj.show();
}
}
Inheritance in JAVA:
************
i)Single
class base //Base class
{
void disp1()
{ System.out.println("Base class"); }
};
class deri extends base //Derived class
{
void disp2()
{ System.out.println("Derived class"); }
};
class oops
{
public static void main(String args[])
{
deri obj=new deri();
obj.disp1();
obj.disp2();
}}
ii)Multiple:
*********
class base1 //Base class1
{
void disp1()
{ System.out.println("Base class 1"); }
};
class base2 //Base class2
{
void disp2()
{ System.out.println("Base class 2"); }
};
class deri extends base1,base2 //Derived class
{
void disp2()
{ System.out.println(" Deri class"); }
};
class oops
{
public static void main(String args[])
{
deri o=new deri();
o.disp1();
o.disp2();
o.disp3();
}}
iii)Hierarchiacal:
***************
class base //Base class
{
void disp1()
{ System.out.println("Base class"); }
};
class deri1 extends base //Derived class1
{
void disp2()
{ System.out.println("Derived class 1"); }
};
class deri2 extends base //Derived class2
{
void disp2()
{ System.out.println("Derived class 2"); }
};
class base
{
public static void main(String args[])
{
deri1 o1=new deri1();
o1.disp1();
o1.disp2();
deri2 o2=new deri2();
o2.disp1();
o2.disp3();
}
}
Newsletter
Subscribe to our newsletter: