Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
More about classes and objects Classes in Visual Basic.NET.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Inheritance using Java
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 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.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Object-Oriented Design
Sections Inheritance and Abstract Classes
OOP: Encapsulation &Abstraction
Inheritance and Polymorphism
One class is an extension of another.
Object Oriented Programming
One class is an extension of another.
Inheritance Basics Programming with Inheritance
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Computer Programming with JAVA
Java – Inheritance.
CIS 199 Final Review.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance

Chapter 7 - Generalization/Specialization and Inheritance2 Chapter 7 Topics Creating generalization/specialization hierarchies using the extends keyword to implement inheritance with problem domain classes Invoking superclass constructors when writing a subclass constructor Using the abstract and final keywords with Java superclasses Overriding superclass methods in subclasses How polymorphism works with Java subclasses The implications of protected and private access in superclasses

Chapter 7 - Generalization/Specialization and Inheritance3 Implementing the Boat Generalization/Specialization Hierarchy Boat Class (See Figure 7-2) –Includes: 4 attributes Constructor  invokes 4 accessor (setter) methods 8 standard accessor methods –4 setters –4 getters

Chapter 7 - Generalization/Specialization and Inheritance4

5 Implementing the Boat Generalization/Specialization Hierarchy Testing the Boat Superclass –TesterOne tester program (Figure 7-3) Creates three boat instances Retrieves information about each instance –Sequence diagram (Figure 7-5) Shows how TesterOne interacts with the Boat class –Boat is ready to use as a superclass

Chapter 7 - Generalization/Specialization and Inheritance6

7 Implementing the Boat Generalization/Specialization Hierarchy Using the Extends Keyword to Create the Sailboat Subclass –Generalization/specialization hierarchy General superclass includes attributes and methods that are shared by specialized subclasses –Instances of subclass inherit the attributes and methods of the superclass –Include additional attributes and methods defined by subclass

Chapter 7 - Generalization/Specialization and Inheritance8 Implementing the Boat Generalization/Specialization Hierarchy Using the Extends Keyword to Create the Sailboat Subclass –Keywords: Use extends keyword to implement inheritance –public class Sailboat extends Boat Use super keyword to invoke superclass constructor –super(aStateRegNo, aLength, aManufacturer, aYear);

Chapter 7 - Generalization/Specialization and Inheritance9

10 Implementing the Boat Generalization/Specialization Hierarchy Using the Extends Keyword to Create the Sailboat Subclass –To use inheritance: Need to know constructor and method signatures (APIs) Do not need: –Access to source code –Knowledge of how class is written

Chapter 7 - Generalization/Specialization and Inheritance11 Implementing the Boat Generalization/Specialization Hierarchy Testing the Sailboat Subclass –TesterTwoA tester program (Figure 7-8) Creates 3 Sailboat instances and populates them Can use any methods defined in super or sub class –Sequence diagram (Figure 7-10) Shows how TesterTwoA interacts with the Sailboat class

Chapter 7 - Generalization/Specialization and Inheritance12

Chapter 7 - Generalization/Specialization and Inheritance13 Implementing the Boat Generalization/Specialization Hierarchy Adding a Second Subclass – Powerboat –Powerboat Class (Figure 7-11) Second class can be extended from Boat without effecting any other classes derived from Boat –TesterTwoB tester program (Figure 7-12) Creates instances of both subclass and populates them –Sequence diagram (Figure 7-14) Shows how TesterTwoB interacts with the Sailboat and Powerboat classes

Chapter 7 - Generalization/Specialization and Inheritance14

Chapter 7 - Generalization/Specialization and Inheritance15 Understanding Abstract and Final Classes Using Abstract Classes –Concrete class Class that can be instantiated –Abstract class Class not intended to be instantiated Used only to extend into subclasses Facilitates reuse Keyword: –abstract »Used in class header to declare an abstract class »e.g., public abstract class Boat

Chapter 7 - Generalization/Specialization and Inheritance16 Understanding Abstract and Final Classes Using Final Classes –Final class Class not intended to be extended Improves performance  JVM does not have to search for subclass methods that might override superclass methods Implements security by restricting class extension Keyword: –final »Used in class header to declare a final class »e.g. public final class Boat

Chapter 7 - Generalization/Specialization and Inheritance17 Overriding a Superclass Method Method Overriding –Occurs when method in subclass is invoked instead of method in superclass Both methods have the same signature –Allows subclass to modify the behavior of the superclass Compare overriding with overloading

Chapter 7 - Generalization/Specialization and Inheritance18 Overriding a Superclass Method Adding & Overriding tellAboutSelf Method –Use same tellAboutSelf method signature in subclass –Implement new functionality in overridden method definition –Can invoke methods from super or sub class –Compare tellAboutSelf() in Figures 7-15, 7-16

Chapter 7 - Generalization/Specialization and Inheritance19 Overriding a Superclass Method Overriding & Invoking a Superclass Method –Two basic ways to override a superclass method Override completely – Saleboat 7-16 –Redefine what the method does Override and append – Powerboat 7-19 –Override method –Call super to execute superclass method –Add additional functionality to overridden method

Chapter 7 - Generalization/Specialization and Inheritance20 Overriding a Superclass Method Testing Two Method-Overriding Approaches –TesterThreeB tester program (Figure 7-20) Creates instances of both subclass, populates them, and demonstrates method overriding –Sequence diagram Shows how TesterThreeB interacts with the Sailboat and Powerboat classes

Chapter 7 - Generalization/Specialization and Inheritance21

Chapter 7 - Generalization/Specialization and Inheritance22 Overriding a Superclass Method Overriding, Polymorphism, and Dynamic Binding –Polymorphism Objects of different classes can respond to the same message in their own way Simplifies processing: –Responsibility of responding appropriately is left to the particular instance –Method overriding Form of polymorphism

Chapter 7 - Generalization/Specialization and Inheritance23 Overriding a Superclass Method Overriding, Polymorphism, and Dynamic Binding –Dynamic Binding Resolves which method to invoke at runtime if overridden methods exist Provides flexibility when adding new subclasses to a system

Chapter 7 - Generalization/Specialization and Inheritance24 Understanding Private Versus Protected Access Private access –No other object can directly read or modify the value of an attribute Must use accessor methods –Also limits ability of subclass to directly access private variables of superclass Must use accessor methods

Chapter 7 - Generalization/Specialization and Inheritance25 Understanding Private Versus Protected Access Protected access –Subclass has direct access to protected variables of superclass – pp. 236 Other classes in same package have direct access as well –Can also use with methods –Use with care… –Avoid use of public keyword with attributes Violates encapsulation and information hiding