Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, 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.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
(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.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
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.
© 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.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Inheritance in the Java programming language J. W. Rider.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
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. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Specialization aka Inheritance. 2 Inheritance  Review of class relationships  Uses – One class uses the services of another class, either.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Polymorphism
An Introduction to Inheritance
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9 Inheritance and Polymorphism
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.
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Packages and Interfaces
Week 6 Object-Oriented Programming (2): Polymorphism
Overriding Methods & Class Hierarchies
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview 3.1 Extending a class How and when to create a subclass that inherits code from the class (superclass) it extends. A better solution is to use inheritance. 3.2 The Object class Object class, from which all other classes are ultimately derived. 3.3 Packages and Access Levels Packages (collections of classes) and access levels, which give us finer control over who can see which parts of our classes. Refer to http://www.tutorialspoint.com/java/java_inheritance.htm

Extending a Class The Cram game is identical to Domineering except that a player can play each domino either horizontally or vertically. Should we write a new program that looks very similar to the old one? The answer is no. Use inheritance. Cram extends Domineering. New class specifies only the things that are different.

Extending a Class

Extending a Class

Extending a Class Cram is called a subclass of Domineering. Domineering is a superclass of Cram.

Extending a Class Fields and methods not listed in the code for the Cram class are inherited from the Domineering class. If we invoke playAt() or hasLegalMoveFor() the method from the Domineering class is used. The Cram class does override two methods, play() and main(). Additional fields and methods, along with overridden methods, make a Cram instance different from a Domineering instance.

Extending a Class When we override a method, Java decides which version to use based on the object on which the method is invoked. The invocation super(); in a constructor means “do whatever you would do to set up an instance of the parent class”. A constructor in a subclass must always begin by invoking a constructor from the class it extends, although, this invocation can often be implicit.

Extending a Class Inheritance allows us to extend an encapsulated class without thinking about its inner workings. Extending a class is similar to implementing an interface. Key Difference: A superclass provides functionality, while an interface makes promises. A class can implement many interfaces, but it can only have one superclass.

Extending a Class

Extending a Class

Extending a Class Polymorphism and Inheritance Light class could be extended by a ColoredLight class which also has a char indicating its color. The color is determined randomly in the constructor. Red, green, or blue ColoredLights

Extending a Class

Extending a Class

Extending a Class Although ColoredLight inherits the field on, it does not have direct access to the field. Must work through the inherited method isOn() because the field on is private in Light. Private means that no other class, not even a subclass of Light, has direct access to on.

Extending a Class

Extending a Class A variable of type Light can hold an instance of Light or of any subclass of Light. Light bulb = new ColoredLight(); All of the methods defined in the Light class are available to ColoredLight. Every method in Light has to be either inherited or overridden. It is safe to call such a method on bulb. bulb.setOn(false);

Extending a Class If we invoke toString() on bulb, Java uses the class of the instance (ColoredLight) instead of the type of the variable (Light) to determine which version of the method to use. Dynamic dispatch The decision is made dynamically at run time rather than once and for all at compile time. Chains of inheritance Can we make a subclass of subclass? Sure!

Extending a Class

Extending a Class

Extending a Class ($) Proper descendants Descendants Proper ancestors ColoredLight and FlashingColoredLight Descendants Light itself plus its proper descendants. Proper ancestors Light and ColoredLight are proper ancestors of FlashingColoredLight. Ancestors All three classes are ancestors of FlashingColoredLight.

Extending a Class ColoredLight and FlashingColoredLight are both sublasses of Light, but only ColoredLIght is a direct subclass. Light and ColoredLight are superclasses of FlashingColoredLight, but ColoredLight is the direct superclass.

Extending a Class If an instance of the new class is just like an instance of the old class, with only a few modifications, we should extend it. For example, a ColorLight is a Light, but with the added feature of color, so extension is appropriate. If an instance of the new class merely has an instance of the old class as a component, we should not extend it. For example, an instance of BeetleGame merely has two beetles, so it is not appropriate for it to extend Beetle. These are is-a (yes) and has-a (no) relationships. Refer to http://www.tutorialspoint.com/java/java_inheritance.htm

Extending a Class

Extending a Class

Extending a Class Output of the LightString class looks like: Inheritance should be used only when an instance of the subclass can stand in for an instance of the superclass. Example A class Bicycle with a method pedal(). Motorcycle should not extend Bicycle, because the pedal() method wouldn't make sense for a Motorcycle.

Extending a Class

The Object Class ($) Object can hold a reference to any object. The Object class is an ancestor to every other class. If a class doesn't say that it extends some other class, means it extends from Object.

The Object Class The Object class does have a few methods that are usually overridden. The equals() method for the Object class behaves exactly like ==. In order to override the equals(), a method must be provided with the same signature. If we use a different argument type, we are not overriding the method, but merely overloading the name with a method accepting a different argument type.

The Object Class The toString() method for the Object class returns a String like: java.lang.Object@9c26f5 The Instance location in memory is 9c26f5 The instance type (class of instance) is java.lang.Object

The Object Class Implicit Constructors Every class that does not provide an explicit constructor gets an implicit zero-argument constructor which does nothing but invoke the zero-argument super constructor. E.g. super(); (line 9 in Fig. 3-4, p71) Java allows us to omit this line.

Packages and Access Levels A method encapsulates an algorithm. A class encapsulates state (fields) and behavior (methods). A package which contains a number of classes provides encapsulation on an even larger scale. Classes divided into packages helps keep the classes organized. It gives programmers more freedom in choosing names.

Packages and Access Levels There are over 100 packages with Java software. Some commonly used:

Packages and Access Levels The full name of a class includes both its package name and its class name. Example: Object is java.lang.Object Within a package, the package name can be dropped. Java doesn't require us to explicitly name the java.lang package because the classes in this package are used so often. If there is no package declared for a class it belongs to the default package.

Packages and Access Levels To use the Scanner class from the java.util package import java.util.Scanner; If we want to use several classes from a package, it is easier to import the entire package. Example: import java.util.*;

Packages and Access Levels To put a class in a particular package: Before any import statements add a line at the top of the file defining the package. Example: package fruit; The package structure must be reflected in the directory structure where the files are stopped. Package names with dots in them correspond to nested directories. Example: Classes in the social.relationships package must be in the relationships subdirectory of the social subdirectory (social/relationships).

Packages and Access Levels We have seen two access levels so far Private Visible only within the class where it is declared. Public Visible everywhere. Protected It is visible to any class which either is in the same package or in a descendant. If no access level is specified, a field or method gets the default package level, which makes it visible to all classes in the same package.

Packages and Access Levels Fig. 3-16: The fruit package contains the classes Date and Strawberry. The dessert package contains the classes IceCream and ChocolateCoverdStrawberry (a subclass of Strawberry).

Packages and Access Levels getSeed() within Strawberry. If it is private, it can be accessed only within the Strawberry class. If no protection level is specified, it gets the default package level of protection, so it can be accessed only from Date and Strawberry. If it is protected, it can be accessed from Date, Strawberry, and ChocolateCoveredStrawberry. If it is public, it can be accessed by all classes. See Figure 3-15 on page 81 for details. Level Visible to private same class only (package) same package only protected same package and descendents public all classes

Summary Every class (except for the Object class itself) extends one other class. Every class is a polymorphic type Java distinguishes between the type of a variable and the class of the instance to which it refers. A subclass inherits the methods and fields of its superclass, but it can override methods in the superclass and add new fields and methods. Classes can be grouped into packages.

Chapter 3 Self-Study Homework Pages: 76-84 Do the following problems: 3.3, 3.4 Note: If necessary, hand in the error-free program and screenshot of the execution results for each exercise.