Question

*Code in Java* Write a Java program called to demonstrate Inheritance and Polymorphism Create a base...

*Code in Java*

Write a Java program called to demonstrate Inheritance and Polymorphism

  1. Create a base class called Person, that has fields, constructors, and anything else you think you may need as you go along. It should also have a method called speak(), to make the person say their name (using System.out.println)
  2. Create any two subclasses of the Person class. Each subclass must have the following:
    • At least one new field, with getter/setter methods
    • A default constructor that sets its fields to default values, and also all the fields of it's base class to default values
    • A parameterized constructor that accepts all fields from both the base (Person) and new subclass fields
    • A speak method, that says something specific that an object of that class would say. (make it up)
  3. In a main method, create a Person object, and one object of each subclass (2 objects), and stick all 3 objects in either an ArrayList or an array. Then just loop through (iterate) the ArrayList or array, and call the speak method for each object  
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class Person

{

    private String name;

    private int age;

   

    // constructor

    Person()

    {

        this.name = "";

        this.age = 0;

    }

   

    // constructor

    Person(String name, int age)

    {

        this.name = name;

        this.age = age;

    }

   

    // setter method

    public void setName(String name)

    {

        this.name = name;

    }

   

    public void setAge(int age)

    {

        this.age = age;

    }

   

    // getter method

    public String getName()

    {

        return this.name;

    }

   

    public int getAge()

    {

        return this.age;

    }

   

    // to make the person say their name (using System.out.println)

    public void speak()

    {

        System.out.println("My name is " + this.getName());

    }

}

class Dog extends Person

{

    private String type;

    private String voice;

   

    Dog()

    {

        // call the constructor of parent class

        super();

       

        this.type = "Dog";

        this.voice = "";

    }

   

    Dog(String name, int age, String voice)

    {

        // call the constructor of parent class

        super(name, age);

       

        this.type = "Dog";

        this.voice = voice;

    }

   

    // setter method

    public void setVoice(String voice)

    {

        this.voice = voice;

    }

   

    // getter method

    public String getVoice()

    {

        return this.voice;

    }

}

class Cat extends Person

{

    private String type;

    private String voice;

   

    Cat()

    {

        // call the constructor of parent class

        super();

       

        this.type = "Cat";

        this.voice = "";

    }

   

    Cat(String name, int age, String voice)

    {

        // call the constructor of parent class

        super(name, age);

        

        this.type = "Cat";

        this.voice = voice;

    }

   

    // setter method

    public void setVoice(String voice)

    {

        this.voice = voice;

    }

   

    // getter method

    public String getVoice()

    {

        return this.voice;

    }

}

public class Test

{

    public static void main(String[] args)

    {

        ArrayList<Person> arr = new ArrayList<Person>();

       

        arr.add( new Person("Chandler Bing", 35) );

        arr.add( new Dog("Rocky", 11, "Woof") );

        arr.add( new Cat("Tinker", 12, "Meow") );

       

        // traverse the ArrayList

        for( Person ob : arr )

            ob.speak();

    }

}

Sample Output

Add a comment
Know the answer?
Add Answer to:
*Code in Java* Write a Java program called to demonstrate Inheritance and Polymorphism Create a base...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT