lecture13

lecture13 - Introduction to Computation Introduction to...

Info iconThis preview shows pages 1–5. 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: Introduction to Computation Introduction to Computation and Problem Solving and Problem Solving Prof. Steven R. Lerman Prof. Steven R. Lerman and and Dr. V. Judson Harward Dr. V. Judson Harward Class 13: Inheritance and Interfaces Class 13: Inheritance and Interfaces 2 More on Abstract Classes Classes can be very general at the top of a class hierarchy. For example, MIT could have a class Person, from which Employees, Students, Visitors, etc. inherit Person is too abstract a class for MIT to ever create an instance of it in an application, but it can hold name, address, status, etc. that is in common to all the subclasses We can make Person an abstract class: Person objects cannot be created, but subclass objects, such as Student, can be Classes can be concrete or abstract 1 3 Abstract Classes, p.2 Another example (leading to graphics in the next lectures) Shape class in a graphics system Shapes are too general to draw; we only know how to draw specific shapes like circles or rectangles Shape abstract class can define a common set of methods that all shapes must implement, so the graphics system can count on certain things being available in every concrete class Shape abstract class can implement some methods that every subclass must use, for consistency: e.g., object ID, object type 4 Shape class abstract class Shape { public abstract void draw(); // Drawing function must be implemented in each // derived class but no default is possible: abstract public void error(String message); // Error function must be implemented in each derived // class and a default is available: non-abstract method public final int objectID(); // Object ID function: each derived class must have one // and must use this implementation: final method }; class Square extends Shape {}; class Circle extends Shape {}; 2 5 Abstract method Shape is an abstract class (keyword) Shape has an abstract method draw() No objects of type Shape can be created draw() must be redeclared by any concrete (non- abstract) class that inherits it There is no definition of draw() in Shape This says that all Shape s must be drawable, but the Shape class has no idea of how to draw specific shapes 6 Non-abstract method Shape has a non-abstract method error() Every derived class must have an error method Each derived class may handle errors as it wishes: It may define its own error function using this interface (method arguments/return value) It may use the super class implementation as a default This can be dangerous: if new derived classes are added and programmers fail to redefine non- abstract methods, the default will be invoked but may do the wrong thing 3 7 Final method Shape has a final method objectID Final method is invariant across derived classes Behavior is not supposed to change, no matter how specialized the derived class becomes Super classes should have a mix of methods...
View Full Document

Page1 / 15

lecture13 - Introduction to Computation Introduction to...

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

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