Lec07_Polymorphism_2per

Lec07_Polymorphism_2per - 1 EECS EECS 285 285 EECS285...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 EECS EECS 285 285 EECS285 Lecture 07 Polymorphism Van der Linden Ch. 8 (second half) 2 Andrew M. Morgan EECS EECS 285 285 Inheritance, Revisited • Recall that inheritance allows one class to obtain attributes and functionality from another class • Base classes should be generic-type classes which contain members common to all objects • Many new classes can inherit from the base class • A set of classes which all inherit from the same base class are related in that sense • This relationship can be advantageous in Java 2 3 Andrew M. Morgan EECS EECS 285 285 Shape Example public class Square extends Shape { int len; public Square(final int inR, final int inC, final int inLen) { super(inR, inC); len = inLen; } public int computeArea() { return (len * len); } public int computeCircumference() { return (4 * len); } } public class Rectangle extends Shape { int len; int wid; public Rectangle(final int inR, final int inC, final int inLen, final int inWid) { super(inR, inC); len = inLen; wid = inWid; } public int computeArea() { return (len * wid); } public int computeCircumference() { return (2 * len + 2 * wid); } } public class Shape { int row; int col; public Shape(final int inR, final int inC) { row = inR; col = inC; } } Common functionality? Promote to parent class? 4 Andrew M. Morgan EECS EECS 285 285 Polymorphism • The computeArea method seems like functionality common to all subclasses of Shape • But each subclass has a different implementation • Polymorphism: A common interface for multiple implementations – Will allow the interface to be placed at the parent level, but each subclass still has its own implementation • Why do we care? – Remember a base class reference can refer to a subclass object – The actual type of the object being referred to is maintained – Therefore, when a base class reference is used to invoke a method, the subclass implementation can still be executed • b Polymorphism 3 5 Andrew M. Morgan EECS EECS 285 285 Polymorphism in Java • Polymorphism is automatic in Java – Not true for C++ - programmer must include "virtual" keyword...
View Full Document

This note was uploaded on 01/19/2012 for the course EECS 285 taught by Professor Idk during the Fall '08 term at University of Michigan.

Page1 / 6

Lec07_Polymorphism_2per - 1 EECS EECS 285 285 EECS285...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online