Java Week - 8 Pratical
Java Week - 8 Pratical
System.out.println(obj.mainSubject);
Output:
Beginnersbook
Teacher
Physics
Teaching
Properties: designation and collegeName properties
Method: does()
Child classes like MathTeacher, MusicTeacher and PhysicsTeacher do not need to write this
code and can access these properties and method directly from base class.
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output:
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
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 JavaExample
{
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();
}
}
Output:
method of Class A
method of Class A
method of Class A
In the following Java program, we have achieved the hybrid inheritance by implementing the
combination of single and multiple inheritance (through interfaces).
In this program, we have taken the example of Human body that performs different
functionalities like eating, walking, talking, dancing etc. Human body may be either Male or
Female. So, Male and Female are the two interfaces of the HumanBody class. Both the child
class inherits the functionalities of the HumanBody class that represents
the single Inheritance.
Suppose, Male and Female may have child. So, the Child class also inherits the
functionalities of the Male, Female interfaces. It represents the multiple inheritance.
/*filename*/
Child.java
class HumanBody
{
public void displayHuman()
{
System.out.println("Method defined inside HumanBody class");
}
}
interface Male
{
public void show();
}
interface Female
{
public void show();
}
public class Child extends HumanBody implements Male, Female
{
public void show()
{
System.out.println("Implementation of show() method defined in interfaces Male and Female");
}
public void displayChild()
{
System.out.println("Method defined inside Child class");
}
public static void main(String args[])
{
Child obj = new Child();
System.out.println("Implementation of Hybrid Inheritance in Java");
obj.show();
obj.displayChild();
}
}
Output:
*/file name*/
Dauhter.java
//parent class
class GrandFather
{
public void showG()
{
System.out.println("He is grandfather.");
}
}
//inherits GrandFather properties
class Father extends GrandFather
{
public void showF()
{
System.out.println("He is father.");
}
}
//inherits Father properties
class Son extends Father
{
public void showS()
{
System.out.println("He is son.");
}
}
//inherits Father properties
public class Daughter extends Father
{
public void showD()
{
System.out.println("She is daughter.");
}
public static void main(String args[])
{
//Daughter obj = new Daughter();
//obj.show();
Son obj = new Son();
obj.showS(); // Accessing Son class method
obj.showF(); // Accessing Father class method
obj.showG(); // Accessing GrandFather class method
Daughter obj2 = new Daughter();
obj2.showD(); // Accessing Daughter class method
obj2.showF(); // Accessing Father class method
obj2.showG(); // Accessing GrandFather class method
}
}
GrandFather is a super class. The Father class inherits the properties of the GrandFather
class. Since Father and GrandFather represents single inheritance. Further, the Father class is
inherited by the Son and Daughter class. Thus, the Father becomes the parent class for Son
and Daughter. These classes represent the hierarchical inheritance. Combinedly, it denotes
the hybrid inheritance.
Output:
210.0
3.142857142857143