Inheritance Inheritance allows a software developer to derive a new class by extending an existing one. The existing class is called the parent class or.

Slides:



Advertisements
Similar presentations
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
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.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
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.
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.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance. 2 Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class or superclass.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
WHAT IS INHERITANCE? Java Unit 11: Inheritance I.
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.
Inheritance using Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Intro to OOP with Java, C. Thomas Wu
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
Peyman Dodangeh Sharif University of Technology Fall 2014.
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
Topics Inheritance introduction
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 Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
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.
Object-Oriented Programming: Inheritance and Polymorphism.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Interface, Subclass, and Abstract Class Review
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,
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Chapter 10 Thinking in Objects
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Advanced Programming Behnam Hatami Fall 2017.
Java – Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance Inheritance allows a software developer to derive a new class by extending an existing one. The existing class is called the parent class or superclass The derived class is called the child class or subclass. Inheritance creates an is-a relationship. The subclass is a more specific version of the original. (Remember: has-a is aggregation. Not to be confused!)

Inheritance Terminology: class - a data type extend - to make a new class that inherits the properties of an existing class superclass - A parent or “base” class subclass - a child class that inherits, or extends, a superclass.

Kingdom Animalia – Phylum Chordata

Window World Window Modal Window Dialog box Alert window … Non-Modal Window OS Window(minimizable, closeable, maximizable) Window w/ Menu Window w/ ribbon Toast alerts

Software Reuse The child class inherits the members (methods and data) defined in the parent class. (inheritance) To tailor a derived class, the programmer can add new variables or methods, or can even modify the inherited ones. (customization/specialization) Design approach: Class hierarchies Advantage: Software reuse By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software.

Polymorphism in Variables Polymorphism allows a single variable to refer to objects from different subclasses in the same inheritance hierarchy For example: Window aWindow; aWindow = new ModalWindow(); . . . aWindow = new ToastAlert();

Creating an Array We can exploit the class hierarchy when we use an array, by combining objects from the Window classes. Window theWindows = new Window[40]; . . . theWindows[0] = new MenuWindow(); theWindows[1] = new ToastAlert(); theWindows[2] = new RibbonWindow();

State of the Array The array theWindows has elements referring to instances of both MenuWindow, ToastAlert and RibbonWindow classes. In fact any class that is inheriting from Window 0 1 2 3 38 39 MenuWindow ToastAlert MenuWindow AlertWindow ToastAlert RibbonWindow

Polymorphism and Method Invocation To show all of the windows, we can now for (int i = 0; i < theWindows.length ; i++) { theWindows[i].show(); } for (Window window : theWindows) { window.show(); }

The “instanceof” Operator The instanceof operator allows us to learn the class of an object. The following code will only show ToastAlert windows. for (Window window : theWindows){ if(window instanceof ToastAlert) { window.show(); }

Inheritance and Constructors Unlike members of a superclass, constructors of a superclass are not inherited by its subclasses. You must define a constructor for a class or use the default constructor added by the compiler. The statement: super(); calls the superclass’s constructor.