DOC PREVIEW
Penn CIT 591 - Abstract Classes and Interfaces

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Abstract Classes and InterfacesJava is “safer” than PythonAbstract methodsAbstract classes IAbstract classes IIWhy have abstract classes?An example abstract classWhy have abstract methods?A problemA solutionInterfacesDesigning interfacesImplementing an interface IImplementing an interface IIPartially implementing an InterfaceWhat are interfaces for?How to use interfacesinstanceofInterfaces, againAdapter classesVocabularyThe EndJan 14, 2019Abstract Classes and InterfacesJava is “safer” than PythonPython is very dynamic—classes and methods can be added, modified, and deleted as the program runsIf you have a call to a function that doesn't exist, Python will give you a runtime error when you try to call itIn Java, everything has to be defined before the program begins to executeIf you have a call to a function that doesn't exist, the compiler marks it as a syntax errorSyntax errors are far better than runtime errorsAmong other things, they won’t make it into distributed codeTo achieve this, Java requires some additional kinds of classes23Abstract methodsYou can declare an object without defining it: Person p;Similarly, you can declare a method without defining it: public abstract void draw(int size);Notice that the body of the method is missingA method that has been declared but not defined is an abstract method4Abstract classes IAny class containing an abstract method is an abstract classYou must declare the class with the keyword abstract: abstract class MyClass {...}An abstract class is incompleteIt has “missing” method bodiesYou cannot instantiate (create a new instance of) an abstract class5Abstract classes IIYou can extend (subclass) an abstract classIf the subclass defines all the inherited abstract methods, it is “complete” and can be instantiatedIf the subclass does not define all the inherited abstract methods, it too must be abstractYou can declare a class to be abstract even if it does not contain any abstract methodsThis prevents the class from being instantiated6Why have abstract classes?Suppose you wanted to create a class Shape, with subclasses Oval, Rectangle, Triangle, Hexagon, etc.You don’t want to allow creation of a “Shape”Only particular shapes make sense, not generic onesIf Shape is abstract, you can’t create a new ShapeYou can create a new Oval, a new Rectangle, etc.Abstract classes are good for defining a general category containing specific, “concrete” classes7An example abstract classpublic abstract class Animal { abstract int eat(); abstract void breathe();}This class cannot be instantiatedAny non-abstract subclass of Animal must provide the eat() and breathe() methods8Why have abstract methods?Suppose you have a class Shape, but it isn’t abstractShape should not have a draw() methodEach subclass of Shape should have a draw() methodNow suppose you have a variable Shape figure; where figure contains some subclass object (such as a Star)It is a syntax error to say figure.draw(), because the Java compiler can’t tell in advance what kind of value will be in the figure variableA class “knows” its superclass, but doesn’t know its subclassesAn object knows its class, but a class doesn’t know its objectsSolution: Give Shape an abstract method draw()Now the class Shape is abstract, so it can’t be instantiatedThe figure variable cannot contain a (generic) Shape, because it is impossible to create oneAny object (such as a Star object) that is a (kind of) Shape will have the draw() methodThe Java compiler can depend on figure.draw() being a legal call and does not give a syntax error9A problemclass Shape { ... }class Star extends Shape { void draw() { ... } ...}class Crescent extends Shape { void draw() { ... } ...}Shape someShape = new Star();This is legal, because a Star is a ShapesomeShape.draw();This is a syntax error, because some Shape might not have a draw() methodRemember: A class knows its superclass, but not its subclasses10A solutionabstract class Shape { abstract void draw();}class Star extends Shape { void draw() { ... } ...}class Crescent extends Shape { void draw() { ... } ...}Shape someShape = new Star();This is legal, because a Star is a ShapeHowever, Shape someShape = new Shape(); is no longer legalsomeShape.draw();This is legal, because every actual instance must have a draw() method11InterfacesAn interface declares (describes) methods but does not supply bodies for them interface KeyListener { public void keyPressed(KeyEvent e); public void keyReleased(KeyEvent e); public void keyTyped(KeyEvent e);}All the methods are implicitly public and abstractYou can add these qualifiers if you like, but why bother?You cannot instantiate an interfaceAn interface is like a very abstract class—none of its methods are definedAn interface may also contain constants (final variables)12Designing interfacesMost of the time, you will use Sun-supplied Java interfacesSometimes you will want to design your ownYou would write an interface if you want classes of various types to all have a certain set of capabilitiesFor example, if you want to be able to create animated displays of objects in a class, you might define an interface as:public interface Animatable { install(Panel p); display();}Now you can write code that will display any Animatable class in a Panel of your choice, simply by calling these methods13Implementing an interface IYou extend a class, but you implement an interfaceA class can only extend (subclass) one other class, but it can implement as many interfaces as you likeExample: class MyListener implements KeyListener, ActionListener { … }14Implementing an interface IIWhen you say a class implements an interface, you are promising to define all the methods that were declared in the interfaceExample: class MyKeyListener implements KeyListener { public void keyPressed(KeyEvent e) {...}; public void keyReleased(KeyEvent e) {...}; public void keyTyped(KeyEvent e) {...};}The “...” indicates actual code that you must supplyNow you can create a new MyKeyListener15Partially implementing an InterfaceIt is possible to define some but not all of the methods defined in an interface: abstract class MyKeyListener implements KeyListener


View Full Document

Penn CIT 591 - Abstract Classes and Interfaces

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Abstract Classes and Interfaces
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Abstract Classes and Interfaces and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Abstract Classes and Interfaces 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?