Lecture 12 Inheritance.

Slides:



Advertisements
Similar presentations
Chapter 1 Inheritance University Of Ha’il.
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
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.
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.
INF 523Q Chapter 7: Inheritance. 2 Inheritance  Another fundamental object-oriented technique is called inheritance, which enhances software design and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 8 Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
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.
© 2004 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 : Inheritance Intermediate Java Programming Summer 2007.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
COS 312 DAY 13 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Over due – Next progress report is March 26 Assignment 4 Posted – Chap 6 & 7 – Due.
Inheritance  Inheritance is a fundamental object-oriented technique  it enhances software design and promotes reuse  We will focus on:  deriving new.
Outline Creating Subclasses Overriding Methods Class Hierarchies Visibility Designing for Inheritance Inheritance and GUIs The Timer Class Copyright ©
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Inheritance Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
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 - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
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.
1 Inheritance  Inheritance allows a software developer to derive a new class from an existing one  The existing class is called the parent class, or.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Object Oriented Concepts Recap OOP. The four Pillars of OOP Abstraction Encapsulation Inheritance Polymorphism.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 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.
Inheritance Chapter 8 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Inheritance New class derived from existing class Software engineering technique Software reuse : faster, easier, cheaper Parent class (supper class):
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Inheritance ITI1121 Nour El Kadri.
Chapter 8 Inheritance.
COS 312 DAY 15 Tony Gauvin.
03/10/14 Inheritance-2.
Abdulmotaleb El Saddik University of Ottawa
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,
03/10/14 Chapter 9 Inheritance.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
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.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming Behnam Hatami Fall 2017.
Overriding Methods & Class Hierarchies
Chapter 8 Inheritance Part 2.
Presentation transcript:

Lecture 12 Inheritance

Inheritance Objectives Derive new classes from existing ones How does inheritance supports software reuse How to add and modify methods in child classes Extend simple class derivations into well-designed class hierarchies Discuss its use in java graphics frame

Introduction Inheritance is the act of deriving a new class from an existing one Derived class is a class created from an existing one Eg: SavingsAccount is the derived class of Account class The key idea here is the software reuse Inheritance is the “is-a” relationship Eg: Spinach “is-a” vegetable

Terminology The original class is called The derived class is called Parent class or Superclass or Base class The derived class is called Child class or subclass

An Example - the base class book { protected int pages = 1500; //---------------------------------------------------------------- // Prints a message concerning the pages of this book. public void pageMessage () System.out.println ("Number of pages: " + pages); }

The derived class dictonary class Dictionary extends Book { private int definitions = 52500; //----------------------------------------------------------------- // Prints a message using both local and inherited values. public void definitionMessage () System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages); }

BOOK Dictionary

The Protected Modifier Not all variables and methods are inherited in derivation The visibility modifier (public, protected, private) decides which gets inherited Protected variables retains the encapsulation properties but derived class will inherit it

The super reference Constructors in base class are not inherited by the derived class, even if they have the public visibility super is a general reference to a parent class The parents constructor can be invoked using the super reference It the super reference is included in the child’s constructor it must be the first line of the constructor

Example - book2 class Book2 { protected int pages; //---------------------------------------------------------------- // Sets up the book with the specified number of pages. public Book2 (int pages) this.pages = pages; } // Prints a message concerning the pages of this book. public void pageMessage () System.out.println ("Number of pages: " + pages);

Example - Dictionary 2 class Dictionary2 extends Book2 { private int definitions; //----------------------------------------------------------------- // Sets up the dictionary with the specified number of pages // (maintained by the Book parent class) and defintions. public Dictionary2 (int pages, int definitions) super (pages); this.definitions = definitions; } // Prints a message using both local and inherited values. public void definitionMessage () System.out.println ("Number of definitions: " + definitions); System.out.println ("Definitions per page: " + definitions/pages);

Multiple inheritance Java’s approach to inheritance is called “single inheritance” Languages like C++ allow multiple inheritance but Java does not PickUpTruck “is-a” car and a truck If parents classes have similar methods, which ones the child supports? However the use of interface provides some of the abilities of multiple inheritance A java class can implement many interfaces It allows to interact with a particular class while inheriting most crucial information

Overriding methods If a child class defines a method with the same name and signature, the child class overrides the parents method from the parent Eg: Message class contains a method that instantiate two objects One from Thought class One from Advice class

Examples class Messages { //----------------------------------------------------------------- // Instatiates two objects a invokes the message method in each. public static void main (String[] args) Thought parked = new Thought(); Advice dates = new Advice(); parked.message (); dates.message (); // overridden }

Class hierarchies Multiple classes can be derived from a single parent. Eg: animal class (parent), Rabbit, Cow, Cat (children) Two children of the same parent are called siblings Common features should be kept as high as possible in the class hierarchy Inheritance mechanism is transitive There are no single best hierarchy organization for all situations

The object class In Java all classes are ultimately derived from the “object” class. If the class definition doesn’t uses the extends clause, then it is automatically derived from the object class. class Thing{ } class Thing extends Object{ } Are equivalent

Object class methods boolean equals (Object obj); String toString(); Returns true if the objects are the same(aliases) String toString(); Returns a string representation of this object Object clone(); Creates and returns a copy of this object

Abstract Classes An abstract class is a generic concept in a class hierarchy Abstract class cannot be instantiated Abstract class is similar to an interface Any class that contains one or more abstract methods is declared to be abstract Eg: public abstract class StaffMember{} Eg: Vehicle (abstract), derive car, Van,….