Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13  Inheritance and Polymorphism  Access Modifiers  Abstract.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Abstract Superclasses and Abstract Methods When.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Method signature Name and parameter list public static void test() public static int test() => Syntax error, Duplicate method test You cannot declare more.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 13 Inheritance and Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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 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.
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.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Module 9. Dealing with Generalization Course: Refactoring.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Inheritance ITI1121 Nour El Kadri.
Objects as a programming concept
Inheritance and Polymorphism
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
Inheritance and Polymorphism
Java – Inheritance.
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
CIS 199 Final Review.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common features. First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class The common class is called the superclass and all classes that inherit from it subclasses. The superclass is an ancestor of the descendant subclass. We did not distinguish the savings and checking accounts in designing the Account class. If these two types of accounts are treated in the same way, then only one class to model them is enough. However, if we have to keep track of different information or process the data differently for the savings and checking accounts, using one class to model both is not appropriate. Defining two distinct classes, for example, Savings Account and Checking Account, is not appropriate because we would end up duplicating methods. For example, if the way we deduct the amount of a withdrawal from a savings account and the check amount from a checking account is the same, then we will have to define identical methods for both classes, duplicating the same code. We avoid the problems by using inheritance.

Sample Inheritance Hierarchy Account Savings Checking Interest Bearing ATM Checking SuperSaver Regular Student

Inheritance Subclasses inherit from their superclass Everything that's in the superclass is inherited by the subclass. Objects created from the subclass consist of the inherited components and any new components defined in the subclass If a subclass defined something that is in the superclass, the subclass elements override the superclass elements Constructors (and other things – see later) of the superclass can be called using super() We did not distinguish the savings and checking accounts in designing the Account class. If these two types of accounts are treated in the same way, then only one class to model them is enough. However, if we have to keep track of different information or process the data differently for the savings and checking accounts, using one class to model both is not appropriate. Defining two distinct classes, for example, Savings Account and Checking Account, is not appropriate because we would end up duplicating methods. For example, if the way we deduct the amount of a withdrawal from a savings account and the check amount from a checking account is the same, then we will have to define identical methods for both classes, duplicating the same code. We avoid the problems by using inheritance.

Example Program Ding … out to reality … ColdMedicine.java, ColdPills.java, ColdSyrup.java, UseColdMedicine.java

Inheritance and Member Accessibility Superclass Subclass Which members of a superclass are accessible from its subclasses? There is a third visibility modifier called protected. public private protected

Access from the Outside Only the public elements are accessible from the outside objects. mySub Subclass Superclass mySuper Client test

Access from the Subclass Methods Subclass methods can access public and protected superclass methods and data. Subclass methods cannot access private superclass methods and data. mySub Subclass Superclass accessible inaccessible

Accessing Superclass Data and Methods Constructors of the superclass can be called using super() Public and protected components of the superclass that have been overridden in a subclass can be accessed using super.<the component> Note: Make sure that the superclass has a constructor that takes no arguments. If the superclass does not have one, then the call super( ) from the subclass becomes invalid. To call a constructor that takes one or more arguments, you must explicitly call it from a subclass constructor, such as public Truck( int weightLimit, String vin ) { super( vin ); cargoWeightLimit = weightLimit; }

Example Program Dong … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent1.java Dung … out to reality … HouseOccupant.java, Human.java, Pet.java, UseHouseOccupant.java

Polymorphism Polymorphism allows a variable to refer to objects from different (but related by inheritance) classes. References to data or methods are correctly resolved according to the object’s class. Requires that the superclass have the inherited data or method Student student; student = new GraduateStudent(); student.computeGrade( ); This will call the method of GraduateStudent Student student; student = new UndergraduateStudent(); student.computeGrade( ); This will call the method of UndergraduateStudent

Creating the roster Array roster is an array of Student objects. Student roster[ ] = new Student[40]; roster[0] = new GraduateStudent( ); roster[1] = new UndergraduateStudent( ); roster[2] = new UndergraduateStudent( ); roster[3] = new GraduateStudent( ); Create instances of the subclasses of Student. 1 2 3 4 36 37 38 39 roster Graduate- Student Under- graduate- Student

Example Programs Dung … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent2.java

Abstract Classes Often we want to place elements common to all subclasses in their superclass. But we do not want any instances to be created from the superclass. In such case, we designate the superclass as an abstract class. This is also a way to enforce existence of methods in subclasses.

Abstract Methods It is common for an abstract class to include an abstract method that must be implemented by the descendant classes. If a class includes an abstract method, then it is considered as an abstract class and must have the abstract modifier in the class declaration.

Sample Abstract Class abstract class Student { . . . abstract public void computeGrade( ); } Reserved word abstract in the class declaration. Abstract method has no method body. class GraduateStudent extends Student { . . . public void computeGrade( ) //method body comes here } Abstract method is must be fully implemented in the descendant class.

Example Programs Deng … out to reality … H2O.java, Ice.java, Water.java, Steam.java, UseH2O.java