Data abstraction is the process of hiding certain details and showing only essential information to the user.
Abstraction can be achieved with either abstract
classes or interfaces
The abstract keyword
is a non-access modifier, used for classes and methods:
- Abstract
class: is a restricted class that cannot be used
to create objects (to access it, it must be inherited from another class).
- Abstract
method: can only be used in an abstract class,
and it does not have a body. The body is provided by the subclass
(inherited from).
Following are some important observations about
abstract classes in Java.
- An abstract class must be declared with an abstract keyword.
- An abstract class can have abstract and non-abstract methods.
- An abstract class cannot be instantiated.
- An abstract class can have constructors and static methods
also.
- An abstract class can have final methods which will force the
subclass not to change the body of the method.
Example:
abstract class Base {
abstract
void fun();
}
class Derived extends Base {
void
fun()
{
System.out.println("Derived
fun() called");
}
}
class Main {
//
Main driver method
public
static void main(String args[])
{
Base
b = new Base(); // gives error if written, creating instance of abstract class.
Base
b = new Derived();
b.fun();
}
}
No comments:
Post a Comment