Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.

Slides:



Advertisements
Similar presentations
Starting Out with Java: From Control Structures through Objects
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
OOP: Inheritance By: Lamiaa Said.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
(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.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
CSCI 143 OOP – Inheritance 1. What is Inheritance? A form of software reuse Create a new class from an existing class – Absorb existing class data and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance, Polymorphism, and Virtual Functions
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance Chapter 11.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Intro to OOP with Java, C. Thomas Wu
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
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.
Chapter Outline What inheritance is Calling the superclass constructor Overriding superclass methods Protected members Chains of inheritance The Object.
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 11: Inheritance Starting Out with Java: From Control Structures through Data Structures Second.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
Inheritance and Access Control CS 162 (Summer 2009)
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
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.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Chapter 11 Polymorphism. Contents I. Polymorphism 1. Polymorphism 2. Polymorphism and Dynamic Binding 3. The “is-a” Relationship Does Not Work in Reverse.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Modern Programming Tools And Techniques-I
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
Starting Out with Java: From Control Structures through Objects
Inheritance, Polymorphism, and Virtual Functions
by Tony Gaddis and Godfrey Muganda
An Introduction to Inheritance
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 9: Polymorphism and Inheritance
Inheritance, Polymorphism, and Virtual Functions
Polymorphism, Abstract Classes & Interfaces
Chapter 11 Inheritance and Polymorphism Part 1
Lecture 0311 – Polymorphism
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Presentation transcript:

Inheriatance

9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects. The term “insect” describes a very general type of creature with numerous characteristics. Grasshoppers and bumblebees are insects – They share the general characteristics of an insect. – However, they have special characteristics of their own. grasshoppers have a jumping ability, and bumblebees have a stinger. Grasshoppers and bumblebees are specialized versions of an insect.

9-3 Inheritance Insect GrasshopperBumbleBee Contains those attributes and methods that are shared by all insects. Contains those attributes and methods that specific to a Bumble Bee. Contains those attributes and methods that are specific to a Grasshopper.

9-4 The “is a” Relationship The relationship between a superclass and an inherited class is called an “is a” relationship. – A grasshopper “is a” insect. – A poodle “is a” dog. – A car “is a” vehicle. A specialized object has: – all of the characteristics of the general object, plus – additional characteristics that make it special. In object-oriented programming, inheritance is used to create an “is a” relationship among classes.

9-5 The “is a” Relationship We can extend the capabilities of a class. Inheritance involves a superclass and a subclass. – The superclass is the general class and – the subclass is the specialized class. The subclass is based on, or extended from, the superclass. – Superclasses are also called base classes, and – subclasses are also called derived classes. The relationship of classes can be thought of as parent classes and child classes.

9-6 Inheritance The subclass inherits fields and methods from the superclass without any of them being rewritten. New fields and methods may be added to the subclass. The Java keyword, extends, is used on the class header to define the subclass. public class FinalExam extends GradedActivity

9-7 The GradedActivity Example GradedActivity - score : double + setScore(s : double) : void + getScore() : double + getGrade() : char FinaExam - numQuestions : int - pointsEach : double - numMissed : int + FinalExam(questions : int, missed : int) + getPointsEach() : double + getNumMissed() : int Contains those attributes and methods that are shared by all graded activities. Contains those attributes and methods that are specific to the FinalExam class. Inherits all non-private attributes and methods from the GradedActivity class.

9-8 Inheritance, Fields and Methods Members of the superclass that are marked private: – are not inherited by the subclass, – exist in memory when the object of the subclass is created – may only be accessed from the subclass by public methods of the superclass. Members of the superclass that are marked public: – are inherited by the subclass, and – may be directly accessed from the subclass.

9-9 Inheritance, Fields and Methods When an instance of the subclass is created, the non-private methods of the superclass are available through the subclass object. FinalExam exam = new FinalExam(); exam.setScore(85.0); System.out.println("Score = " + exam.getScore()); Non-private methods and fields of the superclass are available in the subclass. setScore(newScore);

9-10 Inheritance and Constructors Constructors are not inherited. When a subclass is instantiated, the superclass default constructor is executed first. Example: – SuperClass1.java SuperClass1.java – SubClass1.java SubClass1.java – ConstructorDemo1.java ConstructorDemo1.java

9-11 The Superclass’s Constructor The super keyword refers to an object’s superclass. The superclass constructor can be explicitly called from the subclass by using the super keyword. Example: – SuperClass2.java, SubClass2.java, ConstructorDemo2.java SuperClass2.javaSubClass2.java ConstructorDemo2.java – Rectangle.java, Cube.java, CubeDemo.java Rectangle.javaCube.javaCubeDemo.java

9-12 Calling The Superclass Constructor If a parameterized constructor is defined in the superclass, – the superclass must provide a no-arg constructor, or subclasses must provide a constructor, and subclasses must call a superclass constructor. Calls to a superclass constructor must be the first java statement in the subclass constructors.

9-13 Overriding Superclass Methods A subclass may have a method with the same signature as a superclass method. The subclass method overrides the superclass method. This is known as method overriding. Example: – GradedActivity.java, CurvedActivity.java, CurvedActivityDemo.java GradedActivity.javaCurvedActivity.java CurvedActivityDemo.java

9-14 Overriding Superclass Methods GradedActivity - score : double + setScore(s : double) : void + getScore() : double + getGrade() : char CurvedActivity - rawScore : double - percentage : double + CurvedActivity (percent : double) + setScore(s : double) : void + getRawScore() : double + getPercentage() : double This method is a more specialized version of the setScore method in the superclass, GradedActivity.

9-15 Overriding Superclass Methods Recall that a method’s signature consists of: – the method’s name – the data types method’s parameters in the order that they appear. A subclass method that overrides a superclass method must have the same signature as the superclass method. An object of the subclass invokes the subclass’s version of the method, not the superclass’s.

9-16 Overriding Superclass Methods An subclass method can call the overridden superclass method via the super keyword. super.setScore(rawScore * percentage); There is a distinction between overloading a method and overriding a method. Overloading is when a method has the same name as one or more other methods, but with a different signature. When a method overrides another method, however, they both have the same signature.

9-17 Overriding Superclass Methods Both overloading and overriding can take place in an inheritance relationship. Overriding can only take place in an inheritance relationship. Example: – SuperClass3.java, SuperClass3.java – SubClass3.java, SubClass3.java – ShowValueDemo.java ShowValueDemo.java

9-18 Preventing a Method from Being Overridden The final modifier will prevent the overriding of a superclass method in a subclass. public final void message() If a subclass attempts to override a final method, the compiler generates an error. This ensures that a particular superclass method is used by subclasses rather than a modified version of it.

9-19 Protected Members Protected members of class: – may be accessed by methods in a subclass, and – by methods in the same package as the class. Java provides a third access specification, protected. A protected member’s access is somewhere between private and public. Example: – GradedActivity2.java GradedActivity2.java – FinalExam2.java FinalExam2.java – ProtectedDemo.java ProtectedDemo.java

9-20 Protected Members Using protected instead of private makes some tasks easier. However, any class that is derived from the class, or is in the same package, has unrestricted access to the protected member. It is always better to make all fields private and then provide public methods for accessing those fields. If no access specifier for a class member is provided, the class member is given package access by default. Any method in the same package may access the member.

9-21 Access Specifiers Access Modifier Accessible to a subclass inside the same package? Accessible to all other classes inside the same package? default (no modifier) Yes PublicYes ProtectedYes PrivateNo Access Modifier Accessible to a subclass outside the package? Accessible to all other classes outside the package? default (no modifier) No PublicYes ProtectedYesNo PrivateNo

9-22 Chains of Inheritance A superclass can also be derived from another class. Object PassFailActivity PassFailExam GradedActivity Example: GradedActivity.java PassFailActivity.java PassFailExam.java PassFailExamDemo.java GradedActivity.java PassFailActivity.java PassFailExam.java PassFailExamDemo.java

9-23 Chains of Inheritance Classes often are depicted graphically in a class hierarchy. A class hierarchy shows the inheritance relationships between classes. PassFailActivity PassFailExam GradedActivity FinalExam