Inheritance C++

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Single Inheritance

class Bike
{
public int wheels;
public void wheel(int w)
{
wheels=w;
}
}
class Type extends Bike
{
void show()
{
System.out.println("No of Wheels:- "+wheels);
}
}
class singleInheritance
{
public static void main(String args[])
{
Type t1=new Type();
t1.wheel(4);
t1.show();
}
Multilevel Inheritance:
class animal
{
public void leg(int legs)
{
System.out.println("\nNo of legs:- "+legs);
}
}
class dog extends animal
{
public void breed(String type)
{
System.out.println("\nBreed Type:- "+type);
}
}
class colour extends dog
{
public void color(String color)
{
System.out.println("\nColor :- "+color);
}
}
class Multilevel {
public static void main(String args[])
{
colour c1=new colour();
c1.leg(4);
c1.breed("Labrador");
c1.color("Black");
}
}
Heirarchical Inheritance:
class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class heirarchical
{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}

You might also like