Java 2013.03.06.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Object Oriented Programming
Chapter 1 Inheritance University Of Ha’il.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Inheritance and Polymorphism
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
Object Oriented Programming
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
1 Chapter 4 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
1 Chapter 2 Inheritance and Polymorphism. 2 Objectives u To develop a subclass from a superclass through inheritance. u To invoke the superclass’s constructors.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Lecture 4 – Case Study TCP1201: 2017/2018.
Chapter 9 Inheritance and Polymorphism
Interface.
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Inheritance and Polymorphism
Interfaces.
Chapter 11 Inheritance and Polymorphism
Java Inheritance.
Inheritance.
Chapter 11 Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
CIS 110: Introduction to computer programming
Inheritance and Polymorphism
Presentation transcript:

Java 2013.03.06

Outline 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

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

Superclasses and Subclasses Novice HP MP attack() inheritance inheritance Magician Swordman magic_attack() power_attack()

程式範例: Novice.java /* Superclass */ public class Novice //Novice類別 { //---state, attribute----- int HP = 100; int MP = 50; //---constructor---------- Novice() {} //---behavior------ void attack() //代表attack行為的method System.out.println("Attack!"); }

程式範例: Magician.java /* 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!");

程式範例: Swordman.java /* 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!");

程式範例: Game.java /* 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(); }

Using super keyword 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.

Overiding Method Novice HP MP attack() inheritance Archer attack() overriding

程式範例: Archer.java /* 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!");

Overiding vs. Overloading 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)

Dynamic Binding Dynamic Binding 動態載入 物件的行為並不是在編譯時期 (compiler-time)就已經 決定了。而是在程式執行時期於(run-time)才動態地決定 的。如何動態地決定。就看物件當時的狀態(state)而定, 物件封裝了所有可能的狀態處理 方法,並且根據外邊送 來的訊息做出適當的反應。 (課本 p.409)

程式範例: DynamicBindingDemo.java 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 return "Person";

Polymorphism Polymorphism 多型  簡單來說,多型是指一個物件可以擁有多個不同的型 態,而這些型態必須是該物件所繼承的其他類別。

生物 動物 人 Organism O = new Organism(); People P = new People(); Organism O2 = new People(); 生物 extends 動物 extends 人

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

ArrayList ArrayList class that can be used to store an unlimited number of objects. Java.util.ArrayList 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

程式範例: TestArrayList.java /* ArrayList */ class Car { int speed() return 100; } void run() System.out.println("Runing!"); public class TestArrayList @SuppressWarnings("unchecked") 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());

practice 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 email 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 person’s name.

Ppt下載: http://oss.csie.fju.edu.tw/~jastine01/ppt.html