CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Chapter 1 Inheritance University Of Ha’il.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
Inheritance. Inheritance  New class (derived class) is created from another class (base class).  Ex. EmployeeEmployee  HourlyEmployee  SalariedEmployee.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Polymorphism Briana B. Morrison CSE 1302C Spring 2010.
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 The final Modifier A method marked final indicates that it cannot be overridden with a new definition.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
CS102--Object Oriented Programming Lecture 9: – Polymorphism Copyright © 2008 Xiaoyan Li.
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Computer Science I Inheritance Professor Evan Korth New York University.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Inheritance using Java
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Chapter 8 Polymorphism and Abstract Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Coming up: Inheritance
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Polymorphism and Abstract Classes
Polymorphism 2nd Lecture
Inheritance and Polymorphism
COMP 2710 Software Construction Inheritance
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Road Map Inheritance Class hierarchy Overriding methods Constructors
Comp 249 Programming Methodology
Inheritance 2nd Lecture
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Polymorphism 2nd Lecture
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
Abstract methods and classes
CMSC 202 Inheritance II.
Chapter 8 Abstract Classes.
Chapter 7 Inheritance.
Presentation transcript:

CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li

Question? How do you define a class/what are the components of a class? You want to define class A and B. A has 2 instance variables and 8 methods. B has 4 instance variables and 10 methods. How would you define class A and B if B has all A’s instance variables and methods?

Introduction to Inheritance Inheritance is the process by which a new class is created from another class – The new class is called a derived class – The original class is called the base class A derived class automatically has all the instance variables and methods that the base class has, and it can have additional methods and/or instance variables as well Inheritance is especially advantageous because it allows code to be reused, without having to copy it into the definitions of the derived classes 7-3 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

A Class Hierarchy 7-4 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Classes All employees share certain characteristics in common – All employees have a name and a hire date – The methods for setting and changing names and hire dates would be the same for all employees Some employees have specialized characteristics – Hourly employees are paid an hourly wage, while salaried employees are paid a fixed wage – The methods for calculating wages for these two different groups would be different 7-5 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Classes Within Java, a class called Employee can be defined that includes all employees This class can then be used to define classes for hourly employees and salaried employees – In turn, the HourlyEmployee class can be used to define a PartTimeHourlyEmployee class, and so forth 7-6 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Classes Since an hourly employee is an employee, it is defined as a derived class of the class Employee – A derived class is defined by adding instance variables and methods to an existing class – The existing class that the derived class is built upon is called the base class – The phrase extends BaseClass must be added to the derived class definition: public class HourlyEmployee extends Employee 7-7 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Classes When a derived class is defined, it is said to inherit the instance variables and methods of the base class that it extends – Class Employee defines the instance variables name and hireDate in its class definition – Class HourlyEmployee also has these instance variables, but they are not specified in its class definition – Class HourlyEmployee has additional instance variables wageRate and hours that are specified in its class definition 7-8 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Classes Just as it inherits the instance variables of the class Employee, the class HourlyEmployee inherits all of its methods as well – The class HourlyEmployee inherits the methods getName, getHireDate, setName, and setHireDate from the class Employee – Any object of the class HourlyEmployee can invoke one of these methods, just like any other method 7-9 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Derived Class (Subclass) A derived class, also called a subclass, is defined by starting with another already defined class, called a base class or superclass, and adding (and/or changing) methods, instance variables, and static variables – The derived class inherits all the public methods, all the public and private instance variables, and all the public and private static variables from the base class – The derived class can add more instance variables, static variables, and/or methods – Q: Which components are not inherited from the base class? 7-10 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Inherited Members A derived class automatically has all the instance variables, all the static variables, and all the public methods of the base class – Members from the base class are said to be inherited Definitions for the inherited variables and methods do not appear in the derived class – The code is reused without having to explicitly copy it, unless the creator of the derived class redefines one or more of the base class methods 7-11 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Parent and Child Classes A base class is often called the parent class – A derived class is then called a child class These relationships are often extended such that a class that is a parent of a parent... of another class is called an ancestor class – If class A is an ancestor of class B, then class B can be called a descendent of class A 7-12 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Overriding a Method Definition Although a derived class inherits methods from the base class, it can change or override an inherited method if necessary – In order to override a method definition, a new definition of the method is simply placed in the class definition, just like any other method that is added to the derived class 7-13 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Changing the Return Type of an Overridden Method Ordinarily, the type returned may not be changed when overriding a method However, if it is a class type, then the returned type may be changed to that of any descendent class of the returned type This is known as a covariant return type – Covariant return types are new in Java 5.0; they are not allowed in earlier versions of Java 7-14 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Covariant Return Type Given the following base class: public class BaseClass {... public Employee getSomeone(int someKey)... The following is allowed in Java 5.0: public class DerivedClass extends BaseClass {... public HourlyEmployee getSomeone(int someKey) Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Changing the Access Permission of an Overridden Method The access permission of an overridden method can be changed from private in the base class to public in the derived class However, the access permission of an overridden method can not be changed from public in the base class to a more restricted access permission in the derived class 7-16 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Changing the Access Permission of an Overridden Method Given the following method header in a base case: private void doSomething() Is the following method header valid in a derived class? public void doSomething() Given the following method header in a base case: public void doSomething() Is the following method header valid in a derived class? private void doSomething() 7-17 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Overriding Versus Overloading Do not confuse overriding a method in a derived class with overloading a method name – When a method is overridden, the new method definition given in the derived class has the exact same number and types of parameters as in the base class – When a method in a derived class has a different signature from the method in the base class, that is overloading – Note that when the derived class overloads the original method, it still inherits the original method from the base class as well 7-18 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The final Modifier If the modifier final is placed before the definition of a method, then that method may not be redefined in a derived class It the modifier final is placed before the definition of a class, then that class may not be used as a base class to derive other classes 7-19 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The super Constructor A derived class uses a constructor from the base class to initialize all the data inherited from the base class – In order to invoke a constructor from the base class, it uses a special syntax: public derivedClass(int p1, int p2, double p3) { super(p1, p2); instanceVariable = p3; } – In the above example, super(p1, p2); is a call to the base class constructor 7-20 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The super Constructor A call to the base class constructor can never use the name of the base class, but uses the keyword super instead A call to super must always be the first action taken in a constructor definition An instance variable cannot be used as an argument to super 7-21 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The super Constructor If a derived class constructor does not include an invocation of super, then the no-argument constructor of the base class will automatically be invoked – This can result in an error if the base class has not defined a no-argument constructor Since the inherited instance variables should be initialized, and the base class constructor is designed to do that, then an explicit call to super should always be used 7-22 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

An Object of a Derived Class Has More than one Type. An object of a derived class has the type of the derived class It also has the type of the base class More generally, it has the type of every one of its ancestor classes. Example: – 1. What are the types of an HourlyEmployee object? – 2. What are the types of an Engineer object?

A Class Hierarchy 7-24 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

An Object of a Derived Class Has More than one Type. You can assign an object of a derived class to a variable of any ancestor type. You can plug in a derived class object for a parameter of any of its ancestor types More generally, you can use a derived class object anyplace you can use an object of any of its ancestor types.

The Class SalariedEmployee (a regular constructor) The class Employee has two instance variables – name and hireDate The class SalariedEmployee is a derived class of Employee. It has one additional variable: – salary

The Class SalariedEmployee (a regular constructor) public SalariedEmployee(String theName, Date theDate, double theSalary) { super(theName, theDate); if (salary >=0) { salary = theSalary; }else{ System.out.println(“Fatal error: negative salary!”); System.exit(0); }

The Class SalariedEmployee (a copy constructor) public SalariedEmployee(SalariedEmployee originalObject ) { super(originalObject); salary = originalObject.salary; } Note: An object of the class SalariedEmployee is also an object of the class Empoyee.

The this Constructor Within the definition of a constructor for a class, this can be used as a name for invoking another constructor in the same class – The same restrictions on how to use a call to super apply to the this constructor If it is necessary to include a call to both super and this, the call using this must be made first, and then the constructor that is called must call super as its first action 7-29 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The this Constructor Often, a no-argument constructor uses this to invoke an explicit-value constructor – No-argument constructor (invokes explicit-value constructor using this and default arguments): public ClassName() { this(argument1, argument2); } – Explicit-value constructor (receives default values): public ClassName(type1 param1, type2 param2) {... } 7-30 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

The this Constructor public HourlyEmployee() { this("No name", new Date(), 0, 0); } The above constructor will cause the constructor with the following heading to be invoked: public HourlyEmployee(String theName, Date theDate, double theWageRate, double theHours) 7-31 Copyright © 2008 Pearson Addison-Wesley. All rights reserved

Exercises: 1. Suppose the class named DiscountSale is a derived class of a class called Sale. Suppose the class Sale has instance variables named price and numberOfItems. – Will an object of the class DiscountSale also have instance variables named price and numberOfItems? 2. Suppose the class sale has public methods named getTotal and getTax. – Will an object of the class DiscountSale also have methods named getTotal and getTax? – If so, do these methods have to perform the exact actions in the class DiscountSale as in the class Sale?

Exercises: 3. Suppose the class Sale has a method with the following heading and no other method named getTax: pubic getTax() Suppose the class DiscountSale has a method with the following heading and no other method named getTax: pubic getTax(double rate) How many methods named getTax will the class DiscountSale have and what are their headings?

Announcements Next Lecture: (Wednesday March 5th) – More about inheritance & exercises Next Monday (March 10th) – Review session In-class midterm: – Date: Wednesday (March 12th) – Topics (Ch1 – Ch7)