Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

OOP: Inheritance By: Lamiaa Said.
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.
Chapter Inheritance Jim Burns. The Concept of Inheritance Since day one, you have been creating classes and instantiating objects that are members.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 10: Introduction to Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Advanced Object-Oriented Programming Features
Chapter 10 Classes Continued
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
© 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.
Chapter 7 - Generalization/Specialization and Inheritance1 Chapter 7 Generalization/Specialization and Inheritance.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
What is inheritance? It is the ability to create a new class from an existing class.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance and Access Control CS 162 (Summer 2009)
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.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
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.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Classes, Interfaces and Packages
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.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Class Inheritance Part I
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Modern Programming Tools And Techniques-I Inheritance
Understanding Inheritance
Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Polymorphism
Advanced Java Topics Chapter 9
METHOD OVERRIDING in JAVA
Computer Programming with JAVA
Java – Inheritance.
Java Programming, Second Edition
Inheritance.
Advanced Inheritance Concepts
Inheritance and Polymorphism
Presentation transcript:

Chapter 10: Introduction to Inheritance

Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors during inheritance Access superclass methods Employ information hiding Learn which methods you cannot override 2Java Programming, Seventh Edition

Learning About the Concept of Inheritance Inheritance – A mechanism that enables one class to inherit both the behavior and the attributes of another class – Apply your knowledge of a general category to more specific objects 3Java Programming, Seventh Edition

Diagramming Inheritance Using the UML Unified Modeling Language (UML) – Consists of many types of diagrams Class diagram – A visual tool – Provides an overview of a class 4Java Programming, Seventh Edition

Diagramming Inheritance Using the UML (cont’d.) 5Java Programming, Seventh Edition

Diagramming Inheritance Using the UML (cont’d.) 6Java Programming, Seventh Edition

Diagramming Inheritance Using the UML (cont’d.) Use inheritance to create a derived class – Save time – Reduce errors – Reduce the amount of new learning required to use a new class 7Java Programming, Seventh Edition

Inheritance Terminology Base class – Used as a basis for inheritance – Also called: Superclass Parent class 8Java Programming, Seventh Edition

Inheritance Terminology (cont’d.) Derived class – Inherits from a base class – Always “is a” case or an example of a more general base class – Also called: Subclass Child class 9Java Programming, Seventh Edition

Extending Classes Keyword extends – Used to achieve inheritance in Java – Example: public class EmployeeWithTerritory extends Employee Inheritance is a one-way proposition – A child inherits from a parent, not the other way around Subclasses are more specific instanceof operator 10Java Programming, Seventh Edition

Extending Classes (cont’d.) 11Java Programming, Seventh Edition

Overriding Superclass Methods Create a subclass by extending an existing class – A subclass contains data and methods defined in the original superclass – Sometimes superclass data fields and methods are not entirely appropriate for subclass objects Polymorphism – Using the same method name to indicate different implementations 12Java Programming, Seventh Edition

Overriding Superclass Methods (cont’d.) Override the method in the parent class – Create a method in a child class that has the same name and parameter list as a method in its parent class Subtype polymorphism – The ability of one method name to work appropriately for different subclass objects of the same parent class 13Java Programming, Seventh Edition

Calling Constructors During Inheritance When you instantiate an object that is a member of a subclass, you call at least two constructors: – The constructor for the base class – The constructor for the extended class The superclass constructor must execute first When the superclass contains a default constructor, the execution of the superclass constructor is transparent 14Java Programming, Seventh Edition

Calling Constructors During Inheritance (cont’d.) Figure 10-8 Three classes that demonstrate constructor calling when a subclass object is instantiated 15Java Programming, Seventh Edition

Calling Constructors During Inheritance (cont’d.) Figure 10-9 Output of the DemoConstructors application 16Java Programming, Seventh Edition

Using Superclass Constructors That Require Arguments When you write your own constructor, you replace the automatically supplied version When extending a superclass with constructors that require arguments, the subclass must provide the superclass constructor with the arguments it needs 17Java Programming, Seventh Edition

Using Superclass Constructors That Require Arguments (cont’d.) When a superclass has a default constructor, you can create a subclass with or without its own constructor When a superclass contains only constructors that require arguments, you must include at least one constructor for each subclass you create – The first statement within each constructor must call one of the superclass constructors 18Java Programming, Seventh Edition

Using Superclass Constructors That Require Arguments (cont’d.) Call the superclass constructor – super(list of arguments); Keyword super – Always refers to the superclass 19Java Programming, Seventh Edition

Use the overridden superclass method within a subclass – Use the keyword super to access the parent class method 20Java Programming, Seventh Edition Accessing Superclass Methods

Accessing Superclass Methods (cont’d.) Figure The PreferredCustomer class 21Java Programming, Seventh Edition

Comparing this and super Think of the keyword this as the opposite of super within a subclass When a parent class contains a method that is not overridden, the child can use the method name with super or this, or alone 22Java Programming, Seventh Edition

Employing Information Hiding Within the Student class: – The keyword private precedes each data field – The keyword public precedes each method Information hiding – The concept of keeping data private – Data can be altered only by methods you choose and only in ways that you can control 23Java Programming, Seventh Edition

Employing Information Hiding (cont’d.) Figure The Student class 24Java Programming, Seventh Edition

Employing Information Hiding (cont’d.) When a class serves as a superclass, subclasses inherit all data and methods of the superclass – Except private members of the parent class are not accessible within a child class’s methods 25Java Programming, Seventh Edition

Employing Information Hiding (cont’d.) Keyword protected – Provides an intermediate level of security between public and private access – Can be used within its own class or in any classes extended from that class – Cannot be used by “outside” classes 26Java Programming, Seventh Edition

Methods You Cannot Override static methods final methods Methods within final classes 27Java Programming, Seventh Edition

A Subclass Cannot Override static Methods in Its Superclass A subclass cannot override methods declared static in the superclass A subclass can hide a static method in the superclass by declaring a static method with the same signature as the static method in the superclass – Then call the new static method from within the subclass or in another class by using a subclass object – Within the static method of a subclass, you cannot access the parent method using the super object 28Java Programming, Seventh Edition

A Subclass Cannot Override static Methods in Its Superclass (cont’d.) Although a child class cannot inherit its parent’s static methods, it can access its parent’s static methods in the same way any other class can 29Java Programming, Seventh Edition

A Subclass Cannot Override static Methods in Its Superclass (cont’d.) Figure The ProfessionalBaseballPlayer class 30Java Programming, Seventh Edition

A Subclass Cannot Override final Methods in Its Superclass A subclass cannot override methods declared final in the superclass final modifier – Does not allow the method to be overridden Virtual method calls – Default in Java – The method used is determined when the program runs – The type of object used might not be known until the method executes 31Java Programming, Seventh Edition

A Subclass Cannot Override final Methods in Its Superclass (cont’d.) Advantages to making the method final : – The compiler knows there is only one version of the method – The compiler knows which method version will be used – It can optimize a program’s performance by removing calls to final methods Replaces them with expanded code of their definitions at each method call location Called inlining the code 32Java Programming, Seventh Edition

A Subclass Cannot Override Methods in a final Superclass When a class is declared final : – All of its methods are final regardless of which access modifier precedes the method name – It cannot be a parent class 33Java Programming, Seventh Edition

A Subclass Cannot Override Methods in a final Superclass (cont’d.) Figure The HideAndGoSeekPlayer and ProfessionalHideAndGoSeekPlayer classes 34Java Programming, Seventh Edition

You Do It Demonstrating Inheritance Overriding a Superclass Method Understanding the Role of Constructors in Inheritance 35Java Programming, Seventh Edition

Don’t Do It Don’t capitalize the o in the instanceof operator Don’t try to directly access private superclass members from a subclass Don’t forget to call a superclass constructor from within a subclass constructor if the superclass does not contain a default constructor Don’t try to override a final method in an extended class Don’t try to extend a final class 36Java Programming, Seventh Edition

Summary Inheritance – A mechanism that enables one class to inherit both the behavior and the attributes of another class Keyword extends – Used to achieve inheritance in Java Polymorphism – The act of using the same method name to indicate different implementations 37Java Programming, Seventh Edition

Summary (cont’d.) Use a superclass method within a subclass – Use the keyword super to access it Information hiding – The concept of keeping data private Keyword protected – Provides an intermediate level of security between public and private access A subclass cannot override methods that are: – Declared static in a superclass – Declared final or declared within a final class 38Java Programming, Seventh Edition