Abstract N Interface &
Abstract N Interface &
Java Interface
Abstract Classes
Objectives…
● What is an Abstract method?
● Abstract class?
● When to use???
● Exaples….
2
Abstract Classes
Abstract method:
Keyword abstract is used.
abstract type name(parameter-list);
3
Example…
• // A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in
abstract classes
void callmetoo() {
System.out.println("This is a concrete
method.");
}
}
4
class B extends A {
void callme() {
System.out.println("B's implementation of
callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
} 5
Example 2…
// Using abstract methods and classes.
abstract class Figure {
double dim1;
double dim2;
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
// area is now an abstract method
abstract double area();
} 6
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for
Rectangle.");
return dim1 * dim2;
}
}
7
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
// override area for right triangle
double area() {
System.out.println("Inside Area for Triangle.");
8
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal now
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref; // this is OK, no object is created
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
9
Interfaces…
Objectives…
● Defining an Interface
● Implementing an Interface
● Implementing multiple Interface's
● Inheritance among Interface's
● Interface and Polymorphism
● Rewriting an Interface
10
Interfaces
• interface InterfaceName
{
variables declaration;
methods declaration;
}
Variables declared as:
11
Example…
interface Driveable
{
boolean startEngine( ) ;
void stopEngine( ) ;
float accelerate( float acc ) ;
boolean turn( Direction dir ) ;
}
12
Implementing Interfaces:
13
Example…
class Automobile implements Driveable
{
...
public boolean startEngine( )
{
if ( notTooCold )
engineRunning = true;
...
}
14
public void stopEngine( )
{
engineRunning = false;
}
public float accelerate( float acc ){
...
}
public boolean turn( Direction dir ) {
...
}
…
}
15
Interface as a type…
16
Why interfaces???
Reason 1:
To reveal an object's programming interface
(functionality of the object) without revealing its
Implementation
Reason 2:
To have unrelated classes implement similar
methods (behaviors)
Reason 3:
To model multiple inheritance - you want to impose
multiple sets of behaviors to your class
17
Interfaces can be extended…
//one interface can extend another.
Interface A{
void meth1( );
void meth( )2;
}
//B now includes meth1( ) and meth2( )---- it adds
meth3( )
Interface B extends A {
Void meth3( );
}
18
//This class must implement all of A and B
Class MyClass implements B {
public void meth1( ){
System.out.println(“Implement meth1( )”);
}
public void meth2( ){
System.out.println(“Implement meth2( )”);
}
public void meth3( ){
System.out.println(“Implement meth3( )”);
}
}
Class IFExtend {
Public static void main(String arg[ ] ) {
Myclass ob = new Myclass ( ) ;
Ob. meth1( ) ;
Ob. meth2( ) ;
Ob. meth3( ) ;
}
}
By:
Vishekha Chaturvedi