Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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)
ITEC200 – Week03 Inheritance and Class Hierarchies.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Object Oriented Programming & Software Engineering.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
© 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.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Inheritance using Java
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Inheritance in the Java programming language J. W. Rider.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Modern Programming Tools And Techniques-I
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance, Polymorphism, and Interfaces
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
Slides by Steve Armstrong LeTourneau University Longview, TX
Stack Implementations
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Chapter Contents Composition  Generic Types  Adapters Inheritance  Invoking Constructors from Within Constructors  Private Fields and Methods of The Base Class  Protected Access  Overriding, Overloading Methods  Multiple Inheritance Type Compatibility and Base Classes  The Class Object  Abstract Classes and Methods Polymorphism

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Composition 1 When a class has a data field that is an instance of another class Example – an object of type Student. Fig. 2-1 A Student object composed of other objects fig 2-1 A "has a" relationship Click to view source code

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Generic Types 3 Consider a class with fields that can be of any class type Use a generic type  Follow class name with an identifier enclosed in angle brackets public class MyClass View the OrderedPair class which takes ordered pairs of any typeView the OrderedPair class Note sample code using this generic class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Adapters 7 Use composition to write a new class  Has an instance of an existing class as a data field  Defines new methods needed for the new class Example – a NickName class adapted from class Name View source code of class NickNameView source code

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Inheritance 8 A general or base class is first defined Then a more specialized class is defined by …  Adding to details of the base class  Revising details of the more general class Advantages  Saves work  Common properties and behaviors are define only once for all classes involved

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Inheritance Fig. 2-2 A hierarchy of classes. An "is a" relationship

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Inheritance View source code of class CollegeStudentView source code Fig. 2-3 A hierarchy of student classes.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Invoking Constructors from Within Constructors 13 Constructors usually initialize data fields In a derived class  The constructor must call the base class constructor Note use of reserved word super as a name for the constructor of the base classNote use of  When super is used, it must be the first action in the derived constructor definition  Must not use the name of the constructor

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Private Fields, Methods of Base Class 15 Accessing inherited data fields  Not accessible by name within definition of a method from another class – including a derived class  Still they are inherited by the derived class Derived classes must use public methods of the base class Note that private methods in a base class are also unavailable to derived classes  But usually not a problem – private methods are used only for utility duties within their class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Protected Access 17 A method or data field modified by protected can be accessed by name only within  Its own class definition  Any class derived from that base class  Any class within the same package

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Protected Access Note accessibility of elements of a class C determined by the access modifiers Fig. 2-4

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Overriding Methods 18 When a derived class defines a method with the same signature as in base class  Same name  Same return type  Same number, types of parameters Objects of the derived class that invoke the method will use the definition from the derived class It is possible to use super in the derived class to call an overridden method of the base class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Overriding Methods 19 Fig. 2-5 The method toString in CollegeStudent overrides the method toString in Student

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Overloading a Method 21 When the derived class method has  The same name  The same return type … but …  Different number or type of parameters Then the derived class has available  The derived class method … and  The base class method with the same name Java distinguishes between the two methods due to the different parameters

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Multiple use of super 22 Consider a class derived from a base … that itself is derived from a base class  All three classes have a method with the same signature The overriding method in the lowest derived class cannot invoke the method in the base class's base class  The construct super.super is illegal

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Overloading a Method 23 A programmer may wish to specify that a method definition cannot be overridden  So that the behavior of the constructor will not be changed This is accomplished by use of the modifier final

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Multiple Inheritance 24 Some languages allow programmer to derive class C from classes A and B Java does not allow this  A derived class can have only one base class Multiple inheritance can be approximated  A derived class can have multiple interfaces  Described in Chapter 3

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Object Types of a Derived Class 25 Given :  Class CollegeStudent,  Derived from class Student Then a CollegeStudent object is also a Student object In general … An object of a derived class is also an object of the base class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X The Class Object 27 Every class is a descendant of the class Object Object is the class that is the beginning of every chain of derived classes  It is the ancestor of every other class  Even those defined by the programmer

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Abstract Classes and Methods 31 Some base classes are not intended to have objects of that type  The objects will be of the derived classes Declare that base class to be abstract public abstract class Whatever {... } The designer often specifies methods of the abstract class without a body public abstract void doSomething();  This requires all derived classes to implement this method

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Polymorphism 34 When one method name in an instruction can cause different actions  Happens according to the kinds of objects that invoke the methods Example The object still remembers it is of type UndergradStudent

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Polymorphism Figure 2-6 The method displayAt calls the correct version of display.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Polymorphism Which displayAt is called …  Depends on the invoking object's place in the inheritance chain and is not determined by the type of the variable naming the object Fig. 2-7 The variable s is another name for an undergraduate object.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Dynamic Binding The process that enables different objects to …  Use different method actions  For the same method name Objects know how they are supposed to act  When an overridden method is used …  The action is for the method defined in the class whose constructor created the object

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Dynamic Binding Fig. 2-8 An object, not its name, determines its behavior.