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 extends Animal{

//class properties/attributes

private int birdSeeed;

//constructor method

public Bird(String name){

super(name);

this.birdSeed = 0;

}

//accessor methods

public String getBirdSeed(){

return this.birdSeed;

}

//mutator methods

public void setBirdSeed(){

this.birdSeed = this.birdSeed+1;

}

@override

public void makeSound(){

System.out.println("Tweet Tweet!");

}

}

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 String name;

//constructor method

public Animal(String name){

this.name = name;

}

//accessor methods

public String getName(){

return this.name;

}

//other methods

public void makeSound(){

System.out.println("A generic animal sound!");

}

}

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

Advantages

  • Code Reusability: Subclasses inherit existing code from base classes, reducing the need for redundant code and minimizing duplication.
  • Extensibility: Existing functionality can be extended or modified (overridden) in subclasses without changing the original base class, allowing for easier feature additions.
  • Maintenance and Organization: Changes made to a base class are automatically reflected in its derived classes, simplifying maintenance. It helps organize code into hierarchical structures, improving readability.
  • Polymorphism: Inheritance supports polymorphism, where a subclass object can be treated as an instance of its superclass, enabling more flexible and general code.
  • Reliability: Since base/super class code is already tested and debugged, reusing it in derived/sub classes increases overall reliability.
  • Data Hiding/Security: Base classes can control data access, providing a way to hide data while exposing functionality to derived classes.

Object Oriented Programming

Abstract Classes

 

 

Click on the button. We can see that a Bird IS-A Animal.

It exhibits similar properties and behaviour to Animals, but may override certain behaviour and may also have additional attributes.

Here is the Animal superclass:

The Super Class Conundrum

Inheritance is super cool! It has many .

In the above example we can now do this:

Animal tweety = new Bird("Tweety");

tweety.makeSound();

The question is: Do we ever want or need to create a generic Animal eg:

Animal bob = new Animal("Bob");

bob.makeSound();

What sound would Bob make?

Task 1

Complete the worksheet.

Concept check!

Think about these questions:

  1. When should a class be made abstract?
  2. When should a method be made abstract?
  3. Does a shared method need to be made abstract?
  4. Do other inheritance concepts still apply when using abstract classes eg an abstract class can still contain common attributes?

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