1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Final and Abstract Classes
1 Inheritance Classes and Subclasses Or Extending a Class.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Object Oriented Programming with Java
Inheritance and OOP Chapter 11. Chapter Contents Chapter Objectives 11.1 Introductory Example: A Trip to the Aviary 11.2 Inheritance and Polymorphism.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
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.
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.
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.
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.
Chapter 10 Classes Continued
Computer Science I Inheritance Professor Evan Korth New York University.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Inheritance using Java
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
What is inheritance? It is the ability to create a new class from an existing class.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Peyman Dodangeh Sharif University of Technology Fall 2014.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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 Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Object Oriented Programming
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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 Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Advanced Programming in Java
Lecture 12 Inheritance.
Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Java Inheritance.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Java Inheritance.
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
Chapter 11 Inheritance and Polymorphism Part 1
Java Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

1 What is Inheritance? A form of software reuse Create new class from existing class Absorb existing class data and behaviors Enhance with new or modified capabilities Used to eliminate redundant code Example Circle class inherits from Shape class Circle extends Shape

2 Subclass and Superclass Subclass extends superclass Subclass –Also called child class or derived class –More specialized group of objects –Inherits data and behaviors from superclass –Can add or modify behaviors (methods) Superclass –Also called parent class or base class –Typically represents larger group of objects –Supplies data and behaviors to subclass –Direct and indirect Single and multiple inheritance

3 The Object class Top of the Java class hierarchy Located in package java.lang Class from which every other Java class inherits A class implicitly extends Object if no other class is specified.toString(),.clone(),.equals() equals() compares addresses, not the contents of objects

4 is-a vs. has-a relationships is-a Represents inheritance subclass object is an example of the superclass object Example: a Car is a Vehicle Car is subclass; Vehicle is superclass Keywords: extends, implements has-a Represents composition Object contains one or more objects of other classes as members Example: Car has a Steering Wheel

5 Composition Dog has-a owner, mother, father, leash… public class Dog { private String name; private int age; private Person owner; private Dog mother, father; private Leash dogLeash; }

6 Inheritance Animals Stuff Dogs Stuff Animal Dog Dog extends (is-a) Animal

7 public class Animal { private int numLegs; public int getLegs() { return numLegs; } public void setLegs(int legs) { numLegs = legs; } public String noise() { return ?; } } public class Dog extends Animal { public static void main (String[] args) { Dog d = new Dog(); d.setLegs(4); System.out.println ("A dog has " + d.getLegs() + " legs"); System.out.println ("A dog says " + d.noise()); } public String noise() { return "WOOF!"; }

8 Inheritance Examples

9 Fig. 9.2Inheritance hierarchy for university CommunityMember s. CommunityMember EmployeeStudent StaffFaculty AdministratorTeacher Alumnus An Inheritance (Class) Hierarchy

10 Draw an Inheritance Hierarchy for these classes: Triangle Sphere TwoDimensionalShape Shape Pyramid Square ThreeDimensionalShape Cube Circle Sphere Shape TwoDimensionalShape CircleSquareTriangle ThreeDimensionalShape SphereCubePyramid

11 Try one more! WalkingBird Owl Ostrich Parrot Bird FlyingBird Chicken TalkingParrot Bird WalkingBird OstrichChicken FlyingBird Parrot TalkingParrot Owl

12 Strategy Design classes for objects Identify characteristics classes have in common Abstraction: focus on commonalities among objects in a system Design superclasses to store common characteristics

Bird call: ? color:? food:? movement:? WalkingBird call: ? color:? food:? movement:walk FlyingBird call: ? color:? food:? movement:fly Chicken call: cluck color: white food: bugs Ostrich call: neek-neek color: brown food: grass Parrot call: Squawk color:? food: fruit Owl call: hoo color: brown food:mice TalkingParrot...

14 protected Members Variables or methods available only to derived classes Intermediate level of protection between public and private Accessible to superclass members subclass members class members in the same package Use super. to access a superclass method that has been overridden by a subclass method. Dont use protected instance variables! Fragile software can break if superclass changes

15 protected Members ClassA public m1() private m2() protected m3() ClassB ClassA.m1() ClassC ClassA.m1() ClassA.m3() is-a has-a

16 Constructors in Subclasses Constructors are not inherited! Chain of constructor calls subclass constructor invokes superclass constructor –Implicitly or explicitly –To call explicitly, use super() –Superclass constructor call must be first statement in subclass constructor Object constructor is always fired last Example: Student extends Person

17 Software Engineering with Inheritance Customizing existing software Inherit from existing classes Include additional members Redefine superclass members No direct access to superclass source code Independent software vendors (ISVs) Develop proprietary code for sale/license Users derive new classes –Without accessing ISV proprietary source code

18 Quiz _________ methods are not inherited. The _____ class is at the top of the Java hierarchy. The _____ keyword is used in Java to represent inheritance. A Java class _____ inherit from more than one class. A variable or method that is available only to derived classes is called a ____________. Private/Static/ Constructor Object extends cannot protected member