abstract n interface &
Post on 10-Apr-2018
222 Views
Preview:
TRANSCRIPT
-
8/8/2019 Abstract n Interface &
1/21
ABSTRACTCLASS &
JAVAINTERFACE
-
8/8/2019 Abstract n Interface &
2/21
Abstract Classes
OBJECTIVESu
What is an Abstract method?
Abstract class?
When to use???
Exaples.
2
-
8/8/2019 Abstract n Interface &
3/21
Abstract method:
Keyword abstractis used.
abstracttype name(parameter-list);
When to use Abstract Methods &Abstract Class?
3
Abstract Classes
-
8/8/2019 Abstract n Interface &
4/21
Example
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed inabstract classes
void callmetoo() {
System.out.println("Th
is is a concretemethod.");
}
}4
-
8/8/2019 Abstract n Interface &
5/21
class B extends A {
void callme() {
System.out.println("B's implementation ofcallme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
} 5
-
8/8/2019 Abstract n Interface &
6/21
// 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
Example 2
-
8/8/2019 Abstract n Interface &
7/21
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
-
8/8/2019 Abstract n Interface &
8/21
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.");
return dim1 * dim2 / 2;
}
}
8
-
8/8/2019 Abstract n Interface &
9/21
class AbstractAreas {
public static void main(String args[]) {
// Figure f = new Figure(10, 10); // illegal nowRectangle 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
-
8/8/2019 Abstract n Interface &
10/21
Interfaces
OBJECTIVESu
Defining an Interface
Implementing an Interface Implementing multiple Interface's
Inheritance among Interface's
Interface and Polymorphism Rewriting an Interface
10
-
8/8/2019 Abstract n Interface &
11/21
interface InterfaceName
{
variables declaration;
methods declaration;
}
Variables declared as:
11
Interfaces
Static final type VariableName = value;
-
8/8/2019 Abstract n Interface &
12/21
interface Driveable
{
boolean startEngine( ) ;void stopEngine( ) ;
float accelerate( float acc ) ;
boolean turn( Direction dir ) ;}
12
Example
-
8/8/2019 Abstract n Interface &
13/21
IMPLEMENTING INTERFACES:
class classname [extends superclass]
[implements interface1[,interface2]]
{
//class body
}
13
-
8/8/2019 Abstract n Interface &
14/21
class Automobile implements Driveable{
...
public boolean startEngine( )
{
if ( notTooCold )
engineRunning = true;
...
}
14
Example
-
8/8/2019 Abstract n Interface &
15/21
public void stopEngine( )
{
engineRunning = false;
}
public float accelerate( float acc ){
...
}
public boolean turn( Direction dir ) {
...}
}15
-
8/8/2019 Abstract n Interface &
16/21
After declaring the interface, we have a new
type, Driveable.We can declare variables of
type Driveable and assign them any instance
of a Driveable object:Automobile auto = new Automobile( );
Lawnmower mower = new Lawnmower( );
Driveable vehicle;
vehicle = auto;
vehicle.startEngine( );
vehicle.stopEngine( );
16
Interface as a type
-
8/8/2019 Abstract n Interface &
17/21
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 class17
Why interfaces???
-
8/8/2019 Abstract n Interface &
18/21
//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
Interfaces can be extended
-
8/8/2019 Abstract n Interface &
19/21
//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( ));
}
}
-
8/8/2019 Abstract n Interface &
20/21
Class IFExtend {
Public static void main(String arg[ ] ) {
Myclass ob = new Myclass ( ) ;
Ob. meth1( ) ;
Ob. meth2( ) ;
Ob. meth3( ) ;
}
}
-
8/8/2019 Abstract n Interface &
21/21
BY:
VISHEKHACHATURVEDI
top related