Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area.

Similar presentations


Presentation on theme: "Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area."— Presentation transcript:

1 Lecture 21 - Abstract Classes and Interface

2 Example Figure –Rectangle –Triangle Figure –Dimensions –Area

3 Figure Class public class Figure { protected double dim1; protected double dim2; public Figure (double firstD, double secondD) { dim1 = firstD; dim2 = secondD; } public double area() { system.out.println(“Area here is undefined”); return 0; }

4 Class Rectangle public class Rectangle extends Figure { Rectangle (double firstD, double secondD) { super (firstD, secondD); } public double area () { return dim1*dim2; }

5 Class Triangle public class Triangle extends Figure { Triangle (double firstD, double secondD) { super (firstD, secondD); } public double area () { return (dim1*dim2)/2; }

6 Class FindAreas public class FindAreas { public static void main(String args[]) { Figure f = new Figure(10,10); Rectangle r = new Rectangle (2,5); Triangle t = new Triangle (3,4); Figure figref; figref =f; f.area(); // print area of figure figref = r; r. area(); // print area of rectangle figref = t; t.area();// print area of triangle }

7 Abstract Classes Superclasses just provide generalization Subclasses must complete implementation to be more meaningful. One way: Print a warning message. Abstract classes: More elegant and ensure that subclass implements it.

8 Figure class public abstract class Figure { double dim1; double dim2; Figure (firstD, secondD) { dim1 = firstD; dim2 = secondD; } public abstract double area(); }

9 Abstract classes and methods Abstract methods have abstract in the signature. Abstract methods have no body. Abstract methods make the class abstract. Abstract classes cannot be instantiated. Concrete subclasses complete the implementation.

10 Class FindAreas public class FindAreas { public static void main(String args[]) { Figure f = new Figure(10,10); Rectangle r = new Rectangle (2,5); Triangle t = new Triangle (3,4); Figure figref; figref =f; f.area(); figref = r; r. area(); // print area of rectangle figref = t; t.area();// print area of triangle }

11 Using final public class A { final void aMethod() { } } public class B extends A { public void aMethod() { } // Error can’t override } Using final prevents the method to be overridden.

12 Final Prevents Inheritance public final class A { // … } public class B extends A // Error can’t subclass A { }

13 Interfaces public interface MyInterface { void method1 ( ); void method2 ( ); void method3 ( ); } Access is generally public If the interface is public, all its members are implicitly public Notice the keyword interface and complete lack of method bodies.

14 Interfaces An interface defines a type, just like classes do. They tell the computer what classes that implement the interface will do. They do not tell the computer how those classes will perform those tasks. Unrelated classes in terms of class hierarchy can have same interface. Using this you can fully abstract a class’s interface from its implementation. Any number of classes can implement an interface Also, a class can have more than one interface

15 class MyClass implements MyInterface, MyInterface2… { // must implement all the methods declared by Interface }

16 Abstract Class vs. Interface An abstract class can have fields and other methods that have bodies. Interface cannot have either of those.

17 Which to Choose When a partial implementation is feasible, abstract classes make sense as they can provide some functionality with the methods. Pure abstract classes (with all abstract methods) in Java are functionally equivalent to an interface, but restricted to single inheritance. Java will allow you to implement more than one interface. You can use access modifiers (protected, package, etc) in an abstract class though. Interfaces are always public.


Download ppt "Lecture 21 - Abstract Classes and Interface. Example Figure –Rectangle –Triangle Figure –Dimensions –Area."

Similar presentations


Ads by Google