130 likes | 283 Views
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?
E N D
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 Bike Data: speed, weight, gas amount, size of tires Actions: show speed, show weight, accelerate, brake, add gas, show tire size Data: speed, weight, size of tires, size of pedals Actions: show speed, show weight, accelerate, brake, show tire size, show pedal size
Inheritance Data: speed, weight, size of tires Actions: show speed, show weight, accelerate, brake, show tire size Vehicle extends Car Data: gas amount Actions: add gas Bike Data: size of pedals Actions: show pedal size
Inheritance • TestInheritance.java • Vehicle.java • Car.java • Bike.java
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 placeof 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