Abstract keyword In Java

What is Abstract

As we all know,An abstract method declaration introduces the method as a member, providing its signature, return type, and throws clause , but does not provide an implementation. Every subclass of A that is not abstract must provide an implementation for method, or a compile-time error occurs.

also read:

JLS states:
An instance method that is not abstract can be overridden by an abstract method.

For Example:
class BufferEmpty extends Exception {
BufferEmpty() { super(); }
BufferEmpty(String s) { super(s); }
}

class BufferError extends Exception {
BufferError() { super(); }
BufferError(String s) { super(s); }
}

interface Buffer {
char get() throws BufferEmpty, BufferError;
}

abstract class InfiniteBuffer implements Buffer {
public abstract char get();
}

public class AbstractClassExample extends InfiniteBuffer {

public char get() {
return 'c';
}

public static void main(String[] args) {

AbstractClassExample abstractClassExample=new AbstractClassExample();
System.out.println(abstractClassExample.get());
}

}

The overriding declaration of method get in class InfiniteBuffer states that method get in any subclass of InfiniteBuffer never throws a BufferEmpty exception, putatively because it generates the data in the buffer, and thus can never run out of data.Also Overriding method may omit throws clause which superclass may declare but may not introduce newly as it may break exisiting code specification.

For Example:
class BadPointException extends Exception {
BadPointException() { super(); }
BadPointException(String s) { super(s); }
}

class BadPoint {
int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
}

class CheckedPoint extends BadPoint {
void move(int dx, int dy) throws BadPointException {
if ((x + dx) < 0 (y + dy) < 0)
throw new BadPointException();
x += dx; y += dy;
}
}

class BadPointDemo {
public static void main(String[] args) {
CheckedPoint checkedPoint=new CheckedPoint();
checkedPoint.move(10,10);
System.out.println(checkedPoint.x);
}
}

This example results in a compile-time error, because the override of method move in class CheckedPoint declares that it will throw a checked exception that the move in class Point has not declared. If this were not considered an error, an invoker of the method move on a reference of type Point could find the contract between it and Point broken if this exception were thrown.

Even Removing the throws clause does not help:

class CheckedPoint extends Point {
void move(int dx, int dy) {
if ((x + dx) < 0 (y + dy) < 0)
throw new BadPointException();
x += dx; y += dy;
}
}

A different compile-time error now occurs as we expect, because the body of the method move cannot throw a checked exception, namely BadPointException, that does not appear in the throws clause for move.

Now when we consider the following program.

interface ColorableAgain {
int size=10;
void setColor(int color);
}

abstract class Colored implements ColorableAgain {
static int size=10;
abstract int setColor(int color);
}

public class AbstractDemo2 {

public static void main(String[] args) {
Colored colored=null;
System.out.println(Colored.size);
}
}

This result in a compile-time error: it would be impossible for any subclass of class Colored to provide an implementation of a method named setColor, taking one argument of type int, that can satisfy both abstract method specifications, because the one in interface ColorableAgain requires the same method to return no value, while the one in class Colored requires the same method to return a value of type int.Hence we cannot change the return type of the implemented method from an interface.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*