11.23. Code Practice with Object Oriented Concepts

Write a method that overloads the talk method by taking in a name and printing “Hello” with that name.

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.

Show Comments

Create an interface named Test that has a void talk method and void walk method. Check your answer with the answer tab instead of running it!

We declare an interface similarly to how we declare a class, first put its access modifier (public, private, etc) then what it is, which in this case would be interface. Since it is an interface, it is important to remember that the methods cannot have a body. They will be public and abstract methods even if you don’t use those keywords when you declare the methods.

Show Comments

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”

In order to specify the parent class, use the extends keyword in the class header of the child class.

Show Comments

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.

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.

Show Comments

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

To override a method in a child class, you must have the same return types and parameters as the parent class’s method

Show Comments

Overload the greet method to just print “Hello” if not given any parameters. It should print “Hello” and then “Hello Sansa”.

To overload a method, you use the same name as the method but change the parameters or return type.

Show Comments

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

In order to use a method that has been overwritten in a subclass, you can use super.methodName().

Show Comments

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

Use super(parm1,parm2) to call the parent’s constructor. This is especially useful to initialize inherited fields.

Show Comments

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.

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.

Show Comments

Correctly finish the Dog subclass for the following abstract Animal class. Override the abstract methods. It should print “woof” and then “num num”.

For something to be a proper subclass of an abstract class, the subclass must include non-abstract versions of the methods from the abstract class.

Show Comments

Override the compareTo method so that it returns a postive 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.

By overriding the compareTo method you are able to compare objects based on specified factors.

Show Comments

Override the Person class’s speak function inside the Student class. Make the function print “I’m a student”.

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 function you must have the same method header and parameters!

Show Comments
Next Section - 11.24. Mixed Up Code Practice