java
abstract class class-name {
// ...
}
java
abstract type name(parameter-list);
new
operator).abstract class A {
abstract void callme();
// Concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
}
public class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}
}
Output:
B's implementation of callme.
This is a concrete method.
A
are not declared, as it's not possible to instantiate an abstract class.A
implements a concrete method callmetoo()
, which is perfectly acceptable.abstract class Bank {
abstract int getRateOfInterest();
}
class BSP extends Bank {
int getRateOfInterest() {
return 3;
}
}
class KinaBank extends Bank {
int getRateOfInterest() {
return 8;
}
}
public class TestBank {
public static void main(String args[]) {
BSP bsp = new BSP();
KinaBank kb = new KinaBank();
Bank b;
// Object Referencing
b = bsp;
System.out.println("Rate of Interest is: " + b.getRateOfInterest() + " %");
b = kb;
System.out.println("Rate of Interest is: " + b.getRateOfInterest() + " %");
}
}
The final
keyword has two uses related to inheritance:
final
as a modifier at the start of its declaration.final
cannot be overridden.class A {
final void meth() {
System.out.println("This is a final method.");
}
}
class B extends A {
void meth() {
// ERROR! Can't override.
System.out.println("Illegal!");
}
}
meth()
in class B
will result in a compile-time error because it is declared as final
in class A
.final
methods cannot be overridden, a call to one can be resolved at compile time (early binding).final
.final
implicitly declares all of its methods as final
too.abstract
and final
, because an abstract
class is incomplete by itself and relies upon its subclasses to provide complete implementations.final class A {
// ...
}
// The following class is illegal.
class B extends A {
// ERROR! Can't subclass A
// ...
}
B
to inherit A
since A
is declared as final
.interface
, you can fully abstract a class’ interface from its implementation.access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
access
is either public
or not used (default access).public
, the interface can be used by any other code.name
is the name of the interface.final
and static
, meaning they cannot be changed by the implementing class.public
if the interface itself is declared as public
.interface Callback {
void callback(int param);
}
implements
clause in a class definition, and then create the methods defined by the interface.access class classname [extends superclass] [implements interface [,interface...]] {
// class-body
}
access
is either public
or not used.public
.class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
}
public
.class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement interfaces " +
"may also define other members, too.");
}
}
interface Bank {
float rateOfInterest();
}
class BSP implements Bank {
public float rateOfInterest() {
return 4.0f;
}
}
class KinaBnk implements Bank {
public float rateOfInterest() {
return 5.5f;
}
}
class TestInterface2 {
public static void main(String[] args) {
Bank b = new BSP();
System.out.println("ROI: " + b.rateOfInterest());
}
}
Output:
ROI: 4.0
static
and final
by default.interface Math {
public static final double PI = 3.14;
public static final double ELUERNUMBER = 2.718;
public static final double SQRT = 1.41421;
}
/* Test interface variables */
public class Sample implements Math {
public static void main(String[] args) {
// Calculate area of circle
int radious = 2;
// call interface constant Math.PI
double area = Math.PI * radious * radious;
System.out.println("Area of Circle =" + area);
}
}
Output:
Area of Circle =12.56
interface Moveable {
int AVERAGESPEED = 40;
void move();
}
public class Vehicle implements Moveable {
public void move() {
System.out.println("Average speed is" + AVERAGESPEED);
}
public static void main(String[] arg) {
Vehicle vc = new Vehicle();
vc.move();
System.out.println("Average speed is" + AVERAGESPEED);
}
}
Output:
Average speed is 40
Average speed is 40
AVERAGESPEED
interface variable can be accessed directly without specifying the interface name “Moveable” while accessing through the interface method itself.What we declare:
interface Moveable
int AVERAGE-SPEED = 40;
void move();
How the compiler interprets:
interface Moveable
Public static final int AVERAGE-SPEED = 40;
Public abstract void move();
Compiler automatically converts methods of Interface as public and abstract, and the data members as public, static and final by default.
push()
and pop()
methods).// Define an integer stack interface.
interface IntStack {
void push(int item); // store an item
int pop(); // retrieve an item
}
// An Interface implementation of IntStack that uses fixed storage.
class FixedStack implements IntStack {
private int stck[];
private int tos;
// allocate and initialize stack
FixedStack(int size) {
stck = new int[size];
tos = -1;
}
// Push an item onto the stack
public void push(int item) {
if (tos == stck.length - 1) // use length member
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
// Pop an item from the stack
public int pop() {
if (tos < 0) {
System.out.println("Stack underflow.");
return 0;
} else
return stck[tos--];
}
}
public class IntegerStackTest {
public static void main(String args[]) {
FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
IntStack mystack3 = new FixedStack(7);
// Using reference
// push some numbers onto the stack
for (int i = 0; i < 5; i++)
mystack1.push(i);
for (int i = 0; i < 8; i++)
mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for (int i = 0; i < 5; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for (int i = 0; i < 8; i++)
System.out.println(mystack2.pop());
}
}
FixedStack
implementation through an interface reference.push()
and pop()
methods are resolved at run time rather than at compile time.