An Introduction to Inheritance

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
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.
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
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.
1 Inheritance and Polymorphism Inheritance (Continued) Polymorphism Polymorphism by inheritance Polymorphism by interfaces Reading for this lecture: L&L.
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.
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.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Chapter 10: Inheritance 1. Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called.
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.
Chapter 8 More Object Concepts
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.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
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.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
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 allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
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.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Coming up: Inheritance
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.
November 27, 2001Lecture 231  Previous Lecture: Parameter passing Method overloading  Today’s Lecture: Introduction to inheritance Class diagrams and.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance.
Modern Programming Tools And Techniques-I
Class Inheritance Part I
Lecture 12 Inheritance.
Inheritance and Polymorphism
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Road Map Inheritance Class hierarchy Overriding methods Constructors
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
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
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Overriding Methods & Class Hierarchies
Java Programming, Second Edition
Fundaments of Game Design
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 7 Inheritance.
Presentation transcript:

An Introduction to Inheritance Lecture 11

What is inheritance? Inheritance is the process in which a new class is derived from an existing one

What is the core of inheritance? At the heart of inheritance is the idea of software reuse

What is a class? A class is a set of elements The set is defined using the characteristics of the elements rather than by listing the elements

What are the characteristics the attributes the methods Javadocs describes the class in terms of its characteristics

What do we call the original class? the parent class or the super class the base class

What do we call a derived class? a child class or a sub class

What is the relationship between the base class and a derived class? The relationship is an is-a-relationship A derived class is-a more specific version of the original or base class A derived class does everything the base class does and more

What else can we say about the is-a-relationship? A derived class is a specialization of its base class. A base class is a generalization of its derived class(es).

How can we graphically indicate the is-a-relationship? We can use UML diagrams UML stands for Unified Modeling Language In a UML diagram, the arrow points from the derived class to the base class (i.e. from the child class to the parent class). See Class Hierarchies.doc

What does a child class inherit from its parent class? The child class inherits methods and attributes (variables) from the parent class. The child class can use public methods and access public attributes of the parent class The child class can use protected methods and access protected attributes of the parent class. The child class can not use private methods nor access private attributes of the parent class

What can a parent class inherit from one of its child classes? NOTHING!!! The is-a-relationship is NOT symmetric Children inherit from parents but parents DON’T inherit from children except for insanity

How do we indicate that a class is a derived class? We use the Java reserved word extends. The word extends indicates that a new class is being derived from an existing class. Many child classes may be derived from a single parent but in Java, each child only has one parent. in Java, no child can be derived from more than one parent. Java uses single inheritance.

How does a child access characteristics of the superclass? by using the reserved word super

What about constructors? Constructors are not inherited by the child class. The constructor of the parent class can be explicitly invoked by the child class using the reserved word super along with the appropriate parameter list. The constructor of the parent class has to be called first (before anything else in the child class) Only one of the parent class’ constructors can be called. Child must choose.

What are some of the uses of derived classes? to add characteristics to what exists in the base class to modify behavior

How does a derived class modify the behavior of its base class?

by overriding existing methods by adding additional methods a method in the derived class which overrides a method in its base class has the same signature (name and parameter list) but behaves differently.

What is the difference between the terms overloading and overriding as applied to methods? overloaded methods have different signatures and are in the same class. overriden methods have the same signature and are in different classes.

How else can we explain the difference between overloaded and overriden methods? With an overloaded method choice of which method is being invoked is determined by which method’s number, order and type of parameters match the call. With an overridden method, access to the method in the parent class is blocked (the parent class’ method is hidden and can’t be seen unless the word super is applied) The word super can only be used INSIDE the child class.