Java Unit 11: Inheritance I

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
OOP: Inheritance By: Lamiaa Said.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CSE 115 Week 10 March , Announcements March 21 – Lab 7 Q & A in lecture March 21 – Lab 7 Q & A in lecture March 26 – Exam 7 March 26 – Exam.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Chapter 10 Classes Continued
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Computer Science I Inheritance Professor Evan Korth New York University.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 8 More Object Concepts
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
4.1 Instance Variables, Constructors, and Methods.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Object-Oriented Programming (Java), Unit 12 Kirk Scott.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance Chapter 11 in Gaddis. Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java Unit 11: Inheritance I
Summary prepared by Kirk Scott
Class Inheritance Part I
Sections Inheritance and Abstract Classes
Java Unit 11: Inheritance I
Object-Oriented Programming (Java), Unit 12
Object-Oriented Programming
Inheritance and Polymorphism
Inheritance in Java.
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Object Oriented Programming
Chapter 9 Inheritance and Polymorphism
Comp 249 Programming Methodology
Chapter 9 Object-Oriented Programming: Inheritance
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Java – Inheritance.
Java Inheritance.
Chapter 10: Method Overriding and method Overloading
Fundaments of Game Design
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Review of Previous Lesson
Review of Previous Lesson
Building Java Programs
Method Overriding and method Overloading
Chapter 11 Inheritance and Polymorphism Part 1
Final and Abstract Classes
Presentation transcript:

Java Unit 11: Inheritance I The Keyword ‘super’, Methods, and Construction

Keyword ‘super’ You might have observed in the previous section that in overriding the getPrice() method, the majority of the code simply duplicated the logic of the method being overridden. It was necessary to get the price including the markup and then apply the tax rate to that. The following example shows how it is possible to make use of the method as defined above it in the hierarchy in order to implement the overriding method lower down in the hierarchy:

Keyword ‘super’ public double getPrice() { double price; price = super.getPrice(); price = price + price * mTaxRate; return price; }

Keyword ‘super’ At execution time the key word super signals that the call to getPrice() is not a recursive call. The system looks in the hierarchy for the nearest class above the current class where getPrice() is implemented, and uses the version found there.

Keyword ‘super’ and Constructors The keyword super also arises when writing subclass constructors which take parameters. The reason that this topic is complex is that constructors in a subclass also do not have direct access to the instance variables inherited from the superclass. In other words, without new syntax it is not possible to write a constructor for the TaxedFoodV1 class which would have parameters for the wholesaleCost and markup and successfully use those parameters to initialize the inherited instance variables.

Default Constructor It is possible to write classes with no constructors at all. If this is done, the system automatically provides default constructors which take no parameters. It is also possible to do as done in the example code so far, namely write constructors which take no parameters and contain no code, and thus rely on default initialization. The effect is no different from providing no constructors at all, but it is less cryptic. It is important to remember that as soon as the programmer writes any constructor for a class other than a default constructor, then the system does not provide a default constructor. At that point, if a default constructor is desired, the programmer has to provide it.

‘super’ and Constructors So far, the example has made use of default construction. Default subclass constructors rely on the existence of a default constructor in the superclass. Default initialization of instance variables, including inherited ones, is handled automatically. After construction, it is possible to use set methods to store non-default values in the instance variables, if desired. The example will now be extended to illustrate construction with parameters.

‘super’ and Constructors The modifications are significant enough that the classes in this section will be named with V2 at the end. Example code will be given for various pieces of code for this version. In order to save space, a complete set of code will not be given. In the next section more modifications will be introduced and a complete set of code will be given there.

‘super’ and Constructors Here is a constructor for FoodV2, which takes a full set of parameters for its instance variables: public FoodV2(String nameIn, double wholesaleCostIn, double markupIn) { mName = nameIn; mWholesaleCost = wholesaleCostIn; mMarkup = markupIn; }

‘super’ and Constructors Then this is the correct form for a constructor in the TaxedFoodV2 class below it that takes a full set of parameters, including parameters for the inherited variables: public TaxedFoodV2(String nameIn, double wholesaleCostIn, double markupIn, double taxRateIn) { super(nameIn, wholesaleCostIn, markupIn); taxRate = taxRateIn; }

‘super’ and Constructors When the system encounters a call to super() in a constructor it looks for a constructor above the current class in the hierarchy. It will use the nearest constructor in the hierarchy above it which has a parameter list which matches the parameters used in the call. If you explicitly call super() in a subclass constructor, this call has to be the first line of code. If you don’t explicitly call super(), then you are relying on the default constructor in the superclass.

‘super’ and Constructors Keep in mind that if the superclass contains any other constructor, then it becomes the responsibility of the programmer to provide the needed default constructor. In other words, you will get a compiler error under the following scenario: The superclass contains a constructor that takes parameters, but not an explicit default constructor; the subclass contains a constructor that depends on the existence of a default constructor above it. Typically, such a constructor would be a default constructor. In general, it is a constructor which does not call super().