Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles of Computer Programming (using Java) Chapter 10 Subclasses Haidong Xue Summer 2011, at GSU.

Similar presentations


Presentation on theme: "Principles of Computer Programming (using Java) Chapter 10 Subclasses Haidong Xue Summer 2011, at GSU."— Presentation transcript:

1 Principles of Computer Programming (using Java) Chapter 10 Subclasses Haidong Xue Summer 2011, at GSU

2 Content Inheritance Overriding Polymorphism The “Object” class

3 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

4 Inheritance Car Data: speed, weight, gas amount, size of tires Actions: show speed, show weight, accelerate, brake, add gas, show tire size

5 Inheritance Bike Data: speed, weight, size of tires, size of pedals Actions: show speed, show weight, accelerate, brake, show tire size, show pedal size

6 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

7 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

8 TestInheritance.java Vehicle.java Car.java Bike.java Inheritance

9 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

10 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.

11 The punch game InstructorsInCSC2310.java

12 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().

13 The bonus assignment Worth: 10 points in the final grade Develop a program to play Texas Holdem Poker


Download ppt "Principles of Computer Programming (using Java) Chapter 10 Subclasses Haidong Xue Summer 2011, at GSU."

Similar presentations


Ads by Google