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.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Constructors Method Overriding & Overloading Encapsulation.
Advertisements

OOP: Inheritance By: Lamiaa Said.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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,
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
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.
Computer Science I Inheritance Professor Evan Korth New York University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance using Java
Chapter 8 More Object Concepts
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.
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 and Polymorphism Daniel Liang, Introduction to Java Programming.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
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.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
Python First Edition STARTING OUT WITH Chapter 10 Inheritance
Polymorphism, Abstract Classes & Interfaces
Inheritance and Polymorphism
by Tony Gaddis and Godfrey Muganda
An Introduction to Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Chapter 9: Polymorphism and Inheritance
Advanced Java Topics Chapter 9
Java – Inheritance.
Java Programming, Second Edition
Chapter 10: Method Overriding and method Overloading
Inheritance.
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Presentation transcript:

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 setter is-a dog A jet is-a motorized plane A rose is-a flower A card game is-a game The specialized object has all of the characteristics of the general object plus additional characteristics that make it special.

Is-a relationships in OO Programming Are created using inheritance Inheritance is used to create is-a relationships among classes. Java does not allow what is called multiple inheritance.

Inheritance terminology General Superclass Base class Specific Subclass Derived class

Super class elements inherited/not inherited by subclass Inherited –Public fields –Public methods Accessors Mutators Not inherited –Constructors –Private fields –Private methods

Important re: constructors Superclass constructors are not inherited. Superclass constructors create superclass objects In an inheritance relationship, the superclass constructor always executes before the subclass constructor. It happens automatically when the superclass has a no-arg constructor or a default constructor.

Important re: constructors What if superclass has –Programmer-defined 1 argument constructor –Programmer-defined 2 argument constructor –Programmer-defined multi- argument constructor –All of the above Subclass uses the keyword super to call the desired superclass constructor

Constructor Guidelines The super statement can only appear in the subclass’s constructor The super statement must be the first statement in the subclass’s constructor IF the subclass constructor does not explicitly call the superclass’s constructor, Java will automatically call the superclasses no-arg or default constructor just before the subclass constructor executes. –Equivalent to super();

Two more important points If a superclass does not have a no-arg constructor but does have a programmer-defined constructor, the subclass must call it or a subclass compile error will occur. Subclass header statement must indicate what its superclass is using the reserved word extends

Important When a subclass extends a superclass, the public members of the superclass become public members of the subclass.

Important Inheritance is a one-way relationship. Subclasses inherit from superclasses. Superclasses do not inherit from subclasses.

Terminology review Overriding vs Overloading –Overloaded methods have the same name as one or more methods but a different parameter list. –Overridden methods have the same signature

Overriding and Overloading Both can take place in an inheritance relationship. Overloaded methods can occur: –Within the same class –A subclass can overload a method in a superclass Overridden methods can only occur in the inheritance relationship –A subclass can not override another method in the same class –A subclass can override a method in its superclass.

How to prevent overriding A method in the superclass with the modifier final can not be overridden.

More on Inheritance AFTER the exam