CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152:...

44
CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013

Transcript of CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152:...

Page 1: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

CS-152: Inheritance,interfaces, andabstract classes

Neal Holtschulte

July 16, 2013

Page 2: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Many objects in a program will need similar

instance variables and methods.

Page 3: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Let’s talk about a video game example.

Page 4: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

General Object

It is common to create a general object with

such shared instance variables from which

many other objects can inherit.

Page 5: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

General Object

The general object may have instance

variables such as:

I x

I y

I direction

I speed

I hitPoints

Page 6: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

General Object

This general object may have methods such

as:

I move - move in the current direction at

the current speed

I turn - change direction

I isAlive - return true or false based on

whether or not object is alive

I takeDamage - takes an int argument and

subtracts that amount from hit points

Page 7: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

General Object

Lots of objects inherit from this generic

object in the video game including:

I the player

I enemies

I vehicles

I the camera

Page 8: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Strange bug

A strange bug was discovered.

Explosions near the player cause the camera

to stop moving.

Page 9: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Strange bug

What is going on?

The move command and turn command

check to see if the object isAlive before

executing.

Camera inherited from the generic object and

only had the default hit points.

Page 10: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Strange bug

What is going on?

The move command and turn command

check to see if the object isAlive before

executing.

Camera inherited from the generic object and

only had the default hit points.

Page 11: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Strange bug

Occasionally an explosion near the player

killed the camera without killing the player.

The camera stopped responding to move and

turn commands.

The solution was to make the camera

unkillable.

Page 12: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

To inherit or not to inherit

Sometimes it makes sense to inherit methods

and variables, but these particular game

designers might have gone too far.

Page 13: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Asteroids

What sort of inheritance might you use in a

game like Asteroids?

Page 14: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Source

Most of the following is from

Java An Introduction to Programming and

Problem Solving

by Savitch

Starting on page 557

Page 15: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Inheritance

Inheritance lets you define a general class,

then define more specialized classes that add

details to the general class.

These specialized classes “inherit” the

methods and instance variables of the

general class.

Page 16: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects
Page 17: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Parent / Super / Base class

pub l i c c l a s s Person{

p r i v a t e String name;

pub l i c Person(String name){

t h i s .name = name;

5 }

pub l i c String getName (){

r e tu rn name;

}

10 }

Page 18: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Child / Sub / Derived class

pub l i c c l a s s Student extends Person{

p r i v a t e i n t grade;

pub l i c Student(String name , i n t grade ){

super (name);5 t h i s .grade = grade;

}

pub l i c i n t getGrade (){

r e tu rn grade;

10 }

pub l i c vo id setGrade( i n t grade ){

t h i s .grade = grade;

}

15 }

Page 19: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Child / Sub / Derived class

//Create a new student.Student charlie = new Student("Charlie", 0);

//We can call methods of Student’s parent class5 //on objects of type Student.

System.out.println(charlie.getName ());

//We can also call methods of the Object as we//normally would.

10 charlie.setGrade (100);

Page 20: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Is-a versus Has-a

Use inheritance only to model “is-a”

relationships.

“has-a” relationships imply instance

variables.

Page 21: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Is-a versus Has-a

A student is-a person.

A student has-a grade.

Page 22: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Is-a relationships

Page 23: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

New Java syntax

ClassName extends SuperClassName

pub l i c c l a s s Student extends Person{

p r i v a t e i n t grade;

....

Page 24: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

New Java syntax

The constructor should call super() which

calls the super class (aka parent class)

constructor with any arguments that the

parent class’s constructor takes.

pub l i c Student(String name){

super (name);}

Page 25: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Overriding

You can override a parent class method in a

child class by creating a method of the same

name with the same number and type of

parameters.

Page 26: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Overriding

If the following was in the Student class, it

would override the Person class’s method of

the same name.

pub l i c String getName (){

r e tu rn "Fred";

}

Page 27: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Reminder of Parent class

pub l i c c l a s s Person{

p r i v a t e String name;

pub l i c Person(String name){

t h i s .name = name;

5 }

pub l i c String getName (){

r e tu rn name;

}

10 }

Page 28: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Reminder of Child class

pub l i c c l a s s Student extends Person{

p r i v a t e i n t grade;

pub l i c Student(String name , i n t grade ){

super (name);5 t h i s .grade = grade;

}

pub l i c i n t getGrade (){

r e tu rn grade;

10 }

pub l i c vo id setGrade( i n t grade ){

t h i s .grade = grade;

}

15 }

Page 29: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Private parent variables

A child class cannot directly access its parent

class’s private variables or methods.

The Student class cannot have the following

method:pub l i c vo id printName (){

System.out.println(name);

}

Page 30: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Private parent variables

The Student class could have the following

method:

pub l i c vo id printName (){

System.out.println(getName ());

}

Page 31: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Question

What is the difference between overriding a

method and overloading a method?

Page 32: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Suppose class SportsCar is derived from class

Automobile.

Automobile has private instance variables

speed, manufacturer, and number of

cylinders.

Will an object of type SportsCar have

instance variables speed, manufacturer, and

number of cylinders?

Page 33: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Inheritance and constructors

Child classes do not inherit constructors.

Parent or super class constructors must be

called with super();.

If you omit super(); this will not break the

code!

The default parent class constructor will be

called automatically.

Page 34: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Polymorphism is the ability to create a

variable, a function, or an object that has

more than one form.

Source: http://en.wikipedia.org/wiki/Polymorphism_in_

object-oriented_programming

Page 35: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Polymorphism in biology is when two or more

clearly different phenotypes exist in the same

population of a species.

Source: http://simple.wikipedia.org/wiki/Polymorphism

Page 36: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Polymorphism is the ability of one type, A, to

appear as and be used like another type, B.

Polymorphism usually means that type A

somehow derives from type B, or type C

implements an interface that represents type

B.

Source:

http://www.princeton.edu/~achaney/tmve/wiki100k/docs/

Polymorphism_in_object-oriented_programming.html

Page 37: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

The primary usage of polymorphism in

industry (object-oriented programming

theory) is the ability of objects belonging to

different types to respond to method, field,

or property calls of the same name, each one

according to an appropriate type-specific

behavior.

Source:

http://www.princeton.edu/~achaney/tmve/wiki100k/docs/

Polymorphism_in_object-oriented_programming.html

Page 38: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

The different objects involved only need to

present a compatible interface to the clients’

(calling routines).

Source:

http://www.princeton.edu/~achaney/tmve/wiki100k/docs/

Polymorphism_in_object-oriented_programming.html

Page 39: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Polymorphism allows us to treat

RandomCritters and NealsCritters as simply

Critters.

See Critter Source Code in World.java,

specifically this line:Critter animal = grid[r][c]. getOccupant ();

Page 40: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Page 41: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Polymorphism

Quadrilateral methods can be called on

parallelograms, rhombuses, rectangles, and

squares.

Parallelogram methods can be called on

rhombuses, rectangles, and squares.

etc

Page 42: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Interfaces and Abstract Classes

When you want to write a contract without

writing any code.

Abstract classes are partially implemented

interfaces.

An interface is pure contract. No code.

Source: http://stackoverflow.com/questions/1913098/

what-is-the-difference-between-an-interface-and-abstract-class

Page 43: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Interface

pub l i c i n t e r f a c e Automobile{

p r i v a t e i n t speed;

pub l i c vo id drive ();

5

pub l i c i n t getSpeed ();

pub l i c vo id accelerate(double acceleration );

}

Page 44: CS-152: Inheritance, interfaces, and abstract classesneal.holts/cs152summer2013/...CS-152: Inheritance, interfaces, and abstract classes Neal Holtschulte July 16, 2013 Many objects

Abstract class

pub l i c ab s t r a c t c l a s s Automobile{

p r i v a t e i n t speed = 0;

ab s t r a c t vo id drive ();

5

pub l i c i n t getSpeed (){

r e tu rn speed;

}

10 ab s t r a c t vo id accelerate(double acceleration );

}