This Java tutorial helps to
understand what abstract classes and methods are. This tutorial is applicable
for beginners in Java. An abstract class in Java cannot be instantiated. It
does not end with that. An abstract Java class can have a constructor? An
abstract method can be defined as static? If you are not comfortable with these
questions, read this tutorial and update the basics.
Java abstract
class
A Java class declared using
the abstract keyword is called an abstract class. New instances cannot be
created for an abstract class, but they can be extended. An abstract class can
have abstract methods and concrete methods, or both. The methods with body of
implementation are concrete methods. An abstract class can have static fields
and methods and can be used in the same way they are used in a specific class.
Next, an example for the Java abstract class.
public abstract class Animal {
}
We cannot create new
instances for the above Animal class as it is declared as ‘abstract’.
Java abstract method
A method that is declared using the keyword abstract is called
the abstract method. The abstract methods are only statement and will not be
implemented. It will not have a method body. A Java class that contains an
abstract class must be declared as an abstract class. An abstract method can
only define a visibility modifier, a public or protected modifier. That is, an
abstract method cannot add a static or final modifier to the declaration. Here
is an example of the Java abstract method.
public abstract class Animal {
String name;
public abstract String getSound();
public String getName() {
return name;
}
}
Extending an Abstract Class
When an abstract class is
implemented in Java, all its abstract methods are usually defined. If one or
more abstract methods are not defined in the implementation class, they must
also be declared as an abstract class. The following is an example class that
implements the abstract class of animals. @Override is a Java annotation used
to indicate that this method replaces the method in the superclass
public class Lion extends Animal {
@Override
public String getSound() {
return "roar";
}
}
Abstract class Implements an Interface
It is possible for an
abstract class to implement a Java interface. If the implementing class does
not implement all the abstract methods of the interface, this must be defined
as an abstract class in Java. In the following example, the Animal class does
not implement the abstract method of the Species interface. Although Animal
does not have any abstract method by itself, it must be declared as abstract,
since it did not implement the abstract method of the Species interface. Any
class that extends the Animal class must implement the getClassification
abstract method. The difference between an interface and an abstract class is
that the methods in an interface are implicitly abstract. Go through this
linked tutorial to learn more about the differences.
public interface Species {
public String getClassification();
}
public abstract class Animal implements Species {
String name;
public String getName() {
return name;
}
}
When Should I use an Abstract class
We must go to the abstract class when we are working
with classes that contain similar code. That is, there is the possibility of
modeling behaviors and attributes. The common behavior can be elevated to a
superclass and provide its implementation. Next, add the behavior that cannot
be implemented and declare it abstract. Classes that are similar to this
abstract class extend and use the methods already implemented and add the
implementation to abstract methods.
In the example given above, the name is a common
attribute for animals and, therefore, it is elevated for the Animal superclass.
getName is a common behavior for all animals and depends on the name of the
common attribute. Therefore, the implementation of this getName is provided in
the abstract superclass. getSound depends on the individual animals and,
therefore, is declared abstract and left to extend the class to implement it.
What we gain with this model pattern is to avoid the repetition of common code.
Abstract
Class Example in Java API
AbstractMap is an abstract part of the Collections
Framework class in Java JDK. It spans a long list of subclasses
ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, IdentityHashMap,
TreeMap, WeakHashMap. These classes share and reuse many methods of this abstract
class like get, put, isEmpty.
Can an
Abstract Class have Constructor in Java?
Yes, an abstract class can have a constructor in
Java. It can be a useful option to impose class restrictions, such as setting
up a field. Let me demonstrate this by using our animal example below.
public abstract class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String getSound();
}
This abstract class defines a constructor with an argument that is used to setup the field name. Classes that extends this abstract class should define a constructor with implicit
super()
call
to the super abstract class. Otherwise we will get an error as “Implicit super
constructor Animal() is undefined. Must explicitly invoke another constructor”.
This is to do some initialization before instantiation.public class Lion extends Animal {
public Lion(String name) {
super(name);
}
@Override
public String getSound() {
return "roar";
}
}
Can an Abstract class be final in Java?
No, an abstract class cannot
be declared as
final
in Java .Because
it will completely negate the purpose of an abstract class. An abstract class
should be extended to create instances. If it is declared final, then it cannot
be extended and so an abstract class cannot be declared as final.
To getting expert-level training for JavaTraining in your location
– java training in chennai | java training in bangalore | java training in pune | java training in chennai | java training in bangalore | java training in tambaram | java training in omr | java training in velachery | java training in annanagar | java training in chennai | java training in marathahalli | java training in btm layout | java training in jayanagar | java training in rajaji nagar | For getting javaonline
training | java online training
No comments:
Post a Comment