Inheritance In the real world, objects aren’t usually one-of-a-kind.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
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.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13 - Inheritance.
Modern Programming Tools And Techniques-I
Chapter 11 Inheritance and Polymorphism
CSC 205 Java Programming II
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Continuing Chapter 11 Inheritance and Polymorphism
CSC 205 Java Programming II
Extending Classes.
Lecture 22 Inheritance Richard Gesick.
Polymorphism and access control
Week 6 Object-Oriented Programming (2): Polymorphism
More About Inheritance & Interfaces
Java Inheritance.
CSC 205 – Java Programming II
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Presentation transcript:

Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of vehicles. A car is a special type of vehicle; so is a truck. In Java, relationships such as the one between cars and vehicles are expressed using a feature known as inheritance. Inheritance allows a class to have a “parent” from which it inherits some of its state and some of its behavior.

Advantages of Inheritance There would be no point for a car designer to start from scratch when designing a new car. By assuming that a car has certain components and certain behaviors a car designer can focus on what distinguishes the new model from older ones. Inheritance gives programmers a similar advantage. A new class created from an existing class inherits its variables and methods. The programmer can declare additional variables and methods to make the new class unique.

Class Extension 1/2 In Java, inheritance is accomplished by extending an existing class. Extending a class is done by putting the word extends in a class declaration, followed by the name of the class that’s being extended: Example public class Car extends Vehicle { … } Car is said to be a subclass of Vehicle, and Vehicle is said to be the superclass of Car

Class Extension 2/2 A subclass inherits the variables and methods of its superclass with the exception of constructors, private variables, and private methods The subclass still stores all variables from the superclass including private variables But…the subclass cannot directly access the private variables Inherited variables and methods behave as though they were declared in the subclass. The subclass may define additional variables and methods that were not present in the superclass. A superclass can be any previously existing class, including a class in the Java API.

Writing a Subclass Example - An Account Class public class Account { private double balance; public Account(double initialBalance) { balance = initialBalance; } public Account() { balance = 0.0; public void deposit(double amount) { balance += amount; public void withdraw(double amount) { balance -= amount; public double getBalance() { return balance; public void close() {

Extending Account class The Account class can be extended to create a new SavingsAccount class. If different savings accounts can have different interest rates each SavingsAccount object will need to store an interest rate. public class SavingsAccount extends Account { private double interestRate;   public double getInterestRate() { return interestRate; } public void setInterestRate(double rate) { interestRate = rate;

Properties of the Subclass An instance of a subclass stores all the instance variables of the superclass plus all the instance variables defined in the subclass. Even the superclass’s private variables, which aren’t inherited, are still stored in instances of the subclass. A SavingsAccount object will contain two variables balance interestRate Methods that can be used by SavingsAccount objects: getInterestRate setInterestRate deposit (inherited) withdraw (inherited) getBalance (inherited) close (inherited)

Properties of the Subclass - Example If myAcct is a SavingsAccount variable, the following are all legal: System.out.println("Rate: " + myAcct.getInterestRate()); myAcct.setInterestRate(4.25); myAcct.deposit(500.00); myAcct.withdraw(100.00); System.out.println("Balance: " + myAcct.getBalance()); myAcct.close(); When an instance method is called Java looks first in the class to which the calling object belongs then in the class’s superclass then in the superclass of that class and so on. Example Java first looks for the deposit method in the SavingsAccount class, then in the Account class.

Writing Subclass Constructors 1/2 A subclass doesn’t inherit constructors from its superclass, so it will need its own constructors. The hard part of writing a constructor for a subclass is initializing the variables that belong to the superclass, which are likely to be private. The constructor for the SavingsAccount class will need to initialize both the balance and interestRate variables. A first attempt at writing the constructor: public SavingsAccount(double initialBalance, double initialRate) { balance = initialBalance; // WRONG; balance is private interestRate = initialRate; } This version of the constructor won’t compile, because balance was declared private in the Account class.

Writing Subclass Constructors 1/2 There are two ways to solve this problem: Invoke the superclass constructor Use the protected access modifier instead of private for the balance variable The first technique requires the SavingsAccount constructor to invoke the Account constructor by using the word super Example public SavingsAccount(double initialBalance, double initialRate) { super(initialBalance); // Invoke Account constructor interestRate = initialRate; } super must come first, before the other statements in the body of the subclass constructor

Calling super() from Subclass Constructors Although it is not required to invoke a super constructor from a subclass constructor, If a subclass constructor does not call a super constructor… the compiler will automatically insert super(); at the beginning of the constructor. If a subclass has no constructors at all, the compiler will create a no-arg constructor that contains super(); but no other statements.

When Not to Use Inheritance 1/2 Inheritance is appropriate when the new class “is a” particular case of the old class (the is-a relationship): A Car is a Vehicle. A SavingsAccount is an Account. If the words “is a” don’t fit, then inheritance shouldn’t be used: it wouldn’t make sense to say that a SavingsAccount is a Vehicle. For the is-a relationship to exist the subclass must have every property of the superclass.

When Not to Use Inheritance 2/2 Example Suppose that instances of the Money class represent specific dollar amounts. If Account extends Money, then every Account object would automatically store a monetary amount This seems like a good idea at first glance, but… then Money’s public methods would be inherited by Account making it possible to apply Money operations to Account objects, which doesn’t make sense The is-a rule says that Account shouldn’t extend Money because it’s not true that an Account is a Money. Instead of having Account extend Money, it would be better to declare the type of the balance variable to be Money instead of double. Although the is-a relationship doesn’t hold for Account and Money there’s clearly some connection between the two classes. This is called the has-a relationship because an Account object has a Money object stored within it.

Using the protected Access Modifier The second technique for solving the private variable or method access problem for subclasses is using the protected access modifier. A subclass can use the public variables and methods in its superclass but it has no access to private variables and methods in the superclass. In Java, subclasses can be given access to superclass variables and methods by declaring them to be protected instead of public or private. Using protected provides an intermediate level of access—one that’s more restrictive than public but less restrictive than private

Differences between Access Modifiers A summary of the differences between private, protected, and public when applied to a variable or method in a class is shown below: Access Modifier Meaning private Can be accessed only in the same class protected Can be accessed in the same class or in a subclass public Can be accessed in any class None Can be accessed in any class in the same package A class also has access to the protected variables and methods of all classes in the same package

2nd solution for SavingsAccount constructor access to balance The protected keyword provides the second solution to solve the problem of writing the SavingsAccount constructor declare the balance variable in the Account class to be protected rather than private: Example public class Account { protected double balance; … } The SavingsAccount constructor will now have direct access to balance: public SavingsAccount(double initialBalance, double initialRate) { balance = initialBalance; interestRate = initialRate;

Overriding Overriding is a technique of changing an inherited method in a subclass. An instance method inherited by a subclass may not make sense for that subclass A subclass can override an inherited instance method by supplying a new method provided the new method has the same name the same return type the same number of parameters, and the types of corresponding parameters must be the same. Example Suppose that the Account class has a method named printStatement that prints an account statement. A CheckingAccount class might need a different printStatement method. To override the inherited printStatement method, CheckingAccount would have to have a printStatement method with the same return type and parameters as the one in Account. This is referred to as the method header. Do not confuse this with the method signature which is the header without the return type. The signature is what is used by the runtime to determine which version of the method is being called.

Using super to Call an Overridden Method One tricky situation can arise when writing a subclass method: calling a method that’s inherited from the superclass but overridden in the new class. Calling it the normal way won’t work, because the compiler assumes the call refers to the new version Suppose that the CheckingAccount version of printStatement wants to call the Account version: void printStatement() { printStatement(); // Print regular statement … // Then print list of checks } The compiler will assume that printStatement is calling itself, not the version that was inherited. Adding super to printStatement forces the compiler to use the superclass version of the method: super.printStatement(); // Print regular statement When a method is called using super, Java looks first in the direct superclass. If the method isn’t found there, Java looks in the superclass of that class, and so on.

Polymorphism Inheritance becomes even more potent when combined with another concept: polymorphism. Consider giving instructions to someone to operate a vehicle: Start vehicle Release brake Accelerate Apply brake Stop vehicle These instructions work for any type of vehicle, not just a car The exact meaning of each instruction may vary, depending on the vehicle: For a car, “accelerate” means “press on the accelerator.” For a bicycle, “accelerate” means “turn the pedals faster” In object-oriented programming, polymorphism refers to the ability of different kinds of objects to respond differently to the same commands, provided that the objects belong to classes with a common ancestor.

The Substitution Principle To allow polymorphism, Java has a rule that the author calls the “Substitution Principle” An instance of a subclass can take the place of an instance of any of its superclasses. Although the Substitution Principle may seem odd, there are good reasons for it: If the is-a relationship holds between the subclass and the superclass then the subclass is a more specialized version of the superclass. An object that belongs to the subclass contains at least the same variables (plus possibly some more) as any instance of the superclass. An instance of the subclass responds to the same (public) methods as an instance of the superclass. Example An example that uses the Substitution Principle: Vehicle v = new Car(); Although this statement looks wrong, it is valid as long as Vehicle is a superclass of Car. A car is a vehicle: it has all the properties and behaviors of a vehicle. Some behaviors were inherited and left unchanged, while others were overridden, but—either way—they’re guaranteed to exist.

Substitution Example for Account types Given the Account and SavingsAccount class as defined earlier we can create a SavingsAccount object and store it in an Account variable: Account acct = new SavingsAccount(); How the object is stored new SavingsAccount() creates a SavingsAccount object which contains the inherited balance variable and the interestRate variable, and it is stored in memory as shown below When we assign the SavingsAccount object we created to the Account variable acct, acct now references the SavingsAccount object in memory …but acct is still an Account type, so it can only reference Account variables and methods (that are public)

How acct may be used acct is an Account type but stores a CheckingAccount object The methods you may call and the variables you may access depend on the type of the acct variable (assuming they are public) In this case acct is an Account variable so you may only call methods defined for an Account object However, the method that will run depends on the type of object stored, not the type of the variable name Be careful, this is tricky… If a method is defined in the superclass Account then it may be called But if it is also defined in the subclass CheckingAccount (in other words it is overridden in the subclass) then the CheckingAccount version will be called Example If a method printStatement exists in the Account class and is overridden in the CheckingAccount class then when we make the call: acct.printStatement(); the method that runs will be the CheckingAccount method because the object stored is a CheckingAccount object

A Polymorphic Algorithm in Java A Java version of the algorithm for operating a vehicle: v.start(); v.releaseBrake(); v.accelerate(); v.applyBrake(); v.stop(); It doesn’t matter what v is, as long as it’s some kind of vehicle. This algorithm is polymorphic: it works for a variety of vehicles, not a single kind.

Dynamic Binding In a call such as v.start(), the compiler can’t determine which version of start is being called; instead, v will have to be tested during program execution. This process is known as dynamic binding, because the exact method that’s being called won’t be known until the program is run. If different objects are assigned to v during execution, different versions of start may be called: v = new Car(); v.start(); // Calls start method in Car class v = new Truck(); v.start(); // Calls start method in Truck class

Other Applications of the Substitution Principle The Substitution Principle applies to more than just assignment: A method whose parameter type is a class can be passed an instance of a subclass instead. A method whose result type is a class may return an instance of a subclass instead. The == and != operators can be used to compare references to instances of different classes, provided that one class is a subclass of the other. Example Consider the following method: static void updateAccount(Account acct) { … } Thanks to the Substitution Principle, the argument in a call of updateAccount could be a SavingsAccount object or a CheckingAccount object. As long as updateAccount performs only operations on acct that are valid for any type of account, there won’t be any problems.

Casting Object References Knowing that an object belongs to a particular class doesn’t necessarily make it possible to use the full set of methods defined for that class. Even if acct stores a SavingsAccount object the compiler will allow only the use of Account methods because acct is declared to be of type Account. Casting acct to type SavingsAccount forces compiler to accept acct as a SavingsAccount object. Example Given a statement that casts acct to type SavingsAccount: SavingsAccount myAcct = (SavingsAccount) acct; The myAcct variable can now be used to call creditInterest or any other SavingsAccount method. myAcct.creditInterest(); Be careful when trying to cast an object to a subclass If the object isn’t an instance of the subclass a ClassCastException will be thrown. It’s usually wise to use instanceof to test an object before attempting to cast it

The instanceof Operator Java’s instanceof operator can be used to solve problems such as this. An expression of the form object instanceof class has the value true only if object is an instance of class or one of its subclasses Example If myAcct is an instance of the SavingsAccount class: myAcct instanceof SavingsAccount  true myAcct instanceof Account  true But if acct is an instance of the Account class: acct instanceof SavingsAccount  false because acct is not an instance of the SavingsAccount class… or any subclass of the SavingsAccount class

Advantages of Polymorphism Polymorphism is especially useful when different types of objects are stored in the same data structure. One criteria of good general-purpose code would be that it can process every object in the data structure without checking to see which class it belongs to. Example Given an array of Account objects, we would like to be able to print statements without first checking the type of each account. Advantages of Polymorphism during program maintenance Polymorphism becomes especially important during program maintenance. If a new subclass of Account is created it won't be necessary to go through the entire program to see how this change might affect code that performs operations on accounts. If an existing subclass of Account is removed most of the program should be unaffected.

The Object Class Every class – with the exception of a special class named Object – is required to have a superclass. If no superclass is specified in the declaration of a new class… Java uses Object as the default superclass. Because of this rule, all classes (other than Object itself) have Object as a superclass, either directly or indirectly Java’s classes belong to a single “family tree,” known as a class hierarchy The existence of a single class hierarchy has some important consequences: Every class (other than Object itself) inherits methods from the Object class. A variable of type Object can store a reference to any object whatsoever. A method with an Object parameter will accept any object as its argument.

Object Methods A partial list of methods in the Object class: clone() - Returns a copy of this object. equals(obj) - Indicates whether the object obj is “equal” to this object. toString() - Returns a string representation of this object. These methods are inherited by the subclasses of Object so that every class in Java has these methods. These methods are frequently overridden.

The equals Method 1/2 The equals method is provided to solve the problem of testing whether two objects are equal. equals has one parameter, an arbitrary object, and returns a boolean result: public boolean equals(Object obj) { … } By default, the equals method behaves like the == operator. The call x.equals(y) returns true only if x and y refer to the same object. If the default behavior of the equals method is unsatisfactory, a class can override the method.

The equals Method 2/2 Example An equals method for the Fraction class: public boolean equals(Object obj) { if (!(obj instanceof Fraction)) return false; Fraction f = (Fraction) obj; return ( numerator == f.numerator && denominator == f.denominator ); } Given if (f1.equals(f2)) … The inherited version of equals (from the Object class) would test whether f1 and f2 refer to the same object The new version will test whether f1 and f2 have matching numerators and denominators. It’s not necessary to provide equality testing for every class: Equality testing may not be meaningful for a class. It may not be worthwhile to take the time to write the equals method.

The toString Method Because toString is declared in the Object class all classes will have a toString method. The print and println methods are overloaded with one version of each method requiring an Object parameter When supplied with an object as its argument, print (or println) calls the toString method that belongs to the object, and prints the string returned by toString. If an object belongs to a class that doesn’t have a toString method print will use an inherited version of toString. The version of toString in the Object class returns a string containing the name of the object’s class, the @ character, and a hexadecimal number (the object’s “hash code”). If the Fraction class didn’t contain a toString method printing a fraction would cause Object’s version of toString to invoked and this is returned  Fraction@1cc78a This information isn’t very useful, so it’s a good idea for most classes to provide their own toString method.