Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.

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

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Reusable Classes.  Motivation: Write less code!
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
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 =
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.
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 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
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.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
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.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
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.
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
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.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
1 Principles of Computer Science II Prof. Nadeem Abdul Hamid CSC 121 – Spring 2006 Lecture Unit 4 - Inheritance.
Dr. Majed Abdouli Copyright © 2015 adapted from John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
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.
CSC 205 Java Programming II
Java Inheritance.
Lecture Notes – Inheritance (Ch 9-10)
Inheritance in Java.
Implementing Classes Yonglei Tao.
Chapter 9 – Inheritance.
Phil Tayco Slide version 1.1 Created Oct 30, 2017
Computing with C# and the .NET Framework
CSC 205 Java Programming II
Chapter 10 – Inheritance Big Java by Cay Horstmann
Polymorphism and access control
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Adapted from Java Concepts Companion Slides
Introduction to Inheritance
JAVA CLASSES.
Presentation transcript:

Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{ String type; String model; }

We want a class which to represent a Car. This class will need all of the attributes of a standard Vehicle plus some more..

Inherited Fields are Private Consider deposit method of CheckingAccount public void deposit(double amount) { transactionCount++; // now add amount to balance... } Can't just add amount to balance balance is a private field of the superclass Subclass must use public interface

and all data of Vehicle are automaticaly inherited by class Car Ok to call deposit, getBalance on SavingsAccount object Extended class = superclass, extending class = subclass Inheritance is different from realizing an interface oInterface is not a class oInterface supplies no instance fields or methods to inherit

We want a class which to represent Cars. This class will need all of the behavior of a standard Vehicle plus some more.. Constructor (calling superclass constructor

Object: The Cosmic Superclass All classes extend Object Most useful methods: o String toString() o boolean equals(Object otherObject) o Object clone()

The Object Class is the Superclass of Every Java Class

Overriding the toString Method Returns a string representation of the object Useful for debugging Example: Rectangle.toString returns something like java.awt.Rectangle[x=5,y=10,width=20,height=30] toString used by concatenation operator aString + anObject means aString + anObject. toString() Object.toString prints class name and object address Override toString : public class BankAccount { public String toString() { return "BankAccount[balance=" + balance + "]"; }... }

Overriding the equals Method equals tests for equal contents == tests for equal location Must cast the Object parameter to subclass public class Coin { public boolean equals(Object otherObject) { Coin other = (Coin)otherObject; return name.equals(other.name) && value == other.value; } }

Two References to Equal Objects

Two References to Same Object

Overriding the clone Method Copying object reference gives two references to same object BankAccount account2 = account1 ; Sometimes, need to make a copy of the object Use clone: BankAccount account2 = (BankAccount)account1. clone () ; Must cast return value because return type is Object Define clone method to make new object: public Object clone() { BankAccount cloned = new BankAccount(); cloned.balance = balance; return cloned; } Warning: This approach doesn't work with inheritance--see Advanced Topic 11.6