Download presentation
Presentation is loading. Please wait.
1
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated We use the modifier abstract on the class header to declare a class as abstract b b An abstract class often contains abstract methods (like an interface does), though it doesn’t have to
2
Abstract Classes b b The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract b b An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) b b The use of abstract classes is a design decision; it helps us establish common elements in a class that is to general to instantiate b b Usually concrete classes extend abstract ones, but the opposite is also possible. b b Can an abstract method be private?
3
3 References and abstract classes Suppose the following two classes were defined: b public abstract class Figure public class Rectangle extends Figure b Are these instantiations correct? Rectangle r = new Rectangle(...); Figure f = new Rectangle(...); Figure f = new Figure(...);
4
Interfaces b b A Java interface is a collection of abstract methods and constants An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, it is usually left off b b An interface is used to formally define a set of methods that a class will implement b b A class can implement multiple interfaces b b The interfaces are listed in the implements clause, separated by commas
5
5 Interface example We want to define an interface so that classes which implement it have meaningful string representation b Will the following definition accomplish this purpose? interface Printable { String toString(); } b Such interfaces are called marker interfaces b java.lang.Cloneable is another marker interface b Is this one different? interface Printable {}
6
6 Interface inheritance I Just as class extends class an interface can extend interface b Unlike classes interface can extend more than one interface b Thus in Java we have - single implementation inheritance - multiple interface inheritance
7
7 Interface inheritance II public interface MyInterface1 { void method1(); } public interface MyInterface2 { String method2(); } public interface MyInterface3 extends MyInterface1, MyInterface2 { void method3(); } b b If you write a class that implements MyInterface2 you will have to implement method1(),method2() and method3().
8
8 When to use interfaces? Interfaces allow multiple inheritance b Inheriting from a class allows implementation inheritance but class can extend only one other class b Any major class should be an implementation of an interface.
9
9 Polymorphism via Interfaces b b An interface defines a type just like any class in java. b b This means you can use interface names wherever you used primitive or object types. For example look at FunctionDrawingApplet of Ex. 12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.