Docsity

Java Inheritance: Understanding the 'extends' Keyword and Its Benefits for Reusability, Study notes of Computer Science

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
Inheritance
CIS110 1
Note: What is Jar?
filename.jar
Stands Java archive
Contains collection of precompiled java
files (.class files) and any media files
(images, sound) involved in the program
CIS110 2
Example: Bot and BetterBot
public class Bot{
private int x;
private int y;
public int getX() { .. }
public int getY() { .. }
public void eatDot(){ ..}
public void move(){.. }
public void turnLeft(){.. }
}
public class BetterBot extends Bot{
public void turnRight(){
turnLeft();
turnLeft();
turnLeft();
}
}CIS110 3
Inheritance
One of the key concepts of OOP
A hierarchical relationship among classes
Establishes a superclass/subclass relationship
Establishes “is a” relationships
¾e.g. a BetterBot “is a” Bot
Benefits:
Reusability of code
¾Put code in one class, use it in all the subclasses
¾Revisions only needs to be done in 1 place
Polymorphic code (works on “many forms”)
¾Write general purpose code designed for a supertype that
works for all subtypes
pf3
pf4
pf5

Partial preview of the text

Download Java Inheritance: Understanding the 'extends' Keyword and Its Benefits for Reusability and more Study notes Computer Science in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners^ Inheritance

CIS

1

Note: What is Jar?

filename.jar

Stands Java archive

Contains collection of precompiled javafiles (.class files) and any media files(images, sound) involved in the program

CIS

2

Example: Bot and BetterBot

public class Bot{

private int x;private int y;public int getX() { .. }public int getY() { .. }public void eatDot(){ ..}public void move(){.. }public void turnLeft(){.. }

}

public class BetterBot extends Bot{

public void turnRight(){

turnLeft();turnLeft();turnLeft();

}

}

CIS

3

Inheritance

One of the key concepts of OOP

„

A hierarchical relationship among classes

„

Establishes a superclass/subclass relationship

„

Establishes “is a” relationships

¾

e.g. a BetterBot “is a” Bot

Benefits:

„

Reusability of code

¾

Put code in one class, use it in all the subclasses

¾

Revisions only needs to be done in 1 place

„

Polymorphic code (works on “many forms”)

¾

Write general purpose code designed for a supertype thatworks for all subtypes

CIS

4

The “extends” keyword

public class Bot{} public class BetterBot extends Bot{}

Inheritance is established via the “extends” keyword

Now we say

„

BetterBot

inherits

from Bot

¾

However, based on the visibility modifiers, certain instancevariables and methods defined in Bot may not be accessible

„

A BetterBot “is a” Bot

„

BetterBot is a

subclass

subtype

of Bot

„

Bot is the

superclass/supertype

of BetterBot

CIS

5

What can you inherit?

Visibility modifiers determine which classmembers are accessible and which do not

Members (variables and methods) declaredwith

public

visibility are accessible, and those

with

private

visibility are not

Problem: How to make class instance variablesvisible only to its subclasses?

„

Solution: Java provides a third visibility modifier thathelps in inheritance situations:

protected

CIS

6

Constructors and Inheritance

BetterBot b = new BetterBot(); „

When an object is created, its constructor is called

„

But first, a constructor from its highest ancestor (Object) is called,then the next highest (Bot), then its own (BetterBot)

„

The default behavior is such that the default (no-argumentconstructor) is executed

„

A constructor can explicitly call its parent’s (its superclass’)

constructor by making a call to super()

public class BetterBot extends Bot{

public BetterBot (BotWorld world){

super(world);

CIS

7

Another Example with super

public class

Dog{

private String name;private int age;

Dog(String name, int age){

this.name = name;this.age = age;

} class

BetterDog

extends Dog{

public BetterDog(String name, int age){

super(name,age);

CIS

12

Need for equals(): Comparison of Strings

„

If the == operator is used for Strings

„

Java compares the addresses where the String objects arestored, not the letters in the String

„

For example:

> String a = "hi";> String b = "hi";> a == b> false

„

Use the String class'

equals

method to compare two

Strings for equality

> a.equals(b)

> true> b.equalsIgnoreCase(“HI”)> true

CIS

13

The equals() Method

public class Person {

private int social;

// social security #

private String name;

} public int getSocial() { return social; }public boolean equals(Person p) {

return this.social == p.getSocial();

„

By default, compares heap addresses

„

By convention, it is

overriden to

match the developer’s notion of

equality

CIS

14

Type Rules

„

A reference variable of type t may hold a value of its own type orany subtype (but not of a supertype).

„

Given the following variable declaration:

b = new Bot();b = new String();b = new BetterBot();b = new Object();

Bot b;

Which of the following assignments are valid (compile)?How about these?

BetterBot bb;bb = new Bot();bb = new String();bb = new BetterBot();bb = new Object();

CIS

15

The “instanceof” Operator

> Bot b = new Bot();> b instanceof Bottrue> b instanceof Objecttrue> b instanceof BetterBotfalse> BetterBot bb = new BetterBot();> bb instanceof BetterBottrue> bb instanceof Objecttrue> bb instanceof Bottrue> Bot bbb = new BetterBot();

//a variable can store a subtype

> bbb instanceof BetterBot()true> bbb instanceof Bottrue

CIS

16

Inheritance Example

public class Vehicle{

protected int registrationNumber;

public int getRegNumber(){

return registrationNumber; } public void setRegNumber(int num){

registrationNumber = num; } }

public class Car extends Vehicle { //new instance variable, not inheritedprivate int numberOfDoors;public Car(int doors){

numberOfDoors = doors; } public int getDoors() {

return numberOfDoors; } }

CIS

17

Interactions I

Car c = new Car(2);> c.getDoors() 2 > c.getRegNumber() //Inherited Method 0 > c.setRegNumber(45) //Inherited Method> c.getRegNumber() 45

CIS

18

Interactions II

public class TwoWheelerextends Vehicle{public void setRegNumber(intnum){

registrationNumber = 5000

+ num;

> TwoWheeler t = newTwoWheeler();> t.getRegNumber()//Inherited Method 0 > t.setRegNumber(45);//Inherited Method &Overriden> t.getRegNumber() 5045

CIS

19

Interactions III

Vehicle v = new Car(4); //valid why?> v.setRegNumber(3)> v.getRegNumber() 3 v.getDoors()Compile Error: No getDoors() in Vehicle „

Since reference type is of Vehicle and not Carthis makes sense

In other words “v” could potentially refer to othertypes of vehicles that are not cars

The solution here is to use

down type-casting

(conversion).