Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

OOP: Inheritance By: Lamiaa Said.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Creating Classes from Other Classes Chapter 2 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.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
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.
© 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.
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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 is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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.
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Inheritance 2nd Lecture
The super Reference Constructors cannot be used in child classes, even though they have public visibility Yet we often want to use the parent's constructor.
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
CMSC 202 Inheritance II.
Chapter 7 Inheritance.
Presentation transcript:

Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved

Composition A class uses composition when it has a data field that is an instance of another class Composition is a “has a” relationship Consider a class of students, each has – A name, an identification number. Thus, class Student contains two objects as data fields: – An instance of the class Name – An instance of the class String: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Composition FIGURE D-1 A Student object is composed of other objects © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Composition LISTING D-1 The class Student © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Composition LISTING D-1 The class Student © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Adapters Consider reuse of a class where … – Names of its methods do not suit your application – You want to simplify some methods – Or eliminate others An adapter class – Uses composition to write a new class that has an instance of your existing class as a data field – Defines the methods that you want © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Adapters LISTING D-2 The class NickName © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Inheritance Allows you to define general class – Then later to define more specialized classes – Add to or revise the details of the older, more general class definition Inheritance is an “is a” relationship Example: general class of vehicles – Subclasses automobile, wagon, and boat, etc. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Inheritance FIGURE D-2 A hierarchy of classes © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Inheritance FIGURE D-3 A hierarchy of student classes © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved`

Inheritance LISTING D-3 The class CollegeStudent © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved`

Inheritance LISTING D-3 The class CollegeStudent © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Invoking Constructors from Within Constructors Constructors typically initialize a class’s data fields To call constructor of superclass explicitly. – Use super( ) within definition of a constructor of a subclass If you omit super( ) – Constructor of subclass automatically calls default constructor of superclass. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Invoking Constructors from Within Constructors Also possible to use this to invoke constructor of superclass © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Private Fields and Methods of the Superclass Only a method in the class Student can access fullName and id directly by name from within its definition. Although the class CollegeStudent inherits these data fields, – None of its methods can access them by name Instead it must use some public mutator method such as setId. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods Possible to new method invoke the inherited method – Need to distinguish between the method for subclass and method from superclass © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods When a subclass defines a method with – the same name – the same number and types of parameters – and the same return type as a method in the superclass … Then definition in the subclass is said to override the definition in the superclass. You can use super in a subclass to call an overridden method of the superclass. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods FIGURE D-4 The method toString in CollegeStudent overrides the method toString in Student © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods When subclass has a method with same name as a method in its superclass, – but the methods’ parameters differ in number or data type … Method in subclass overloads method of superclass. – Java is able to distinguish between these methods – Signatures of the methods are different © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods Possible to call an overridden method of the superclass by prefacing the method name with super and a dot. But … repeated use of super is not allowed © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Overriding and Overloading Methods To specify that a method definition cannot be overridden with a new definition in a subclass – Make it a final method by adding the final modifier to the method header. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Multiple Inheritance Some programming languages allow one class to be derived from two different superclasses – This feature not allowed in Java. In Java, a subclass can have only one superclass © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Type Compatibility and Superclasses An object of a subclass has more than one data type. Everything that works for objects of an ancestor class also works for objects of any descendant class. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Type Compatibility and Superclasses Given CollegeStudent, subclass of Student Legal calls Illegal calls © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

The Class Object Java has a class—named Object – It is at the beginning of every chain of subclasses – An ancestor of every other class Class Object contains certain methods – Examples: toString, equals, clone – However, in most cases, you must override these methods © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

The Class Object Inherited version of toString returns value based upon invoking object’s memory address. Need to override the definition of toString – Cause it to produce an appropriate string for data in the class being defined © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

The Class Object Object’s equals method compares the addresses of two objects – Overridden method, when added to the class Name, detects whether two Name objects are equal by comparing their data fields: © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

The Class Object Class Object method clone. – Takes no arguments and returns a copy of the receiving object We will need to override this method Discussion of the method clone appears in Java Interlude 9. © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved

Creating Classes from Other Classes End © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved