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

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

Crocodile class

public class Crocodile{

//class properties/attributes

private String name;

private int score;

//constructor method

public Crocodile(String name){

this.name = name;

this.score = 0;

}

//accessor methods

public int getName(){

return this.name;

}

public int getScore(){

return this.score;

}

//mutator methods

public void setScore(int points){

this.score = this.score+points;

}

}

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

public class Animal{

//class properties/attributes

private int 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

}

Objectives

Students will be able to:

  • understand the basic concept of inheritance and the IS-A relationship
  • define a superclass
  • define subclasses using the extends keyword
  • draw a UML diagram describing IS-A between classes

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

A computer program is going to process animal objects. There are 4 kinds of animals (see above).

Click on the class definitions of each animal. What do you notice?

Intro Activity

The information below is intended to support your understanding of the basics of Inheritance.

However, it is recommended that you complete intro task first which will get you coding quickly!

IS-A relationship

We can see that every class is very similar. In fact, the only difference is the name of the class and the name of the constructor method.

Each class has the same 2 properties.

This is because each class represents an animal and animals in this scenario are very similar.

They have the same properties/attributes and the same methods/actions.

This is because they are all animals!

A Bunny IS-A Animal.

A Bird IS-A Animal.

A Frog IS-A Animal.

A Crocodile IS-A Animal.

Inheritance

If we can identify an IS-A relationship between classes, then we should consider using the OOP concept of inheritance.

A super class will define what every Animal is by defining the common properties and methods of all Animals.

Subclasses eg Bird, Bunny etc will need to create an IS-A relationship with the super class. In Java, the keyword extends is used to do this.

Any properties and methods that are already defined in the superclass can be removed from the subclass(es)!

You can see that the Bird class currently has no properties! Actually, they are defined in the superclass.

Also, there are no accessor/mutator methods!! These are defined in the superclass too!

There is still a constructor method because in another class we still need to be able to do this:

Bird b = new Bird("Cecil");

But how can we assign the name "Cecil" to the property name which is defined in the superclass?

We simply pass this to the super constructor when we construct a Bird;

As well as the keyword extends, java uses the keyword super when we need to refer to the superclass, and super() refers to the super constructor!

Inheritance Intro

This video will help to introduce the above OOP concepts

Video Review

The video showed us:

  1. How to define a superclass
  2. How to create a relationship between a subclass and a superclass
  3. How to construct a subclass object, passing any necessary parameters from the subclass constructor to the superclass constructor

Concept check!

When 2 or more classes display an IS-A relationship, we can use INHERITANCE.

This involves creating a superclass.

All subclasses can inherit properties and methods from this superclass.

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

Tags

idesyntax file class instantiate constructor type propertymethod