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 have 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 keywords.
Java Language Keywords
There 51 keywords in Java language. Today we will explore the 4 major ones.
1. Abstract Keyword in Java
In Java keywords, an 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 used to achieve abstraction in Java which happens to be a key feature of OOP.
Let us study different contexts in detail.
1.1 Abstract class in Java
The class which doesn’t have its all method declared or has a partial implementation is known as Java Abstract class.
Syntax –
abstract class class-name { //body of class }
Due to the partial implementation, we usually cannot instantiate the abstract classes, i.e. all the subclass 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 sub-classes to give finish 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 accomplish 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 –
Don’t forget to check the difference between Abstract class and Interface in Java!
2. 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 majorly used in the contexts mentioned below:
2.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 a possibility of ambiguity for the JVM. It can understand 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-In the example above, both base class and subclass have a member maxSpeed in common. We could access maxSpeed of the base class in sublcass using super keyword.
2.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(); } }
Before we proceed, you should revise the concept of Constructors in Java.
2.3 Use with constructors
It is used to access parent class constructor. It can 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-
3. 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 revelation with the keyword static. At the point when a member is proclaimed static, it get to before any objects of its class are made, and without reference to any object. For instance, in beneath java program, we are getting too static technique m1() without making any protest of Test class.
Master the concept of Java Static Keyword in single-shot!!
4. Volatile Keyword in Java
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 type or objects.
We cannot use a 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 –
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 of the thread is having 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 reflect 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
Java language keyword (reserved words/predefined keywords) are adding 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 abstract, volatile, super, and static keyword.
If you face any query or doubt feel free to ask in the comment section.
Its’ time to uncover the concept of Final Keywords in Java
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google