# Abstract Classes and Interface - PowerPoint PPT Presentation

PPT – Abstract Classes and Interface PowerPoint presentation | free to download - id: 1d81cc-ZDc1Z

The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
Title:

## Abstract Classes and Interface

Description:

### Triangle. Figure. Dimensions. Area. Figure Class. public class Figure ... t.area(); // print area of triangle. Using final. public class A. final void aMethod ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 18
Provided by: course5
Category:
Tags:
Transcript and Presenter's Notes

Title: Abstract Classes and Interface

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 dim1dim2

5
Class Triangle
• public class Triangle extends Figure
• Triangle (double firstD, double secondD)
• super (firstD, secondD)
• public double area ()
• return (dim1dim2)/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 cant
override
• Using final prevents the method to be overridden.

12
Final Prevents Inheritance
• public final class A
• //
• public class B extends A // Error cant 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
• Unrelated classes in terms of class hierarchy can
have same interface.
• Using this you can fully abstract a classs
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.