Inheritance (Part 4) Polymorphism and Abstract Classes 1.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
Inheritance Notes Chapter 6 and AJ Chapters 7 and 8 1.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Inheritance Inheritance allows the derivation of a new class from an existing one, for the purpose of reuse, enhancement, adaptation, etc. superclass (a.k.a.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
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.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance (Part 4) Abstract Classes.
Polymorphism in Methods
Lecture 12 Inheritance.
Overriding Method.
Inheritance and Polymorphism
Interfaces.
Computer Science II Exam 1 Review.
Chapter 9 Inheritance and Polymorphism
Interfaces.
Inheritance, Polymorphism, and Interfaces. Oh My
Inheritance.
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Summary.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Inheritance (Part 4) Polymorphism and Abstract Classes 1

Inheritance Recap  inheritance allows you to create subclasses that are substitutable for their ancestors  inheritance interacts with preconditions, postconditions, and exception throwing  subclasses  inherit all non-private features  can add new features  can change the behaviour of non-final methods by overriding the parent method  contain an instance of the superclass  subclasses must construct the instance via a superclass constructor 2

Puzzle 3 3  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[] args) { Enigma e = new Enigma(); System.out.println( e.equals(e) ); }  You must not override Object.equals() [Java Puzzlers by Joshua Block and Neal Gaffer]

Polymorphism  inheritance allows you to define a base class that has attributes and methods  classes derived from the base class can use the public and protected base class attributes and methods  polymorphism allows the implementer to change the behaviour of the derived class methods 4

// client code public void print(Dog d) { System.out.println( d.toString() ); } // later on... Dog fido = new Dog(); CockerSpaniel lady = new CockerSpaniel(); Mix mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); 5 Dog toString CockerSpaniel toString Mix toString

 notice that fido, lady, and mutt were declared as Dog, CockerSpaniel, and Mutt  what if we change the declared type of fido, lady, and mutt ? 6

// client code public void print(Dog d) { System.out.println( d.toString() ); } // later on... Dog fido = new Dog(); Dog lady = new CockerSpaniel(); Dog mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); 7 Dog toString CockerSpaniel toString Mix toString

 what if we change the print method parameter type to Object ? 8

// client code public void print(Object obj) { System.out.println( obj.toString() ); } // later on... Dog fido = new Dog(); Dog lady = new CockerSpaniel(); Dog mutt = new Mix(); this.print(fido); this.print(lady); this.print(mutt); this.print(new Date()); 9 Dog toString CockerSpaniel toString Mix toString Date toString

Late Binding  polymorphism requires late binding of the method name to the method definition  late binding means that the method definition is determined at run-time 10 obj.toString() non-static method run-time type of the instance obj

Declared vs Run-time type 11 Dog lady = new CockerSpaniel(); declared type run-time or actual type

 the declared type of an instance determines what methods can be used  the name lady can only be used to call methods in Dog  lady.someCockerSpanielMethod() won't compile 12 Dog lady = new CockerSpaniel();

 the actual type of the instance determines what definition is used when the method is called  lady.toString() uses the CockerSpaniel definition of toString 13 Dog lady = new CockerSpaniel();

Abstract Classes  sometimes you will find that you want the API for a base class to have a method that the base class cannot define  e.g. you might want to know what a Dog 's bark sounds like but the sound of the bark depends on the breed of the dog  you want to add the method bark to Dog but only the subclasses of Dog can implement bark  e.g. you might want to know the breed of a Dog but only the subclasses have information about the breed  you want to add the method getBreed to Dog but only the subclasses of Dog can implement getBreed 14

Abstract Classes  sometimes you will find that you want the API for a base class to have a method that the base class cannot define  e.g. you might want to know the breed of a Dog but only the subclasses have information about the breed  you want to add the method getBreed to Dog but only the subclasses of Dog can implement getBreed 15

 if the base class has methods that only subclasses can define and the base class has attributes common to all subclasses then the base class should be abstract  if you have a base class that just has methods that it cannot implement then you probably want an interface  abstract :  (dictionary definition) existing only in the mind  in Java an abstract class is a class that you cannot make instances of 16

 an abstract class provides a partial definition of a class  the subclasses complete the definition  an abstract class can define attributes and methods  subclasses inherit these  an abstract class can define constructors  subclasses can call these  an abstract class can declare abstract methods  subclasses must define these (unless the subclass is also abstract) 17

Abstract Methods  an abstract base class can declare, but not define, zero or more abstract methods  the base class is saying "all Dog s can provide a String describing the breed, but only the subclasses know enough to implement the method" 18 public abstract class Dog { // attributes, ctors, regular methods public abstract String getBreed(); }

Abstract Methods  the non-abstract subclasses must provide definitions for all abstract methods  consider getBreed in Mix 19

public class Mix extends Dog { // stuff from public String getBreed() { if(this.breeds.isEmpty()) { return "mix of unknown breeds"; } StringBuffer b = new StringBuffer(); b.append("mix of"); for(String breed : this.breeds) { b.append(" " + breed); } return b.toString(); } 20

PureBreed  a purebreed dog is a dog with a single breed  one String attribute to store the breed  note that the breed is determined by the subclasses  the class PureBreed cannot give the breed attribute a value  but it can implement the method getBreed  the class PureBreed defines an attribute common to all subclasses and it needs the subclass to inform it of the actual breed  PureBreed is also an abstract class 21

public abstract class PureBreed extends Dog { private String breed; public PureBreed(String breed) { super(); this.breed = breed; } public PureBreed(String breed, int size, int energy) { super(size, energy); this.breed = breed; } 22

@Override public String getBreed() { return this.breed; } 23

Subclasses of PureBreed  the subclasses of PureBreed are responsible for setting the breed  consider Komondor 24

Komondor public class Komondor extends PureBreed { private final String BREED = "komondor"; public Komondor() { super(BREED); } public Komondor(int size, int energy) { super(BREED, size, energy); } // other Komondor methods... } 25