Bunny class

public class Bunny extends Animal{

//class properties/attributes

//constructor method

public Bunny(String name){

//send the parameter to be managed by the super class

super(name);

}

//accessor methods

//mutator methods

}

Bird class

public class Bird extends Animal{

//class properties/attributes

//constructor method

public Bunny(String name){

//send the parameter to be managed by the super class

super(name);

}

//accessor methods

//mutator methods

}

Frog class

public class Frog extends Animal{

//class properties/attributes

//constructor method

public Frog(String name){

//send the parameter to be managed by the super class

super(name);

}

//accessor methods

//mutator methods

}

Crocodile class

public class Crocodile extends Animal{

//class properties/attributes

//constructor method

public Crocodile(String name){

//send the parameter to be managed by the super class

super(name);

}

//accessor methods

//mutator methods

}

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 int getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

Bird class (a subclass of Animal)

public class Bird extends Animal{

//class properties not shown

//constructor method not shown

//accessor methods not shown

//mutator methods not shown

}

Bird class

public class Bird extends Animal{

//class properties/attributes

//constructor method

public Bird(String name){

//to be updated

}

//accessor methods

//mutator methods

}

Bird class

public class Bird extends Animal{

//class properties/attributes

//constructor method

public Bird(String name){

super(name);

}

//accessor methods

//mutator methods

}

Hare class

public class Hare extends Animal{

//additional property only for Hares

private int carrotCount;

//constructor method

public Hare(String name){

super(name);

this.carrotCount = 0;

}

//accessor methods

public int getCarrotCount(){

return this.carrotCount;

}

//mutator methods

public void setCarrotCount(){

this.carrotCount = this.carrotCount + 1;

}

}

Objectives

Students will be able to:

  • understand the basic concept of a subclass having its own properties and methods.

The Farmer Class

public class Farmer{

//first declare the class properties

String name;

int score;

boolean bankrupt;

//then design the constructor method-usually this is used to assign initial data to the object when it is constructed

public Farmer(String name, int score, boolean bankrupt){

//assign parameters to class properties.

this.name = name;

this.score = score;

this.bankrupt = bankrupt;

}

}

Object Oriented Programming

Intro to Inheritance II

In the last lesson we were introduced to Inheritance which is a strategy we can use when classes display an IS-A relationship.

These designs are possible because each class inherits properties and methods from a superclass, .

A Hare IS-A Animal

 

A Hare is also an Animal.

It also has a name and a score property, and accessor and mutator methods as all other Animals have!

But, in one (or more) way, a Hare is different.

A Hare eats carrots and it can remember how many it has eaten!

This is reasonable. We cannot expect all Animals to be exactly the same.

So, given that a Hare IS-A Animal, how can we incorporate this difference into our design when we are utilising inheritance?

Worksheet task

Complete the worksheet.

Inheritance Pros and Cons

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

Printables

Tags

idesyntax file class instantiate constructor type propertymethod