 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)

Slides:



Advertisements
Similar presentations
1 Inheritance Classes and Subclasses Or Extending a Class.
Advertisements

Object Oriented Programming with Java
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
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.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 9: Inheritance and Interfaces.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
COMP201 Java Programming Topic 4: Inheritance Readings: Chapter 5.
Chapter 10: Inheritance and Polymorphism
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
COMP201 Java Programming Topic 4: Inheritance Readings: Chapter 5.
1 Introduction to Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding.
Computer Science I Inheritance Professor Evan Korth New York University.
CS 61B Data Structures and Programming Methodology July David Sun.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Polymorphism &Virtual Functions
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
A class in Java is a software construct that includes fields (also called data fields or class scope variables) to provide data specification, and methods.
What is inheritance? It is the ability to create a new class from an existing class.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
1 Building Java Programs Chapter 9: Inheritance and Interfaces These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
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
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Topics Inheritance introduction
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
BY:- TOPS Technologies
Modern Programming Tools And Techniques-I
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Lecture 15: More Inheritance
Extending Classes.
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism CT1513.
Advanced Programming Behnam Hatami Fall 2017.
Law firm employee analogy
Inheritance.
Lecture 15: Inheritance II
Building Java Programs
Chapter 6 Inheritance.
Chapter 7 Inheritance.
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived) classes are created by absorbing the methods and variables of an existing (base) class –It then adds its own methods to enhance its capabilities –“Is a” relationship: derived class object can be treated as base class object –“Has a” relationship: class object has object references as members –A derived class can only access non-private base class members unless it inherits accessor functions –Constructors are not inherited

 2002 Prentice Hall. All rights reserved. 2 Base Classes and Derived Classes To specify that class Two is derived from class One class Two extends One or class Circle extends Shape { {

 2002 Prentice Hall. All rights reserved. 3 Members All inheritance in Java is public inheritance –Can be accessed by base class or any class derived from that base class –No analog to C++ features of private and protected Definitions –super, base, parent class –child, sub-, derived class

 2002 Prentice Hall. All rights reserved. 4 Base Classes and Derived Classes Inheritance forms a tree-like hierarchy Inheritance hierarchy for university CommunityMember s. CommunityMember EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

 2002 Prentice Hall. All rights reserved. 5 Relationship between Base Classes and Derived Classes Text’s Example: –class Employee (parent class) –class Manager (child class)

 2002 Prentice Hall. All rights reserved. Outline 6 class Employee { public Employee(String n, double s, int year, int month, int day) { … } public String getName() { … } public double getSalary() { … } public Date getHireDay() { … } public void raiseSalary(double byPercent) { … } private String name; private double salary; private Date hireDay; }

 2002 Prentice Hall. All rights reserved. Outline 7 class Manager extends Employee { //added methods here //redefined methods here private double bonus; } public double getSalary() { return salary + bonus; //won’t work } public double getSalary() { double baseSalary = getSalary(); //still won’t work return baseSalary + bonus; } Manager class has no direct access to private fields of superclass Calls itself – infinite recursion

 2002 Prentice Hall. All rights reserved. Outline 8 class Manager extends Employee { n the employee's s the year the hire month the hire day the hire day */ public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus = 0; } public double getSalary() { double baseSalary = super.getSalary(); return baseSalary + bonus; } public void setBonus(double b) { bonus = b; } private double bonus; } Manager inherits from Employee Calls the constructor for the employee class Calls the getSalary method for the employee class

 2002 Prentice Hall. All rights reserved. Outline 9 // construct a Manager object Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15); boss.setBonus(5000); Employee[] staff = new Employee[3]; // fill the staff array with Manager and Employee objects staff[0] = boss; staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15); // print out information about all Employee objects for (Employee e : staff) System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); Create a new manager and set bonus Create an array of parent class Employee Fill array with employees and manager foreach loopWhich getSalary( ) will it call?

 2002 Prentice Hall. All rights reserved. Outline 10 Carl’s salary is = 85000

 2002 Prentice Hall. All rights reserved. 11 Polymorphism The fact that an object variable (such as e) can refer to multiple actual types is called polymorphism. Automatically selecting the appropriate methods at run time is called dynamic binding. Note: In Java, you do not need to declare a method as virtual. Dynamic binding is the default behavior. If you do not want a method to be virtual, you tag it as final.

 2002 Prentice Hall. All rights reserved. 12 Preventing Inheritance: Final Methods class Employee { … public final String getName( ) { return name; } … }

 2002 Prentice Hall. All rights reserved. 13 Inheritance Inheritance does not have to stop at one layer of classes. Could have an Executive class that extends Manager. Path from a particular class to its ancestor is the inheritance chain. May be more than one chain of descent. For example, a subclass Programmer and Secretary that extends Employee. Java does not support multiple inheritance. (Has interface classes, though.)

 2002 Prentice Hall. All rights reserved. 14 Polymorphism A simple rule for inheritance ‘is-a’ rule. –subclass ‘is-an’ object of the superclass –every manager ‘is-an’ employee The opposite is not true. Another way of formulating rule is substitution principle. –You can use a subclass object whenever the program expects a superclass object. –Already did this in last example.

 2002 Prentice Hall. All rights reserved. 15 Polymorphism Last example. –staff[0] and boss refer to the same object. –staff[0 ] is only considered to be an Employee object by compiler. –That means you can call boss.setBonus (5000); //ok –but you can’t call staff[0].setBonus(5000); //error

 2002 Prentice Hall. All rights reserved. 16 Preventing Inheritance: Final Classes final class Executive extends Manager { … }