Skip to main content
Logo image

Section 5.11 Code Practice with Object Oriented Concepts

Activity 5.11.1.

Write a method that overloads the talk method by taking in a name and printing Hello with that name.
Solution.
Overloading is when several methods have the same name but different parameter types, order, or number. In this case, the original method had no parameters and we overloaded it by creating a talk method with a String parameter.
public class Test1
{
    public static void talk()
    {
        System.out.println("hello there!");
    }

    public static void talk(String name)
    {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args)
    {
        talk("Matthew");
    }
}

Activity 5.11.2.

Edit this code so the class Beagle is a subclass of the Dog class. When you run the code it should print β€œwoof!” and then β€œarf arf”.
Solution.
In order to specify the parent class, use the extends keyword in the class header of the child class.
public class Dog
{
    public void speak()
    {
        System.out.println("woof!");
    }

    public static void main(String[] args)
    {
        Dog d = new Dog();
        d.speak();
        Dog b = new Beagle();
        b.speak();
    }
}

class Beagle extends Dog
{
    public void speak()
    {
        System.out.println("arf arf");
    }
}

Activity 5.11.3.

Add an equals method to this class that returns true if the current Dog and passed Dog have the same name. The code should print false twice then true twice.
Solution.
In order to override the equals method, the method header has to have the same return type and parameters as the equals method for the Object class. The code should print false twice then true twice.
public class Dog
{
    private String name;

    public Dog(String name)
    {
        this.name = name;
    }

    public boolean equals(Object other)
    {
        Dog d1 = (Dog) other;
        return this.name.equals(d1.name);
    }

    public static void main(String[] args)
    {
        Dog d1 = new Dog("Rufus");
        Dog d2 = new Dog("Sally");
        Dog d3 = new Dog("Rufus");
        Dog d4 = d3;
        System.out.println(d1.equals(d2));
        System.out.println(d2.equals(d3));
        System.out.println(d1.equals(d3));
        System.out.println(d3.equals(d4));
    }
}

Activity 5.11.4.

Override the taste method from the Candy class in the Chocolate class to return tastes chocolately. It should print tastes sweet! and then tastes chocolately.
Solution.
To override a method in a child class, you must have the same return types and parameters as the parent class’s method.
public class Candy
{
    public String taste()
    {
        return "tastes sweet!";
    }

    public static void main(String[] args)
    {
        Candy c1 = new Candy();
        System.out.println(c1.taste());
        Candy c2 = new Chocolate();
        System.out.println(c2.taste());
    }
}

class Chocolate extends Candy
{
    public String taste()
    {
        return ("tastes chocolately");
    }
}

Activity 5.11.5.

Overload the greet method to just print Hello if not given any parameters. It should print Hello and then Hello Sansa.
Solution.
To overload a method, you use the same name as the method but change the parameters or return type.
public class Student
{

    public static void greet()
    {
        System.out.println("Hello");
    }

    public static void greet(String name)
    {
        System.out.println("Hello " + name);
    }

    public static void main(String[] args)
    {
        greet();
        greet("Sansa");
    }
}

Activity 5.11.6.

Add a call to Pet’s brag method before printing anything in Dog’s brag method (hint: use super to call an overridden method). It should print I have the best pet! and then I have the best dog.
Solution.
In order to use a method that has been overwritten in a subclass, you can use super.methodName().
public class Pet
{

    public void brag()
    {
        System.out.println("I have the best pet!");
    }

    public static void main(String[] args)
    {
        Dog d1 = new Dog();
        d1.brag();
    }
}

class Dog extends Pet
{
    public void brag()
    {
        super.brag();
        System.out.println("I have the best dog!");
    }
}

Activity 5.11.7.

Finish the Teacher constructor. Use super to use the Person construtor to set the fields inherited from Person. It should print Destini 20 followed by Erica 55 Masters in Teaching.
Solution.
Use super(parm1,parm2) to call the parent’s constructor. This is especially useful to initialize inherited fields.
public class Person
{
    private String name;
    private int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return this.name;
    }

    public int getAge()
    {
        return this.age;
    }

    public String toString()
    {
        return getName() + " " + getAge();
    }

    public static void main(String[] args)
    {
        Person p = new Person("Destini", 20);
        System.out.println(p);
        Teacher p2 = new Teacher("Erica", 55, "Masters in Teaching");
        System.out.println(p2);
    }
}

class Teacher extends Person
{
    String degree;

    public String getDegree()
    {
        return this.degree;
    }

    public String toString()
    {
        return getName() + " " + getAge() + " " + getDegree();
    }

    public Teacher(String name, int age, String theDegree)
    {
        super(name, age);
        this.degree = theDegree;
    }
}

Activity 5.11.8.

Add public getter and setter methods to the Store class so its variables can be accessed by other classes. It should print the store’s name and address and then change both and print the new values.
Solution.
A getter method is one that returns the value of a private variable and a setter method allows one to change the value of a private variable without having direct access to it.
public class Store
{
    private String name;
    private String address;

    public Store(String theName, String theAddress)
    {
        this.name = theName;
        this.address = theAddress;
    }

    public String getName()
    {
        return this.name;
    }

    public String getAddress()
    {
        return this.address;
    }

    public void setName(String theName)
    {
        this.name = theName;
    }

    public void setAddress(String theAddress)
    {
        this.address = theAddress;
    }

    public String toString()
    {
        return this.name + "\n" + this.address;
    }

    public static void main(String[] args)
    {
        Store myStore = new Store("Barb's Store", "333 Main St.");
        System.out.println(myStore);
        myStore.setName("Barbara's Store");
        myStore.setAddress("555 Pine St.");
        System.out.println(myStore);
    }
}

Activity 5.11.9.

Correctly finish the Dog subclass for the following Animal class. Override the methods speak() to print woof and eat() to print num num.
Solution.
class Animal
{
    public String name;
    public int numLegs;

    public void speak()
    {
        System.out.println("sniff");
    }

    public void eat()
    {
        System.out.println("crunch");
    }
}

public class Dog extends Animal
{
    public void speak()
    {
        System.out.println("woof");
    }

    public void eat()
    {
        System.out.println("num num");
    }

    public static void main(String[] args)
    {
        Dog myDog = new Dog();
        myDog.speak();
        myDog.eat();
    }
}

Activity 5.11.10.

Override the compareTo method so that it returns a positive number if the current Person is older than the passed other and a negative number if they are younger. If their age is the same then return the compareTo result on the names.
Solution.
By overriding the compareTo method you are able to compare objects based on specified factors.
public class Person implements Comparable<Person>
{
    private String name;
    private int age;

    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public int compareTo(Person other)
    {
        if (this.age != other.age)
        {
            return this.age - other.age;
        }
        else
        {
            return this.name.compareTo(other.name);
        }
    }

    public static void main(String[] args)
    {
        Person p1 = new Person("Carlos", 17);
        Person p2 = new Person("Lia", 18);
        Person p3 = new Person("Asraf", 17);
        Person p4 = new Person("Lia", 17);
        Person p5 = new Person("Karla", 17);
        System.out.println(p1.compareTo(p2));
        System.out.println(p2.compareTo(p3));
        System.out.println(p3.compareTo(p1));
        System.out.println(p4.compareTo(p3));
        System.out.println(p4.compareTo(p5));
    }
}

Activity 5.11.11.

Override the Person class’s speak method inside the Student class. Make the method print I'm a student.
Solution.
In the Student class we add a public void method called speak() and print β€œI’m a student” inside. It is important to remember that in order to override a method you must have the same method header and parameters!
public class Person
{
    public void speak()
    {
        System.out.println("I'm a person");
    }

    public static void main(String[] args)
    {
        Person p1 = new Student();
        p1.speak();
    }
}

class Student extends Person
{
    public void speak()
    {
        System.out.println("I'm a student");
    }
}
You have attempted of activities on this page.