Download - Principles of Computer Programming (using Java) Chapter 10 Subclasses

Transcript
Page 1: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Principles of Computer Programming(using Java)

Chapter 10 Subclasses

Haidong XueSummer 2011, at GSU

Page 2: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Content• Inheritance• Overriding• Polymorphism• The “Object” class

Page 3: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Inheritance

• What is inheritance?• What are the advantages of inheritance in

Java?• How to make a class in Java using inheritance?• E.g.:

– TestInheritance.java, Vehicle.java, Car.java, Bike.java

Page 4: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Inheritance

Car

Data: speed, weight, gas amount, size of tires

Actions: show speed, show weight, accelerate, brake, add gas, show tire size

Page 5: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Inheritance

Bike

Data: speed, weight, size of tires, size of pedals

Actions: show speed, show weight, accelerate, brake, show tire size, show pedal size

Page 6: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Inheritance

Car

Data: speed, weight, gas amount, size of tires

Actions: show speed, show weight, accelerate, brake, add gas, show tire size

Bike

Data: speed, weight, size of tires, size of pedals

Actions: show speed, show weight, accelerate, brake, show tire size, show pedal size

Page 7: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Inheritance

Car Bike

Vehicle

Data: speed, weight, size of tires

Actions: show speed, show weight, accelerate, brake, show tire size

Data: gas amount

Actions: add gas

Data: size of pedals

Actions: show pedal size

extends

Page 8: Principles of Computer Programming (using Java) Chapter 10 Subclasses

• TestInheritance.java• Vehicle.java• Car.java• Bike.java

Inheritance

Page 9: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Overriding

• If a method in the subclass has the same signature of a method in the superclass, the method in the superclass is hided and overridden.

• E.g.:– TestOverriding.java– Vehicle.java, Car.java, Bike.java

Page 10: Principles of Computer Programming (using Java) Chapter 10 Subclasses

Polymorphism

• An ability of a programming language;• an instance of a subclass can take the place

of an instance of any of its superclasses;• if an object variable of the superclass points to

a subclass object, when calling an overridden method, the method of the subclass will be called.

Page 11: Principles of Computer Programming (using Java) Chapter 10 Subclasses

The punch game

• InstructorsInCSC2310.java

Page 12: Principles of Computer Programming (using Java) Chapter 10 Subclasses

The Object class

• What is Object class?• A rule: all the classes have to have a superclass.• The Object class is the default superclass.• What are the advantages?

– One type for all;– Some common method for all the objects, like toString().

Page 13: Principles of Computer Programming (using Java) Chapter 10 Subclasses

The bonus assignment

• Worth: 10 points in the final grade• Develop a program to play Texas Holdem

Poker