0% found this document useful (0 votes)
2 views2 pages

Experiment6 Inheritance Solution

The document contains code examples demonstrating inheritance in Java through various classes such as Animal, Dog, Plants, Trees, and Member. It illustrates how subclasses can inherit properties and methods from their parent classes, with specific outputs shown for each example. Additionally, it includes details of Employee and Manager classes, showcasing their attributes and salary printing functionality.

Uploaded by

chahmadahmad463
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Experiment6 Inheritance Solution

The document contains code examples demonstrating inheritance in Java through various classes such as Animal, Dog, Plants, Trees, and Member. It illustrates how subclasses can inherit properties and methods from their parent classes, with specific outputs shown for each example. Additionally, it includes details of Employee and Manager classes, showcasing their attributes and salary printing functionality.

Uploaded by

chahmadahmad463
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name:Muhammad Ahmad

Roll no:028
Experiment 6- Inheritence

Q9: Code and Output


Explanation: Dog inherits properties of Animal, so it can use eat() method.

class Animal
{ void eat()
{
[Link]("Eating...");
}
}

class Dog extends Animal


{ void bark() {
[Link]("Barking...");
}
}

public class Test {


public static void main(String[] args)
{ Dog d = new Dog();
[Link]();
[Link]();
}
}

Output:
Eating
Barking

Q11: Plants, Trees, Shrubs, Fruit Trees


class Plants
{ String
name; void
grow() {
[Link]("Plants grow");
}
}

class Trees extends Plants


{ int height;
}

class Shrubs extends Plants {


int width;
}

class FruitTrees extends Trees


{ String fruitType;
}

public class TestPlants {


public static void main(String[] args)
{ FruitTrees ft = new FruitTrees();
[Link] = "Mango Tree";
[Link] = 15;
[Link] = "Mango";

[Link]("Name: " + [Link]);


[Link]("Height: " + [Link]);
[Link]("Fruit: " + [Link]);
[Link]();
}
}
Output:
Name: Mango Tree
Height: 15
Fruit: Mango
Plants grow

Q12: Member, Employee, Manager


class Member
{ String
name; int
age; String
phone;
String address;
double salary;

void printSalary()
{ [Link]("Salary: " + salary);
}
}

class Employee extends Member


{ String specialization;
}

class Manager extends Member


{ String department;
}

public class TestMember {


public static void main(String[] args)
{ Employee e = new Employee();
[Link] = "Ali";
[Link] = 25;
[Link] = "12345";
[Link] = "Rawalpindi";
[Link] = 50000;
[Link] = "IT";

Manager m = new Manager();


[Link] = "Ahmed";
[Link] = 35;
[Link] = "67890";
[Link] = "Islamabad";
[Link] = 80000;
[Link] = "HR";

[Link]("Employee Details:");
[Link]([Link] + " " + [Link] + " " + [Link] + " " + [Link]);
[Link]();

[Link]("\nManager Details:");
[Link]([Link] + " " + [Link] + " " + [Link] + " " + [Link]);
[Link]();
}
}

Output:
Employee Details:
Ali 25 12345 Rawalpindi
Salary: 50000

Manager Details:
Ahmed 35 67890 Islamabad
Salary: 80000

You might also like