Java Language Keywords – Abstract, Super, Static, Volatile Keywords

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In our previous keyword article, we studied the two most useful Java keywords, enum and strictfp, with examples. Today, in this Java language keywords,  we are going to discuss Abstract, Super, Static, and Volatile keywords with real-time examples.

So, let’s start exploring each Java language keyword.

Java Language Keywords

There are 51 keywords in the Java language. Today, we will explore the 4 major ones.

Java Language Keywords

Abstract Keyword in Java

In Java keywords, the abstract keyword defines that it is a non-access modifier which is applicable for classes and methods, but it is not applicable for variables. These Java language keywords are used to achieve abstraction in Java, which happens to be a key feature of OOP.

Let us study different contexts in detail.

Abstract class in Java

The class that doesn’t have all its methods declared or has a partial implementation is known as a Java Abstract class.

Syntax of abstract class in Java –

abstract class class-name
{
   //body of class
}

Due to the partial implementation, we usually cannot instantiate the abstract classes, i.e., all the subclasses of an abstract class should either execute the greater part of the abstract methods in the super-class or announce abstract itself. Some of the predefined classes in Java are abstract. They rely upon their subclasses to give finished usage. For instance, java.lang. A number is a dynamic class.

Sometimes, we require just a declaration of the method in Java super-classes. This can be accomplished by indicating the abstract sort modifier.

Syntax  –

abstract type method-name(parameter-list);
package com.dataflair.abstractclass;
//abstract parent class
abstract class Animal
{
      //abstract method
      public abstract void sound();
}
//Dog class extends Animal class
class Cat extends Animal
{
      public void sound(){
            System.out.println("Meow");
      }
}
public class AbstractClassExample
{
      public static void main(String args[]){
            Animal obj = new Cat();
            obj.sound();
      }
}

Output –

Example of Abstract Class in Java

Don’t forget to check the difference between an abstract class and an interface in Java!

Super Keyword in Java

The super keyword in Java is a variable that is used to indicate parent class objects. The keyword “super” came into the scene with the concept of Java Inheritance.  It is mainly used in the contexts mentioned below:

1 Use of super with variables

This thing occurs when a derived class and base class carry the same data members. In that case, there might be a possibility of ambiguity for the JVM. It can be understood more clearly using the code snippet:

class Vehicle
{
   int maxSpeed = 120;
}
class Car extends Vehicle
{
   int maxSpeed = 180;
   void display()
   {
       System.out.println("Maximum Speed: " + super.maxSpeed);
   }
}
class Test
{
   public static void main(String[] args)
   {
       Car small = new Car();
       small.display();
   }
}

Output-super keyword using variables in JavaIn the example above, both the base class and the subclass have a member maxSpeed in common. We could access the maxSpeed of the base class in sublcass using super keyword.

2 Use with methods

It is done when we need to call the method of the parent class. To avoid ambiguity in classes with the same name, we use this  Super keyword.

package com.dataflair.keyword;
class Person
{
  void message()
  {
    System.out.println("This is person class");
  }
}
class Student extends Person
{
  void message()
  {
    System.out.println("This is student class");
  }
  void display()
  {
    message();
    super.message();
  }
}
public class SuperWithMethods {
  public static void main(String args[])
  {
    Student studentObject = new Student();
    studentObject.display();
  }
}

Output-super keyword in Java with method

Before we proceed, you should revise the concept of Constructors in Java.

3 Use with constructors

It is used to access the parent class constructor. It can be both parametric and non-parametric.  

package com.dataflair.keyword;
class Parent
{
  Parent()
  {
    System.out.println("Parent class Constructor");
  }
}
class Child extends Parent
{
  Child()
  {
    super();
    System.out.println("Child class Constructor");
  }
}
public class SuperWithConstructor {

  public static void main(String[] args)
  {
    Child childObject = new Child();
  }
}

Output-

super-with-constructor

Static Keyword in Java

Java Static is a non-access modifier which is appropriate for accompanying:

  • Blocks
  • Variables
  • Methods
  • Nested classes

To make a static member(block, variable, method, nested class), go before its declaration with the keyword static. At the point when a member is declared static, it gets to before any objects of its class are made, and without reference to any object. For instance, in beneath java program, we are getting the static method m1() without making any protest of Test class.

Master the concept of Java Static Keyword in single-shot!!

Volatile Keyword in Java

The volatile keyword in Java modifies the value of a variable using different threads. It makes the class thread-safe. In other words, multiple threads can use a single method and instance of the classes at the same time without. It can be used with primitive types or objects.

We cannot use the volatile keyword with classes or methods. However, it can be used with variables. Reordering of code is also prevented by using the volatile keyword.

Syntax of the volatile keyword in Java –

class VolatileDemo
{
   static int number = 10;
}

In the following code, suppose two threads are in the same class. But both of them are on different processors, where each thread has a local copy of the variable ‘number’. If any thread tries to change the value of ‘number’, the value will not change, or the change will not be reflected in the main memory.

Let’s explore the important facts about Multithreading in Java

class VolatileDemo
{
   static volatile int number = 10;
}

In the following code, the static variables are the members of a class, and they are shared among all objects. Only one copy exists in the main memory. It is not possible to store the value of a volatile variable in the cache. All read-write operations will be done from the main memory to the main memory.

Summary

Keywords are defined names that are associated with the methods, classes, or variables. It contains the specific task assigned to them in Java. Hence, we cannot use the keyword for defining the variable, method, or class because it violates the task related to that keyword. If the Keywords are used, the properties related to them will be applied.

Java language keywords (reserved words/predefined keywords) add value to code. We can’t use them as a variable name, class name, method name, or object name.

Hope you like the article, and now you can easily practice the abstract, volatile, super, and static keywords.

If you face any queries or doubts, feel free to ask in the comments section.

It’s time to uncover the concept of Final Keywords in Java

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

Your email address will not be published. Required fields are marked *