Lec07_Polymorphism_2per - EECS 285 EECS285 Lecture 07...

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

View Full Document Right Arrow Icon
1 EECS EECS 285 EECS285 Lecture 07 Polymorphism Van der Linden Ch. 8 (second half) 2 Andrew M. Morgan EECS EECS 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
Image of page 1

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

View Full Document Right Arrow Icon
2 3 Andrew M. Morgan EECS EECS 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 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 barb2right Polymorphism
Image of page 2
3 5 Andrew M. Morgan EECS EECS 285 Polymorphism in Java Polymorphism is automatic in Java
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern