Bunny class

public class Bunny{

//class properties/attributes

private String name;

private int score;

//constructor method

public Bunny(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public String getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

Bird class

public class Bird{

//class properties/attributes

private String name;

private int score;

//constructor method

public Bird(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public String getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

Frog class

public class Frog{

//class properties/attributes

private String name;

private int score;

//constructor method

public Frog(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public String getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

Crocodile class

public class Crocodile extends Animal{

//class properties/attributes

private int numTeeth;

//constructor method

public Crocodile(String name, int numTeeth){

super(name)

this.numTeeth = numTeeth;

}

//accessor methods

public int getNumTeeth(){

return this.numTeeth;

}

//override the superclass's speak method.

public void speak(){

System.out.println("I am a wily croc!");

}

public String toString(){

return super.toString() + "; Num Teeth: "+ this.numTeeth;

}

}

Animal class (describes any class that IS-A Animal)

public class Animal{

//class properties/attributes

private String name;

private int score;

//constructor method

public Animal(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public String getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

public String toString(){

return "Name: "+name+"; Score: "+score;

}

}

Animal class (describes any class that IS-A Animal)

public class Animal{

//class properties/attributes

private String name;

private int score;

//constructor method

public Animal(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public String getName(){

return this.name;

}

public String getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

public void speak(){

System.out.println("I am an Animal!");

}

}

Method signatures

Method signatures define/describe:

  • the name/identifier of the method
  • the parameters of the method (identifier and data type)
  • the return type of the method eg void or int etc

Objectives

Students will be able to:

  • understand the basic concept of method overriding
  • understand how to use the super keyword to call superclass methods when method overriding is used

Object Oriented Programming

Method overriding

There are 5 characters in a computer game.

A Bunny IS-A Animal.

A Bird IS-A Animal.

A Frog IS-A Animal.

A Crocodile IS-A Animal.

A Hare IS-A Animal.

Scenario

The game designers have decided that all Animals can speak. One of the advantages of Inheritance is that only the superclass needs to be modified.

The new Animal superclass design is .

And so, in a Main program we can now do this:

Bunny bunny = new Bunny("Bugs");

bunny.speak();

Method overriding

So all Animals speak.

But unfortunately they don't all say the same thing when they speak!

For example, when a Crocodile speaks, it says I am a wily Crocodile!

How can we handle this issue in OOP?

Open your IDE and complete the Worksheet.

Concept check!

Method overriding is different from Method overloading.

Method overloading means that within one class, more than one method with the same name can exist as long as the parameter list is the same.

Method overriding means that a subclass can override a sperclass's method. The name of the method and the parameter list MUST be the same!

Exercises

Click the exercises button and see if you can complete the challenges.

Glossary

object

property

method

create an object/instance of a class

instantiate an object

encapsulate

access

mutate

abstraction

Tags

methodsyntax overriding overloading instantiate instance super parameterattribute