JAVA Abstract - Interface29
JAVA Abstract - Interface29
It is not possible to make instances of abstract classes. Abstract method are defined in subclasses of the abstract class.
C2 d3 d4 A
Abstract class C2. Defines method A but not method B. Adds data elements d3 and d4
C3 d5
B D E
Concrete class C3. Defines method B. Adds the methods D and E and the data element d5.
Classes with abstract methods must declared abstract Classes without abstract methods can be declared abstract A subclass to a concrete superclass can be abstract Constructors can be defined on abstract classes. Instances of abstract classes cannot be made.
public void toggleTop(){ if (size() >= 2){ Object topEl1 = top(); pop(); Object topEl2 = top(); pop(); push(topEl1); push(topEl2); } } public String toString(){ return "Stack"; } }
OOP:Inheritance and Polymorphism, Part 2 5
A method body does not have be defined. Abstract method are overwritten in subclasses. Idea taken directly from C++ You are saying: The object should have this properties I just do
not know how to implement the property at this level of abstraction.
Employee
department
Student
department
TeachingA.
Multiple Classifications
Object Runable Comparable Storable Clonable
10
Describes a set of methods that a class can be forced to An interface can be used as a type concept.
n
11
An interface can be used as a type, like classes A variable or parameter declared of an interface type is polymorph
u
Any object of a class that implements the interface can be referred by the variable
Instantiaztion
n
Does not make sense on an interface. An interface can be public or "friendly" (the default). All methods in an interface are default abstract and public.
u
Access modifiers
n n
Static, final, private, and protected cannot be used. Private, protected cannot be used.
13
14
15
Prevention of cloning
n n
Necessary if unique attribute, e.g., database lock or open file reference. Not sufficient to omit to implement Cloneable.
u
17
18
Methods can be declared Method bodies can be defined All types of variables can be
declared Can have a constructor possible. Always inherits from Object.
Has no constructor Multiple inheritance possible. Multiple inheritance not Has no top interface. Multiple "parent" interfaces.
OOP:Inheritance and Polymorphism, Part 2
With an interface it is possible to send a message to an object without knowing which class(es) it belongs. The client only know that certain methods are accessible By implementing multiple interfaces it is possible for an object to change role during its life span.
Design guidelines
n n
Use classes for specialization and generalization Use interfaces to add properties to classes.
20
Only declaration is inherited. Must coding to implement an Fairly easy to understand. Very flexible. Interface totally
separated from implementation. interface. No hard conflicts.
21
Inner Classes
Fundamental language feature, added in Java 1.1. Used a lot in JFC/Swing (GUI programming). Nest a class within a class. Class name is hidden. More than hiding and organization
n n
22
// [Source: bruceeckel.com] public interface Contents { int value(); } public interface Destination { String readLabel(); }
24
26
With concrete or abstract classes, inner classes are the only way
to produce the effect of "multiple implementation inheritance"
28
Summary
Abstract classes
n n
Complete abstract class no methods are abstract but instatiation does not make sense. Incomplete abstract class, some method are abstract.
Java only supports single inheritance. Java "fakes" multiple inheritance via interfaces.
n
Very flexible because the object interface is totally separated from the objects implementation. Name inner classes Anonymous inner classes
29