Showing posts with label Abstract Class. Show all posts
Showing posts with label Abstract Class. Show all posts

Question Related to Abstract Class

Q1: Must abstract classes contain at least one abstract method?
A: No, abstract classes are not required to have any abstract methods, they can simply be marked abstract for general design reasons. An abstract class may contain a full set of functional, integrated methods but have no practical use in its basic form, for example. In other words, they may require extension with additional methods to fulfil a range of different purposes. If the purpose is not specified by abstract method signatures, the range of potential applications for subclasses can be very broad.

Q2: How can an abstract method be inherited?
A: A subclass inherits all non-private fields and methods of its superclass whether the methods are abstract or have concrete implementations. If a subclass does not implement an abstract method, the subclass must be declared abstract itself. That means that an abstract method can be inherited through numerous abstract classes without any concrete implementation.
A common use of abstract methods is the template design pattern, where the common behaviour of a class hierarchy is defined in a set of concrete methods in an abstract superclass. Those core methods include calls to abstract template methods which must be implemented in concrete subclasses. This makes it relatively easy to implement subclasses and produce type-specific behaviour in each.
 
Q3: Why can't an abstract method be declared private?
A: The private and abstract method modifiers do not make sense in combination and the compiler should normally fail with a warning in this case. An abstract method must be overridden by any subclass, but subclasses do not have access to their superclass' private fields, so a private abstract method could never be fulfilled.

Q4: Why can't abstract methods be declared static?
A: The rule against static abstract methods is fundamentally a Java language design decision, which is not explained in the Java Language Specification. The abstract inheritance and implementation scheme is concerned with forming a structured collaboration of classes with deferred implementation of its abstract instance methods. This scheme can be verified at compile time to ensure it is safe at runtime.
Static methods must be concrete because they are attached to a specific host class and must be available to execute in a static context, when no object instance exists. It is not possible to assign an implementation to a static method at runtime, there is no mechanism to do that in Java.
It is also worth noting that static methods cannot be overridden by a subclass in Java. Static methods with the same signature effectively hide the superclass method. The concept of implementing a static method in a subclass would fail for the same reason, the concrete implementation would be attached to the “wrong” host.

Abstract Class In Java


Java Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.
Like any other class, an abstract class can contain fields that describe the characteristics and methods that describe the actions that a class can perform. An abstract class can include methods that contain no implementation. These are called abstract methods. The abstract method declaration must then end with a semicolon rather than a block. If a class has any abstract methods, whether declared or inherited, the entire class must be declared abstract. Abstract methods are used to provide a template for the classes that inherit the abstract methods.

Abstract classes cannot be instantiated; they must be subclassed, and actual implementations must be provided for the abstract methods. Any implementation specified can, of course, be overridden by additional subclasses. An object must have an implementation for all of its methods. You need to create a subclass that provides an implementation for the abstract method.
To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration:

abstract class Number {
. . .
}

If you attempt to instantiate an abstract class, the compiler displays an error similar to the following and refuses to compile your program:
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
new AbstractTest();
^
1 error

Points of abstract class :
1    Abstract class contains abstract methods.
2    Program can't instantiate an abstract class.
3    Abstract classes contain mixture of non-abstract and abstract methods.
4    If any class contains abstract methods then it must implements all the abstract methods of the abstract class.


Here is a simple example of a class with an abstract method, followed by a class which implements that method:
// 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.");
}
}
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();
}
}
Notice that no objects of class A are declared in the program. As mentioned, it is not possible to instantiate an abstract class. One other point: class A implements a concrete method called callmetoo( ). This is perfectly acceptable. Abstract classes can include as much implementation as they see fit.
 

Advantage of Abstract Classes

The advantage of using an abstract class is that you can group several related classes together as siblings. Grouping classes together is important in keeping a program organized and understandable. 

An Abstract class is a way to organize inheritance, sometimes it makes no since to implement every method in a class (i.e. a predator class), it may implement some to pass to its children but some methods cannot be implemented without knowing what the class will do (i.e. eat() in a Tiger class). Therefore, abstract classes are templates for future specific classes.

Disadvantage of Abstract Classes

A disadvantage is that abstract classes cannot be instantiated, but most of the time it is logical not to create a object of an abstract class.
 

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More