Presentation is loading. Please wait.

Presentation is loading. Please wait.

2013.03.06. Inheritance Superclasses and Subclasses Using super keyword Overiding Method Overiding vs. Overloading Dynamic Binding Polymorphism [Sample.

Similar presentations


Presentation on theme: "2013.03.06. Inheritance Superclasses and Subclasses Using super keyword Overiding Method Overiding vs. Overloading Dynamic Binding Polymorphism [Sample."— Presentation transcript:

1

2 Inheritance Superclasses and Subclasses Using super keyword Overiding Method Overiding vs. Overloading Dynamic Binding Polymorphism [Sample code] Novice.java Magician.java Swordman.java Game.java DynamicBindingDemo.java

3 Object-oriented programming allows you to derive new classes from existing classes. This is called inheritance.

4 HP MP magic_attack()power_attack() Novice MagicianSwordman attack() inheritance

5 /* Superclass */ public class Novice//Novice { //---state, attribute----- int HP = 100; int MP = 50; //---constructor Novice() {} //---behavior void attack()// attack method { System.out.println("Attack!"); }

6 /* subclass */ public class Magician extends Novice//Magician Person { //---state, attribute----- //---constructor Magician() { this.HP = 100; this.MP = 250; } //---behavior void magic_attack()// magic attack method { if(this.MP >=10) { this.MP -= 10; System.out.println("Magic Attack!"); }

7 /* Subclass */ public class Swordman extends Novice// Swordman { //---state, attribute----- //---constructor Swordman() { this.HP = 300; this.MP = 50; } //---behavior void power_attack()// attack method { System.out.println("Power Attack!"); }

8 /* Game */ public class Game { public static void main(String[] args) { // Declair a Novice object Novice player1 = new Novice(); System.out.println("player1 -> HP:" + player1.HP + " MP:" + player1.MP); player1.attack(); // Declair a Magician object Magician player2 = new Magician(); System.out.println("player2 -> HP:" + player2.HP + " MP:" + player2.MP); player2.attack(); player2.magic_attack(); // Declair a Swordman object Swordman player3 = new Swordman(); System.out.println("player3 -> HP:" + player3.HP + " MP:" + player3.MP); player3.attack(); player3.power_attack(); // Declair a Archer object Archer player4 = new Archer(); System.out.println("player3 -> HP:" + player4.HP + " MP:" + player4.MP); player4.attack(); }

9 The super refers to the superclass of the class in which super appears. It can be used in two ways: 1. To call a superclass constructor. 2. To call a superclass method.

10 HP MP Novice attack() Archer inheritance overriding

11 /* Subclass */ public class Archer extends Novice// Archer { //---state, attribute----- //---constructor Archer() { this.HP = 200; this.MP = 150; } //---behavior void attack()// attack method, override the Novice.attack() { System.out.println("Shoot!"); }

12 Overriding Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass. Overloading Define multiple methods with the same name but different signature. ( p.407)

13 Dynamic Binding (compiler-time) (run-time) (state) ( p.409)

14 public class DynamicBindingDemo { public static void main(String [] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); } public static void m(Object x) { System.out.println(x.toString()); } class GraduateStudent extends Student { } class Student extends Person { public String toString() { return "Student"; } class Person { public String toString() { return "Person"; }

15 Polymorphism

16 Organism O = new Organism(); People P = new People(); Organism O2 = new People(); extends

17 Polymorphism - Overriding - Overloading - Dynamic binding - Interface/abstract

18 ArrayList class that can be used to store an unlimited number of objects. add(o object): void add(index: int, o: object): void clear(): void indexOf(o: object): int isEmpty(): boolean lastIndexOf(o: object): int remove(o: obect): boolean size(): int Java.util.ArrayList

19 /* ArrayList */ class Car { int speed() { return 100; } void run() { System.out.println("Runing!"); } public class TestArrayList public static void main(String[] args) { java.util.ArrayList cityList = new java.util.ArrayList(); cityList.add("Taipei"); cityList.add("Taichung"); cityList.add("Kaushiung"); java.util.ArrayList list = new java.util.ArrayList(); list.add(new Car()); System.out.println(((Car)list.get(0)).speed()); //System.out.println(((Car)list.get(1)).run()); }

20 1. (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the persons name.

21 Ppt


Download ppt "2013.03.06. Inheritance Superclasses and Subclasses Using super keyword Overiding Method Overiding vs. Overloading Dynamic Binding Polymorphism [Sample."

Similar presentations


Ads by Google