Important Keywords in Java

Looking at the table, you must think there are several keywords that you don’t understand. I want to say don’t worry about them. We will be talking about each of these keywords with a small example program. Important Note – Java does not support Goto and Const keywords anymore. They were in use when Java was introduced. When Java was introduced, Goto and Const were in use; with the evolution of Java, other keywords took over, and Goto, Const became useless.Important Note – All of these keywords start with a small letter in real programming world. If I mention anywhere any keyword starting with a capital word please ignore that.

Abstract

We have already discussed the keyword in our previous Java Object-oriented programming language concept article. The abstract keyword can be used to declare any class as an abstract class and any method as an abstract method. This type of class comes into use when we don’t have the implementation of any method. We want the child class to provide the implementation of this abstract method.

Abstract Class

In this class at least one abstract method is available.This type of class can have abstract and non-abstract methods.This type of class can have variables and properties.The object cannot be created for this type of class.This type of class can be extended and the child class will use the non-abstract methods and provide the functionality (implementation) of abstract methods.

Example Program

Let’s take an example to understand the abstract class and method. Imagine you are building an operating system for a smartphone. Somebody has already developed the functionality of feature phones that are Calling and SMS in it. You are going to add the Internet and Listening to music functionality to it.

FeaturePhone Class Code

package phone.featurephone; public class FeaturePhone {

public void CallFeature(long PhoneNumber) {
	
	System.out.println("Calling to "+ PhoneNumber);
	
}

public void SMS(long PhoneNumber, String DeliveryMessage) {
	
	System.out.println("Sending Message to " +PhoneNumber+ " "+DeliveryMessage );
	
	}

}

Smartphone Class Code

package phone; import phone.featurephone.FeaturePhone; public class SmartPhone extends FeaturePhone{

public void InternetAccess() {
	System.out.println("Using Internet");
}

public void ListeningToMusic() {
	System.out.println("Playing Music");
}

public void CheckingBloodPressure() {
	
}

}

ExecutionClass Code

package phone; public class ExecutionClass { public static void main(String[] args) {

	SmartPhone phone = new SmartPhone();
	phone.CallFeature(1234500000);
	phone.SMS(1234000000, " Hi Jim, How are you?");
	phone.InternetAccess();
	phone.CheckingBloodPressure();
	phone.ListeningToMusic();
	phone.CheckingBloodPressure();
}

}

Result Of the Program

You can see the smartphone class has inherited the Featurephone’s methods, and the smartphone has its own methods like InternetAccess() and ListeningToMusic(). You have also noticed that there is one more method in the smartphone called CheckingBloodPressure(). We are trying to use the CheckingBloodPressure() method, but it does nothing because it has no implementation. You had this idea that a smartphone will be connected to an external device and collect the data to calculate the blood pressure, but you don’t know how to do this at this point. You have two options: either give up on this idea completely or make this bloodpressure() method an abstract class. Once you declare this method as an abstract method, the entire class will be declared as an abstract class. If someone in the future uses your program, he will see that you had this idea but did not have the implementation for it. He will start working on it and will provide the implementation for CheckingBloodPressure() method. If the class is an abstract class, this becomes the responsibility of the person who will inherit it; he will implement all abstract methods.

SmartPhone Class with Abstract Method and Class

package phone; import phone.featurephone.FeaturePhone; public abstract class SmartPhone extends FeaturePhone{

public void InternetAccess() {
	System.out.println("Using Internet");
}

public void ListeningToMusic() {
	System.out.println("Playing Music");
}

abstract public void CheckingBloodPressure();

}

Conclusion – Abstract, the keyword is essential because it compels the person (who will inherit your class) to provide the implementation for your abstract methods. Your ideas will be researched by someone else and will be implemented.