Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
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.
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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Computer Science and Engineering College of Engineering The Ohio State University Lot More Inheritance and Intro to Design Patterns Lecture 12.
What is inheritance? It is the ability to create a new class from an existing class.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
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.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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:
 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 Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
1 Principles of Computer Science II Prof. Nadeem Abdul Hamid CSC 121 – Spring 2006 Lecture Unit 4 - Inheritance.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Chapter 13 - Inheritance.
Data Structures and Algorithms revision
Inheritance In the real world, objects aren’t usually one-of-a-kind.
Lecture 12 Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Computing with C# and the .NET Framework
CSC 205 Java Programming II
Inheritance, Polymorphism, and Interfaces. Oh My
Polymorphism and access control
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Adapted from Java Concepts Companion Slides
Introduction to Inheritance
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance

Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship exists between the classes Inheritance: A class inherits the attributes of another class –An “is a” relationship exists between the classes class Student extends Person //is a { private Grade grade; // has a private String major; private Transcript courses; // why not name? … }

Inheriting from Applet class SelfPortrait extends Applet Applet is a superclass of SelfPortrait (also base class for SelfPortrait) SelfPortrait is a subclass of Applet (also derived class from Applet) SelfPortrait may selectively override Applet’s methods, e.g., init(), paint() because SelfPortrait is an Applet, the browser knows these methods exist polymorphism: the subclass method is called if the object actually is the subclass, even if it is referenced by a variable of the superclass

super Keyword A subclass can invoke the constructors and methods of its superclass to invoke a superclass constructor from the subclass constructor: super (arguments); // must be first executable stmt. to invoke a superclass method: super.method (arguments);

BankAccount class public class BankAccount { private double balance; public BankAccount (double initialBal) { balance = initialBal; } public double getBalance() { return balance; } public void deposit (double amount) { balance += amount; } public void withdraw (double amount) { balance -= amount; }

Inheriting from BankAccount The BankAccount class has methods deposit(double), withdraw(double), & double getBalance() a CheckingAccount class would want the withdraw() method to charge a fee a SavingsAccount class would also need an addInterest() method a CDAccount would be a SavingsAccount whose withdraw method would charge a penalty for early withdrawal

CheckingAccount public class CheckingAccount extends BankAccount { public CheckingAccount (double initialBal) { super (initialBal); } public void withdraw (double amt) { final double FEE =.10; super.withdraw (amt + FEE);// super req’d }

SavingsAccount public class SavingsAccount extends BankAccount { private double interest; public SavingsAccount (double initialBal, double rate) { super (initialBal); interest = rate; } public void addInterest () { // super not required, no conflict deposit (interest * getBalance()); }

Certificate of DepositAccount public class CDAccount extends SavingsAccount { private int term; public CDAccount (double initialBal, double rate, int months){ super (initialBal, rate); term = months; } public void withdraw (double amt){ double penalty = 0.; int duration = …; if (duration < term){ penalty = 200; } super.withdraw (amt + penalty); }

Polymorphism The ability to reference an object of some subclass via a variable of one of its superclasses, and to have method invocations on that variable use the subclass’s version of an overridden method At runtime, the java interpreter looks at the actual object in memory, determines its class, and invokes the method (if any) associated with that class E.g., BankAccount acct = new CheckingAccount(2000); acct.withdraw (500); // calls CA’s withdraw

Polymorphism (cont.) BankAccount[] accts = new BankAccount[3]; accts[0] = new CheckingAccount(1000); accts[1] = new SavingsAccount(1000,.04); accts[2] = new CDAccount(1000,.065, 12); for (int j=0; j< 3; j++) { accts[j].deposit (1000); accts[j].withdraw (500); //accts[j].addInterest(); won’t compile if (accts[j] instanceof SavingsAccount) { ((SavingsAccount)accts[j]).addInterest(); }

Polymorphism (cont.) polymorphism applies to methods only, not variables variables are referenced according to the declared type of the object, not the actual type class Base { int data; // note: not private... } class Sub extends Base { int data; // shadows Base’s data... } class Test { Base b = new Sub(); b.data = 6; // Base’s version of data Sub s = new Sub(); s.data = 7; // Sub’s version of data

Methods of the Object Class Object is the ultimate superclass the Object class provides some methods which are intended to be overridden –public String toString() // returns string representation –public boolean equals (Object o) // compares objects

The toString() Method public String toString() returns a string representation of an object toString() is called automatically –in string concatenation: BankAccount acct = new BankAccount(); String s = “My account is “ + acct; –by the println method: System.out.println (acct); if not overridden:

The toString() Method(cont.) Override to print instance data public class BankAccount { … public String toString() { return “BankAccount[balance=“ + balance + “]”; }

The equals() Method public boolean equals (Object o) compares this object with the given object if not overridden, an == comparison of the object references is made this is probably not what you want override to compare some or all of the instance data

The equals() Method(cont.) public class BankAccount { private double balance;... public boolean equals (Object o) { if (!(o instanceof BankAccount)) return false; BankAccount other = (BankAccount)o; if (balance == other.balance) { return true; } return false; } Our object's methods can access the private data of another object of the same class

Variable accessibility declaring a variable private makes it available to methods of its own class only it does not include subclasses declaring a variable without an access modifier makes it available to methods of other classes in the same package declaring a variable protected makes it available to methods of subclasses and other classes in the same package declaring a variable public makes it available to methods of any class anywhere

Superclass Instance Variables public class SavingsAccount extends BankAccount { private double interest; // remember: private double balance; in BA... public boolean equals (Object o) { if (!(o instanceof SavingsAccount)) return false; SavingsAccount other = (SavingsAccount)o; if (balance == other.balance && interest == other.interest) { return true; } return false; } } // balance can’t be accessed by subclass if private