Java

Java Keywords – Java Is A Language of 50 Keywords

java is a language of keywords

Freshly we have studied about Object Oriented Programming Concept and this time we will be discussing the necessary keywords in Java programming. In any programming language, the number of keywords is huge but some keywords come in use very frequently. Today, we will be talking about those keywords. Please have patience and wait for all the parts of this article because it is very difficult to talk about all 50 keywords in one piece.

Important Keywords in Java

abstractfor continuenewswitch
assertdefaultgotopackagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthroughs
caseenuminstanceofreturntransit
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile

Looking at the table you must be thinking there are several keywords that you don’t understand at all. I would like 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 in 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 and you are going to add the Internet and Listening to music functionality in 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

Result
Result

You can see the Featurephone’s methods have been inherited by smartphone class 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 this is doing 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 you give up on this idea completely or make this bloodpressure() method as 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 if an abstract class, this becomes the responsibility of the person who will inherit this class, he will provide the implementation for 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 very important 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.

Our sponsor - Deploy your server within 3 minutes


Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More