460 likes | 643 Views
COP3502: Introduction to CIS I. Lecture 16. Today Interfaces and Polymorphism Wednesday, Friday, Monday GUIs, Animation, Events. In-class review Wed Feb 26 In-class Exam Fri Feb 28 Will cover everything up through next Monday’s class. Super Session Thursday Feb 27 8pm-10pm.
E N D
COP3502: Introduction to CIS I Lecture 16
Today Interfaces and Polymorphism Wednesday, Friday, Monday GUIs, Animation, Events
In-class review Wed Feb 26 In-class Exam Fri Feb 28 Will cover everything up through next Monday’s class
Super Session Thursday Feb 278pm-10pm
Tuesday Feb 18 Lab 3 (Objects) Tuesday Feb 25 Lab 4 (GUIs)
Reading Thinking in Java (Eckel) Ch. 1 Intro to Java Programming (Eck) Ch. 5
classes can be both a superclass and a subclass at the same time!
uses of inheritance • Organizing information • Grouping similar classes • Modeling similarity among classes • Creating a taxonomy among objects
What does the subclass inherit? public methods protected methods protected instance variables
What does the subclass inherit? public methods protected methods protected instance variables NOT PRIVATE ANYTHING
subclass specializes superclass adding new methods
subclass specializes superclass adding new methods overriding existing methods
subclass specializes superclass adding new methods overriding existing methods Implementing abstract methods
subclass specializes superclass adding new methods overriding existing methods Implementing abstract methods Superclass “factors out” common capabilities among subclasses
abstract methods empty methods in the superclass need to be implemented by inheriting subclass uses abstract keyword
abstract methods empty methods in the superclass need to be implemented by inheriting subclass uses abstract keyword abstract void draw();
any class with an abstract method should itself be declared abstract abstract class Shape { … abstract void draw(); … }
overriding methods public class Square extends Shape { … @Override public void draw() { … } … }
method resolution compiler walks up the class inheritance hierarchy tree until it finds the appropriate method
super keyword public class Square extends Shape { … public Square(double x, double y, Color c, double len) { super(x, y, c); length = len; } … }
super keyword public class Shape { … public void rotate(double radians) { //perform rotation } … } Pretend this method exists in our Shape implementation!
super keyword public class Square extends Shape { … @Override public void rotate(double degrees) { double radians = Math.PI*degrees/180; super.rotate(radians); } … } Same as Shape.rotate()
super keyword public class Square extends Shape { … @Override public void rotate(double degrees) { double radians = Math.PI*degrees/180; super.rotate(radians); } … } partial overriding Same as Shape.rotate()
classes can only inherit from one superclass But they can inherit from multiple interfaces Superclass Interface Interface Subclass
interface a set of instance methods without any implementation ie. a collection of abstract methods
interface “factors out” commonality among very different classes that share behavior
both have the ability to be “colored” might implement a Colorable interface
interfaces declare methods, but don’t include any implementation
interfaces No constructor usually no instance variables just a list of responsibilities even more abstract than abstract classes
uses of interfaces give specific properties to an object Usually an adjective that ends in –ive or –able Colorable Rotatable Bounceable
uses of interfaces give role to an object Usually ends in –er Container Mover Teacher
public interface Colorable { // set the current color public void setColor(Color color); // get the current color public Color getColor(); }
public interface Decorable { // models something which can be decorated public void decorate(Decoration dcr); }
interfaces can extend any number of other interfaces subclass inherits all superclass methods AND all interfaces
public interface Artistic extends Colorable, Decorable{ // probably uses decorate and change color methods public void putOnDisplay(); }
methods for factoring out common code loops and functions
methods for factoring out common code loops and functions classes and instances
methods for factoring out common code loops and functions classes and instances superclasses and subclasses
methods for factoring out common code loops and functions classes and instances superclasses and subclasses interfaces and implementations