tailieunhanh - Lecture An introduction to object-oriented programming with Java: Chapter 13 - C. Thomas Wu
Chapter 13 - Inheritance and polymorphism. After you have read and studied this chapter, you should be able to: Write programs that are easily extensible and modifiable by applying polymorphism in program design; define reusable classes based on inheritance and abstract classes and abstract methods; differentiate the abstract classes and Java interfaces; define methods, using the protected modifier; parse strings, using a String Tokenizer object. | ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Chapter 13 Inheritance and Polymorphism Animated Version Introduction to OOP with Java 4th Ed, C. Thomas Wu © The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Chapter 13 Objectives After you have read and studied this chapter, you should be able to Write programs that are easily extensible and modifiable by applying polymorphism in program design. Define reusable classes based on inheritance and abstract classes and abstract methods. Differentiate the abstract classes and Java interfaces. Define methods, using the protected modifier. Parse strings, using a String Tokenizer object. Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Simple Example We can effectively model similar, but different types of objects using inheritance Suppose we want to model dogs and cats. They are different types of pets. We can define the Pet class and the Dog and Cat classes as the subclasses of the Pet class. Pet Dog Cat Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - The Pet Class class Pet { private String name; public String getName() { return name; } public void setName(String petName) { name = petName; } public String speak( ) { return “I’m your cuddly little pet.”; } } Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Subclasses of The Pet Class class Cat extends Pet { public String speak( ) { return “Don’t give me orders.\n” + “I speak only when I want to.”; } } class Dog extends Pet { public String fetch( ) { return “Yes, master. . | ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Chapter 13 Inheritance and Polymorphism Animated Version Introduction to OOP with Java 4th Ed, C. Thomas Wu © The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Chapter 13 Objectives After you have read and studied this chapter, you should be able to Write programs that are easily extensible and modifiable by applying polymorphism in program design. Define reusable classes based on inheritance and abstract classes and abstract methods. Differentiate the abstract classes and Java interfaces. Define methods, using the protected modifier. Parse strings, using a String Tokenizer object. Intro to OOP with Java, C. Thomas Wu ©The McGraw-Hill Companies, Inc. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 - Simple Example We can effectively model similar, but
đang nạp các trang xem trước