Principles of Computer Programming (using Java) Chapter 10 Subclasses Haidong Xue Summer 2011, at GSU
Content Inheritance Overriding Polymorphism The “Object” class
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
Inheritance Car Data: speed, weight, gas amount, size of tires Actions: show speed, show weight, accelerate, brake, add gas, show tire size
Inheritance Bike Data: speed, weight, size of tires, size of pedals Actions: show speed, show weight, accelerate, brake, show tire size, show pedal size
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
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
TestInheritance.java Vehicle.java Car.java Bike.java Inheritance
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
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.
The punch game InstructorsInCSC2310.java
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().
The bonus assignment Worth: 10 points in the final grade Develop a program to play Texas Holdem Poker