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:

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Inheritance Inheritance Reserved word protected Reserved word super
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
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.
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
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.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
ISE 582: Web Technology for Industrial Engineers University of Southern California Department of Industrial and Systems Engineering Lecture 4 JAVA Cup.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Java Programming Review (Part I) Enterprise Systems Programming.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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.
Introduction to Object-Oriented Programming
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Programming Languages and Paradigms Programming C++ Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Data Structures and Java CS /14/2015 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6:
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
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 and Polymorphism
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Inheritance and Polymorphism
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Inheritance in Java.
Computing with C# and the .NET Framework
CSC 205 Java Programming II
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Final and Abstract Classes
Presentation transcript:

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 Slide 2 Inheritance Inheritance: an object-oriented programming language feature that allows for the definition of a class in terms of another class Another example of a class relationship (besides Aggregation and Association) In Java, use the extends keyword Promotes reusability of existing code

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 3 Example: CheckingAccount Suppose we define CheckingAccount from scratch Attributes balance number of checks drawn Methods deposit withdraw get balance draw a check …others CheckingAccount int balance int numChecks int getBalance() void deposit( int amount ) void withdraw( int amount ) void drawCheck( int amount ) …others

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 4 Example: CheckingAccount Resulting class is very similar to BankAccount The same as BankAccount except for an additional field and some methods Better to extend BankAccount instead

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 5 BankAccount revisited public class BankAccount { private int balance = 0; public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; }

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 6 public class CheckingAccount { private int balance = 0; private int numChecks = 0; public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public void drawCheck( int amount ) { balance = balance - amount; // or, withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; } CheckingAccount.java Just like BankAccount except for the code in bold

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 7 public class CheckingAccount extends BankAccount { private int numChecks = 0; public void drawCheck( int amount ) { withdraw( amount ); // can’t do balance = balance – amount; // because balance is private to BankAccount numChecks++; } public int numChecksDrawn() { return numChecks; } CheckingAccount.java Using extends Notice that (public) methods defined in BankAccount (e.g., withdraw) can be used within CheckingAccount

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 8 Using CheckingAccount objects CheckingAccount mary = new CheckingAccount(); mary.deposit( 1000 ); System.out.println( “Balance: ” + mary.getBalance() ); mary.drawCheck( 100 ); System.out.println( “Balance: ” + mary.getBalance() ); System.out.println( “Checks Drawn: ” + mary.numChecksDrawn() ); Can call methods defined in BankAccount … and methods defined in CheckingAccount

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 9 The inheritance relationship Use inheritance for “is-a” relationships Examples A checking account is a bank account A manager is an employee A graduate student is a student A circle is a shape Checking Account Bank Account Diagramming notation:

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 10 Some terminology CheckingAccount is a subclass of BankAccount BankAccount is a superclass Inheritance relationships result in a class hierarchy Circle Shape Square Rectangle

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 11 Method overriding Suppose BankAccount has a showStatus() method defined as follows: public void showStatus() { System.out.println( “Balance is ” + balance ); } CheckingAccount can redefine or override showStatus(): public void showStatus() { System.out.println( “Balance is ” + getBalance() ); System.out.println( “Checks issued:” + numChecksDrawn() ); } Calling showStatus() on a CheckingAccount object invokes the appropriate method

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 12 Superclass variables for subclass objects Checking accounts are bank accounts so it is possible to have a BankAccount variable point to a CheckingAccount object But not the other way around Superclass variables can refer to subclass objects, not vice-versa  BankAccount b1 = new CheckingAccount(); (note: only methods indicated in BankAccount may be invoked through b1)  CheckingAccount b2 = new BankAccount(); (not allowed because a bank account is not necessarily a checking account)

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 13 Variable vs object examples Consider the following declarations BankAccount b = new BankAccount(); - valid CheckingAccount c = new CheckingAccount(); - valid BankAccount d = new CheckingAccount(); - valid CheckingAccount f = new BankAccount(); - invalid! Which methods will the following calls invoke? b.showStatus( );- BankAccount’s c.withdraw( 100 );- BankAccount’s c.showStatus( );- CheckingAccount’s c.drawCheck( 100 );- CheckingAccount’s d.withdraw( 100 );- BankAccount’s d.showStatus( );- CheckingAccount’s d.drawCheck();- invalid! Dynamic binding: the overriden showStatus() method will be called Because there is no drawCheck() method in BankAccount

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 14 Object arrays and inheritance Suppose Employee is a superclass and Manager and Secretary are subclasses of Employee An Employee array can have its elements refer to different kinds of objects Can use a for-statement to call the same method on the different objects Allows the program to view the objects in a uniform way

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 15 Object arrays and inheritance null Employee[] emps; for ( int i = 0; i < 5; i++ ) { emps[i].increaseSalary( ); } null emps Manager object Employee object Secretary Object Manager object Secretary Object

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 16 Inheritance and constructors public class BankAccount { private int balance; public BankAccount() { balance = 0; } public BankAccount( int initBal ) { balance = initBal; } public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public class CheckingAccount extends BankAccount { private int numChecks; public CheckingAccount() { numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; } CheckingAccount c = new CheckingAccount(); Which of the constructors are called?

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 17 Inheritance and constructors CheckingAccount = new CheckingAccount(); In the above statement, CheckingAccount’s (default) constructor is called Since CheckingAccount is a BankAccount, a BankAccount constructor should also be called Which one? Answer: the default constructor Note that BankAccount() is called before CheckingAccount() What if we want a particular constructor of a superclass called?

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 18 Incorrect attempt public class CheckingAccount extends BankAccount { private int numChecks; public CheckingAccount() { numChecks = 0; } public CheckingAccount( int startBal ) { numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; } We want CheckingAccount c = new CheckingAccount( 1000 ); to create an account with an initial balance of 1000 This will still call BankAccount( ), not BankAccount( 1000 )

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 19 Using super() public class CheckingAccount extends BankAccount { private int numChecks; public CheckingAccount() { numChecks = 0; } public CheckingAccount( int startBal ) { super( startBal ); numChecks = 0; } public void drawCheck( int amount ) { withdraw( amount ); numChecks++; } public int numChecksDrawn() { return numChecks; } super( … ) indicates which superclass constructor will be called If not indicated, it defaults to super( ) with no parameters Call to super(…) should be the first line in the subclass’ constructor Implicitly calls “super();” or BankAccount( ) Calls a particular constructor BankAccount( int )

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 20 Protected access protected: another access modifier besides private and public If indicated in a superclass, protected means that the field or method is visible in subclasses but not visible elsewhere Example: in BankAccount, if balance attribute was protected instead of private, then CheckingAccount can access balance directly But outside classes still cannot access balance

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 21 public class BankAccount { protected int balance = 0; public int getBalance() { return balance; } public void deposit( int amount ) { balance = balance + amount; } public void withdraw( int amount ) { balance = balance - amount; } public class CheckingAccount extends BankAccount { private int numChecks = 0; public void drawCheck( int amount ) { balance = balance – amount; // above statement is now possible // because balance is protected in BankAccount numChecks++; } public int numChecksDrawn() { return numChecks; } public class AnotherClass {... CheckingAccount c = new CheckingAccount(); c.deposit( 1000 ); // allowed because these c.drawCheck( 100 ); // methods are public c.balance = ; // not allowed because variable is protected c.numChecks = 0; // not allowed because variable is private... }

6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16: Inheritance Slide 22 Summary Use inheritance for similar types of objects Common characteristics and behavior are placed in the superclass Subclasses share or inherit superclass’ characteristics and behavior Use protected modifier to get direct access Behavior can be overridden Use super(…) to ensure the correct superclass constructor is called In Java there are other options To be taken up in CS 21b Abstract classes, Interfaces and implements