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

Experiment6 Inheritance Solution

The document contains examples of inheritance in Java, demonstrating how subclasses can inherit properties and methods from their parent classes. It includes code snippets for classes like Animal and Dog, Plants and its subclasses, and Member, Employee, and Manager, along with their respective outputs. Each example illustrates the use of inheritance and method overriding in object-oriented programming.

Uploaded by

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

Experiment6 Inheritance Solution

The document contains examples of inheritance in Java, demonstrating how subclasses can inherit properties and methods from their parent classes. It includes code snippets for classes like Animal and Dog, Plants and its subclasses, and Member, Employee, and Manager, along with their respective outputs. Each example illustrates the use of inheritance and method overriding in object-oriented programming.

Uploaded by

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

Experiment 6 – Inheritance (Solved)

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