0% found this document useful (0 votes)
54 views

Java Week - 8 Pratical

The document discusses inheritance concepts in Java including single, multilevel, hierarchical, and hybrid inheritance. It provides code examples for each type of inheritance that create classes like Teacher, PhysicsTeacher, Car, Maruti, Maruti800 to demonstrate how properties and methods can be inherited and accessed. The document also discusses how to design a class that follows the open closed principle using interfaces, inheritance, and polymorphism.

Uploaded by

Sheshgiri Sheshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Java Week - 8 Pratical

The document discusses inheritance concepts in Java including single, multilevel, hierarchical, and hybrid inheritance. It provides code examples for each type of inheritance that create classes like Teacher, PhysicsTeacher, Car, Maruti, Maruti800 to demonstrate how properties and methods can be inherited and accessed. The document also discusses how to design a class that follows the open closed principle using interfaces, inheritance, and polymorphism.

Uploaded by

Sheshgiri Sheshu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Week – 8

Code, execute and debug programs that uses inheritance


concept
Program for Inheritance
class Teacher {
//fields of parent class
String designation = "Teacher";
String collegeName = "Beginnersbook";

//method of parent class


void does(){
System.out.println("Teaching");
}
}

public class PhysicsTeacher extends Teacher{


//field of child class
String mainSubject = "Physics";
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
//accessing the fields of parent class
System.out.println(obj.collegeName);
System.out.println(obj.designation);

System.out.println(obj.mainSubject);

//accessing the method of parent class


obj.does();
}
}

Output:

Beginnersbook
Teacher
Physics
Teaching

In the above program we have a base class Teacher and a sub class PhysicsTeacher. Child


class inherits the following fields and methods from parent class:

 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.

1) program for Single Inheritance


Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}

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
}
}

2) program for Multilevel Inheritance


class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{

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

3) Program for Hierarchical 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 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

4) program for Hybrid Inheritance

Using single and multiple inheritance

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:

Implementation of Hybrid Inheritance in Java


Implementation of show() method defined in interfaces Male and Female
Method defined inside Child class

Using Multilevel and Hierarchical Inheritance

*/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.

Design a class & implement java program and check


compliance with OCP .
/*File name = run.java*/
Interface shape {
Public double calculateArea();
}
Class rectangle implements shape {
Double length;
Double width;
Public double calculateArea() {
Return length * width;
}
}
Class circle implements shape {
Public double radius;
Public double calculateArea() {
Return (22.0/7)* radius * radius;
}
}
Class AreaCalculator {
Public Double CalculatorShapeArea(Shape shape)
Return shape.calculateArea();
}
}
Public class run {
Public static void main (string args[]) {
Rectangle r = new Rectangle();
r.length = 10.5;
r.width = 20.0
AreaCalculator a = new AreaCalculator();
System.out.println(a.calculateShapeArea(r));
Circle c = new Circle();
c.radius = 1.0
system.out.println(a.calculateShapeArea(c));
}
}

Output:

210.0
3.142857142857143

You might also like