Abstract
Method
An
abstract method is a method that is declared without an
implementation. An abstract method is declared using the keyword
abstract.
Syntax
abstract returnType
methodName(parameters)
Example
abstract int sum(int
operand1, int operand2);
Abstract
Class
An
abstract class is declared using the keyword abstract—it may or may
not include abstract methods. Abstract classes cannot be
instantiated, but they can be subclassed.
Syntax
abstract class ClassName{
/* may or may not contain
abstract methods */
}
If
a class extends the abstract class, then it must provide the
implementation for all the abstract methods in the abstract class,
otherwise the class should be declared as abstract.
The
main purpose of abstract class is to use the common code in the sub
classes.
Example
abstract class Animal{ String name; String getName(){ return name; } void setName(String name){ this.name = name; } abstract String run(); }
class Elephant extends Animal{ String run(){ return "I can run at the speed of 25 mph"; } }
class Lion extends Animal{ String run(){ return " I can run at the speed of 50 mph"; } }
class Tiger extends Animal{ String run(){ return "I can run at the speed of 60 mph"; } }
class AbstractTest{ public static void main(String args[]){ Animal anim1 = new Elephant(); anim1.setName("Gaja"); System.out.println(anim1.getName() +":" + anim1.run()); anim1 = new Lion(); anim1.setName("Aslam"); System.out.println(anim1.getName() +":" + anim1.run()); anim1 = new Tiger(); anim1.setName("PTR"); System.out.println(anim1.getName() +":" + anim1.run()); } }
Output
Gaja:I can run at the speed of 25 mph Aslam: I can run at the speed of 50 mph PTR:I can run at the speed of 60 mph
Every
Animal has a name, so getName() and setName() are common to all the
animals, So the implementation for these methods kept in the abstract
class Animal. Where as different Animals runs at different speeds, so
the run() method declared as abstract, so all the concrete classes
that are extending the class Animal, must provide the implementation
for the run method.
Some
Points to Remember
1.
Can I create constructor inside the abstract class ?
Yes, but you can't initialize an
object for the abstract class.
Example
abstract class Animal{ String name; Animal(String name){ this.name = name; } }
2.
Can I make abstract class as final ?
No, If you make abstract class as
final, then no other class can able to extend it. So, compiler throws
error. Abstract and final can't be used together.
Example
final abstract class Animal{ String name; Animal(String name){ this.name = name; } }
When
you tries to compile the above program, compiler throws the below
error.
Animal.java:1: error: illegal combination of modifiers: abstract and final final abstract class Animal{ ^ 1 error
3. Can I make abstract method as
final ?
No,
If you make abstract method as final, then no other class can able to
override it. So, compiler throws error. Abstract and final can't be
used together.
Example
abstract class Animal{ String name; final abstract void run(); } }
When
you tries to compile the above program, compiler throws the below
error.
Animal.java:4: error: illegal combination of modifiers: abstract and final final abstract void run(); ^ 1 error
4.
Can I define main method in abstract class ?
Yes, it is valid, but creation of
object to the abstract is not possible.
Example
abstract class Animal{ String name; public static void main(String args[]){ System.out.println("I am in main method"); } }
Output
I am in main method
5.
What is wrong with the below program ?
abstract class Animal{
void run();
}
Above
program won't compile, since if you want to make a method as
abstract, then it must be declared with the specifier abstract. When
you tries to compile, compiler throws the below error.
Animal.java:2:
error: missing method body, or declare abstract
void run();
^
1 error
To
make the program run you have two options.
Option
1
Make the
method as abstract.
abstract
class Animal{
abstract
void run();
}
Option
2
provide the
implementation for the method run().
abstract
class Animal{
void
run(){
}
}
6.
An abstract class allowed to have static methods, where as interfaces
have instance methods only (From Java8 onwards, we can add static methods to interfaces).
7.
The methods declared in interface are abstract by default.
8.
Can an interface has constructor ?
No
9.
Can an abstract class have a final method?
Yes,
it can. But the final method cannot be abstract itself
Example
abstract
class Animal{
final
void print(){
System.out.println("
I am final method in abstract class");
}
}
No comments:
Post a Comment