Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC 11.1-11.3 – CCJ 9.1-9.3) I have to leave at 11am (but you can stay)

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.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.
Interfaces and Polymorphism Lesson - 8. Objectives Interfaces Supertype and subtype references Polymorphism Inner classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Interfaces and Polymorphism.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
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.
Computer Science A 14: 31/3. Inheritence You can extend a class or change behaviour. Inheritance is a method to reuse code. Concept. Extending classes.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Computer Science A 15: 17/4. Today Overview of java How to decribe programming languages Main message: The core of java is small Even a small language.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
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.
Chapter 9  Interfaces and Polymorphism 1 Chapter 9 Interfaces and Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
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 and Polymorphism Daniel Liang, Introduction to Java Programming.
Interfaces and Polymorphism 9.1 Using Interfaces for Code Reuse Defining an Interface implementing an interface 9.2 Converting between Class.
What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Interfaces.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
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.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
Fall 2006Adapted from Java Concepts Companion Slides1 Interfaces and Polymorphism Advanced Programming ICOM 4015 Lecture 10 Reading: Java Concepts Chapter.
CHAPTER 9 INTERFACES AND POLYMORPHISM Chapter Goals: –To learn about interfaces –To be able to convert between supertype and subtype references –To understand.
Inheritance and Access Control CS 162 (Summer 2009)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CHAPTER 9 INTERFACES AND POLYMORPHISM. CHAPTER GOALS To learn about interfaces To be able to convert between supertype and subtype references To understand.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 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.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
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.
Chapter 13 - Inheritance.
Chapter Goals To be able to declare and use interface types
Inheritance and Polymorphism
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Interfaces and Polymorphism
Chapter 11 – Interfaces and Polymorphism
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Chapter 11 Interfaces and Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Computer Science A 9: 3/11

Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)

DataSet for BankAccounts public class DataSet{ // Modified for BankAccount objects... public void add(BankAccount x) { sum = sum + x.getBalance(); if(count==0||maximum.getBalance()<x.getBalance()) maximum = x; count++; } public BankAccount getMaximum(){ return maximum; } private double sum; private BankAccount maximum; private int count; }

DataSet for Coins public class DataSet{ // Modified for Coin objects... public void Coin x) { sum = sum + x.getValue(); if(count==0||maximum.getValue()<x.getValue()) maximum = x; count++; } public Coin getMaximum(){ return maximum; } private double sum; private Coin maximum; private int count; }

Interface public interface Measurable { double getMeasure(); } Measurable is an interface – a description of what a class my contains

Interface Some classes my ”implement” this interface. This means that they contain all the methods described in the interface. public class BankAccount implements Measurable { public double getMeasure() { return balance; } // Additional methods and fields } public class Coin implements Measurable { public double getMeasure() { return value; }... }

General dataset public class DataSet { public void add(Measurable x) { sum = sum + x.getMeasure(); if(count==0||maximum.getMeasure()<x.getMeasure()) maximum = x; count++; } public Measurable getMaximum() { return maximum; } private double sum; private Measurable maximum; private int count; }

Converting objects You can convert from a class type to an interface type, provided the class implements the interface BankAccount account = new BankAccount(10000); Measurable x = account; // OK Coin dime = new Coin(0.1, "dime"); Measurable x = dime; // Also OK Cannot convert between unrelated types Measurable x = new Rectangle(5,10,20,30); // ERROR Because Rectangle doesn't implement Measurable

casts Add coin objects to DataSet DataSet coinData = new DataSet(); coinData.add(new Coin(0.25, "quarter")); coinData.add(new Coin(0.1, "dime"));... Measurable max = coinData.getMaximum(); // Get the largest coin What can you do with it? It's not of type Coin String name = max.getName(); // ERROR You need a cast to convert from an interface type to a class type You know it's a coin, but the compiler doesn't. Apply a cast: Coin maxCoin = (Coin) max; String name = maxCoin.getName(); If you are wrong and max isn't a coin, the compiler throws an exception

Polymorphism Interface variable holds reference to object of a class that implements the interface Measurable x; x = new BankAccount(10000); x = new Coin(0.1, "dime"); Note that the object to which x refers doesn't have type Measurable; the type of the object is some class that implements the Measurable interface You can call any of the interface methods: double m = x.getMeasure(); Which method is called?

Polymorphism Depends on the actual object. If x refers to a bank account, calls BankAccount.getMeasure If x refers to a coin, calls Coin.getMeasure Polymorphism (many shapes): Behavior can vary depending on the actual type of an object Called late binding: resolved at runtime Different from overloading; overloading is resolved by the compiler (early binding)

interface MyShape interface MyShape{ void drawOn(JCanvas canvas); }

class Tree class Tree implements MyShape{ Tree(int x,int y){this.x=x; this.y=y;} int x,y; public void drawOn(JCanvas canvas){ canvas.drawArc(x,y,200,200,290,320); canvas.drawLine(x+80,y+300,x+80,y+180); canvas.drawLine(x+120,y+300,x+120,y+180); }

class Car class Car implements MyShape{ Car(int x,int y){this.x=x; this.y=y;} int x,y; public void drawOn(JCanvas canvas){ canvas.drawRect(x,y+50,300,50); canvas.drawLine(x+50,y+50,x+100,y); canvas.drawLine(x+100,y,x+200,y); canvas.drawLine(x+200,y,x+250,y+50); canvas.drawOval(x+50,y+100,50,50); canvas.drawOval(x+200,y+100,50,50); }

class MyShapeTest port javax.swing.*; public class MyShapeTest{ public static void main(String args[]){ JFrame frame=new JFrame("MyShapes"); frame.setSize(600,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCanvas canvas=new JCanvas(); frame.add(canvas); MyShape s1=new Car(10,300); MyShape s2=new Car(270,400); MyShape s3=new Tree(330,40); s1.drawOn(canvas); s2.drawOn(canvas); s3.drawOn(canvas); frame.setVisible(true); }

Example

And more… An interface is a placeholder for a number of classes. Inheritance is also that you can extend a class. This is the corner-stone of object- oriented programming and code reuse: Extend classes Override methods Protect access

Example class Point{ int x; int y; Point(int x,int y){ this.x=x; this.y=y; } public String toString(){ return "("+x+","+y+")"; }

Example class Point3D extends Point{ int z; Point3D(int x,int y,int z){ super(x,y); this.z=z; } public String toString(){ return "("+x+","+y+","+z+")";} }

Example Point a=new Point(2,4); System.out.println(a.toString()); Point3D b=new Point3D(7,9,13); System.out.println(b.toString()); A Point3D ”is-a” Point object and may be used as a Point object

Basics: class A{ int f(){return 2;} } class B extends A{ int f(){return 3;} }.. A a1=new A(); int i1=a1.f(); // ok, i=2 A a2=new B(); int i1=a2.f(); // ok, i=3 B b1=new A(); // not ok B b2=new B(); int i1=b2.f(); // ok, i=3

Inheriting Methods Override method: Supply a different implementation of a method that exists in the superclass Must have same signature (same name and same parameter types) If method is applied to an object of the subclass type, the overriding method is executed Inherit method: Don't supply a new implementation of a method that exists in superclass Superclass method can be applied to the subclass objects Add method: Supply a new method that doesn't exist in the superclass New method can be applied only to subclass objects

Inheriting Instance Fields Can't override fields Inherit field: All fields from the superclass are automatically inherited Add field: Supply a new field that doesn't exist in the superclass

Constructors public class CheckingAccount extends BankAccount { public CheckingAccount(double initialBalance) { // Construct superclass super(initialBalance); // Initialize transaction count transactionCount = 0; }... } super followed by a parenthesis indicates a call to the superclass constructor Must be the first statement in subclass constructor If subclass constructor doesn't call superclass constructor, default superclass constructor is used (no arguments)

Cast an instanceof class A{.. } class B extends A{..void g().. } A a= new B(); // ok a.g(); // not ok If(a instanceof B){ B b= (B) a; // cast b.g(); // ok }

Access control Java has four levels of controlling access to fields, methods, and classes: public access –Can be accessed by methods of all classes private access –Can be accessed only by the methods of their own class protected access –Access from subclasses package access –The default, when no access modifier is given –Can be accessed by all classes in the same package –Good default for classes, but extremely unfortunate for field