×

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

Related Topics

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Converting Long to Date in Java

What Long and Date are in Java and how are they implemented in the Java programming language are the topics of this article. Additionally, we'll go into great detail on...

4 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

Minimum Window Subsequence in Java

In this article, you will be very well acknowledged about the minimum window subsequence, what is the approach and how it is implemented. The example program is also executed and...

4 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.