Inheritance in Java.

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

Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
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.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
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 =
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.
The child gets it all..  Factor out common behavior  parent class implements behavior needed by children  guarantee that all subclasses have the characteristics.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
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.
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)
Inheritance Motivation –Code reuse –Conceptual modeling.
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CSE 501N Fall ‘09 15: Polymorphism October 22, 2009 Nick Leidenfrost.
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.
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.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
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:
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
 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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
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.
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.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Lecture Notes – Inheritance (Ch 9-10)
Implementing Classes Yonglei Tao.
Computing with C# and the .NET Framework
Subclasses Chapter 11 Copyright © 2000 W. W. Norton & Company.
Packages, Interfaces & Exception Handling
CSC 205 Java Programming II
Chapter 10 – Inheritance Big Java by Cay Horstmann
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Polymorphism and access control
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Interfaces in Java.
Advanced Java Programming
Java Inheritance.
CSC 205 – Java Programming II
Adapted from Java Concepts Companion Slides
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

Inheritance in Java

What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining new classes, which build on the function-ality of existing classes DCS – SWC

What is inheritance (in Java)? Suppose we have a BankAccount class, which provides basic functionality common for all types of bank accounts Depositing money Withdrawing money Retrieving the balance But most bank accounts have more functionality that just this… DCS – SWC

What is inheritance (in Java)? Bank Account Checking Account Monthly fee Transaction fee Savings Account Monthly interest Upper balance limit DCS – SWC

What is inheritance (in Java)? BankAccount - balance + deposit + withdraw + getBalance() CheckingAccount - chargeMonthlyFee() - chargeTransFee() SavingsAccount - rate - depositInterest() - checkBalanceLimit() DCS – SWC

Inheritance in code public class SavingsAccount extends BankAccount { private double rate; public SavingsAccount(double rate) {...} public void depositInterest() {...} public bool checkBalanceLimit() {...} } DCS – SWC

Inheritance We only need to define the new methods and instance fields for SavingsAccount Methods and instance fields from BankAccount are inherited We extend the BankAccount class without touching it (code reuse) ”Closed for modification, open for extension” DCS – SWC

Inheritance Superclass Subclass BankAccount - balance + deposit + withdraw + getBalance() SavingsAccount Subclass - rate - depositInterest() - checkBalanceLimit() DCS – SWC

Inheritance Why is the class with most functionality called the subclass? Terminology from set theory BankAccount Checking Account Savings Account DCS – SWC

Inheritance vs. Interfaces Related, but not the same If you must implement an interface, you are ”ordered” to implement a set of certain methods If you extend a class, you get something ”for free” DCS – SWC

Inheritance vs. Interfaces One quite important difference between inheritance and interfaces: A class can implement multiple interfaces A class can only extend one class There is no such thing as multiple inheritance in Java… Multiple inheritance has issues, taken out of Java to keep things simple DCS – SWC

Inheriting methods A subclass has three options when defining methods: Inherit methods as-is Override methods Define new methods (just as we are used to) DCS – SWC

Inheriting methods Inherit methods as-is Just as it sounds – the method will work exactly as it works in the superclass Methods can (still) be applied to objects of the superclass, and also to objects of the subclass DCS – SWC

Inheriting methods Override methods We can actually provide a different imple-mentation of a method from the superclass Superclass may provide a reasonable default implementation Subclasses may substitute it for a more useful implementation DCS – SWC

Inheriting methods public class DefaultShape { public void draw() { // do nothing } public double getArea() { return 0.0;} } DCS – SWC

Inheriting methods public class Circle extends DefaultShape { // new instance fields, etc. public void draw() // Override // code for drawing a Circle } public double getArea() // Override return (radius*radius*Math.PI); DCS – SWC

Inheriting methods public class Point extends DefaultShape { // new instance fields, etc. public void draw() // Override // code for drawing a Point } // However, I keep implementation of getArea DCS – SWC

Inheriting methods DefaultShape ds = new DefaultShape(); double area1 = ds.getArea(); Circle c = new Circle(1,1,1); double area2 = c.getArea(); Point p = new Point(1,1); double area3 = p.getArea(); DefaultShape dsc = new Circle(2,2,2); double area4 = ds.getArea(); DCS – SWC

Inheriting methods ”In Java, method calls are always determined by the type of the actual object, not the type of the object reference” This is polymorphism DCS – SWC

Inheriting methods What if we want to ”supple-ment” a method, not override it completely Do we need to implement all of the code in the super-class again? No – and it would break encapsulation! DCS – SWC

Inheriting methods Ooops! // Original method from BankAccount public void deposit(double amount) { balance = balance + amount; } // Overridden method in CheckingAccount transactionCount++; Ooops! DCS – SWC

Inheriting methods New keyword! // Overridden method in CheckingAccount public void deposit(double amount) { super.deposit(amount); transactionCount++; } ”Do what the superclass does in deposit, then do my stuff” New keyword! DCS – SWC

Inheriting methods We can also do this for constructors! // Constructor for subclass public CheckingAccount(double initialBalance) { super(initialBalance); // Now do initialisation specific for // the CheckingAccount class transactionCount = 0; } DCS – SWC

Conversions Rules for conversion between super- and sub-class are similar to conversion rules between interfaces and implementation class Always legal to convert to a superclass Legal – but risky – to cast from superclass to subclass DCS – SWC

Conversions In other words: DefaultShape s1 = new Square(10,10,10); // OK DefaultShape s2 = new Point(20,20); // OK Circle c = new Circle(5,10,20); // OK DefaultShape s = new DefaultShape(); // OK (!) Circle c = new DefaultShape(); // NO! Square sq = new Circle(5,20,40); // NO! DCS – SWC

Conversions And this is still dangerous! public void enlarge(DefaultShape s) { Circle c = (Circle)s; double r = c.getRadius(); c.setRadius(2*r); } OK – if s is a Circle! DCS – SWC