×

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

Java.sql.Time Format

In JDBC API as a cover aroundjava.util.Date that handles SQL-specific requirements we use the java.sql.Time. The java.sql.Time extends java.util.Date class. To represent SQL TIME, without a date this class is...

6 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Array Programs in Java

Array Programs in Java: An array is a data structure that stores similar elements in a contiguous memory location. In Java, an array is an object that stores the same...

7 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Hidden classes in Java

There specifically are some APIs available in the market that generally is harmful to be used in our programs specifically literally, and until JDK 15, there, for all intents and...

4 minutes read.

Java Integer toUnsignedLong() method

The toUnsignedLong() method of Java Integer class returns a long value by simply converting the given argument to long after an unsigned conversion. Syntax public static long toUnsignedLong (int  x) Parameters The parameter ‘x’...

1 minute read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.