Java Keywords
Java Keywords
The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are also called as reserved words. These words cannot be used as an object names or variables or class names because, these are predefined words in a java programming language.
In java programming language we have various types of java keywords which are used for different purposes and functionalities to write a code in java language. At most we have 48 to 50 keywords or reserved words in java.
Let us understand regarding the reserved word or keyword “abstract”.
Abstract Keyword
Java Abstract keyword:
- In java, the keyword abstract is used to define or declare the abstraction class.
- It is also the keyword used in achieving abstraction in java language.
- It is used in implementing or creating the abstract class and abstract method by using the non-access modifier.
- Abstract class plays the role in implementing the abstract methods that are contained in abstract class.
- However, abstract classes may also contain the non-abstract methods.
- Abstract method is the method which is declared by the keyword “abstract” and doesn’t have any implementations is called as abstract method.
Syntax of Abstract Keyword
Abstract class Student
{
Abstract void homework ();
}
Rules of the Abstract keyword
Don’ts
- For variable declaration and constructor declaration we cannot use the abstraction keyword.
- If the class is abstract class, then the class cannot be instantiated.
- If the declared method is abstract method, then the method doesn’t contain the body.
- The keyword or reserved word “final” cannot be used by including the abstract keyword.
- Abstract methods cannot be declared as private.
- Abstract methods also cannot be declared as static.
- Abstract methods cannot be synchronized.
Do’s
- The keyword “abstract” is only implemented to be used with the class and the method.
- Constructors and static methods are implemented in the abstract class.
- If the abstract class is been extended by any other class, then it must and should contain or must also should implement at least one of the abstract methods.
- The main method and the final method can declare and developed in the abstract class.
- The abstract class can also contain overloaded abstract methods.
- Abstract class can also be declared as local inner class.
- Abstract method can also be declared with the throw clause.
Examples of Abstract keyword
Example 1: Abstract class that contains the abstract method
Abstract Animal
{
abstracteat ();
}
class Dog extends Animal
{
@Override
voideat () {
System.out.println("Eating the food");
}
}
publicclass AbstractEx1 {
publicstaticvoidmain (String [] s) {
Dog o=newDog ();
o.eat();
}
}
Output:
Eating the food
Example 2: abstract and non-abstract method in Abstract class
abstractclass Animal
{
abstracteats ();
voidbones ()
{
System.out.println("Eating the bones");
}
}
class Dog extends Animal
{
@Override
void eat() {
System.out.println("Eating the food");
}
}
publicclass AbstractEx2 {
publicstaticvoidmain (String[] s) {
Dog d=newDog ();
d.eat();
d.bones();
}
}
Output:
Eating the food
Eating the bones
Example 3: Abstract class containing the constructor
abstract class Animal
{
String mg;
Animal (String mg)
{
this.mg=mg;
}
void display ()
{
System.out.println(mg);
}
}
class Dog extends Animal
{
Dog (String mg) {
super(mg);
}
}
public class AbstractEx3 {
public static void main (String [] args) {
Dog b=new Dog ("Constructor is appealed");
b.display();
}
}
Output:
Constructoris appealed
Example 4: overloaded abstract methods in abstract class
abstract class Animal
{
abstract display ();
abstract display (String mg);
}
class Dog extends Animal
{
@Override
void display () {
System.out.println("abstract method is appealed");
}
@Override
void display (String mg) {
System.out.println(mg);
}
}
public class AbstractEx4 {
public static void main (String [] args) {
Dog j=new Dog ();
j.display();
j.display("overloaded abstract method is appealed");
}
}
Output:
abstract method is appealed
overloaded abstract method is appealed