Java Abstract class part1



Abstract class An abstract class is a class that has at least one abstract method. Moreover, the abstract class can contain data fields and none or several implemented methods. An abstract class can not be instantiated , in other words you can not create an instance of the abstract class. All the abstract methods of an abstract class must be implemented by a subclass which inherits from it, otherwise the subclass must declare itself as an abstract class. The abstract class is declared by using the abstract word in the class header. See the example below.

Java Abstract method

So, what is an abstract method and how is it declared?
An abstract method is a method without implementation (no body and no {}) and is declared by using the keyword abstract. The following is the general declaration of an abstract method.
public abstract return-type method-name(arguments);
Example:
public abstract void phoneSpecs();
To clarify things, I'll provide you with a full example of an abstract class and its use.
As you know, there are different smart-phone makers such as Google, Samsung, Apple, Microsoft and so on. We end up with different makers, but the Smartphones have something in common. So, consider the following abstract class.

public abstract class Smartphones {
// Data field declaration
String message = null;
//Abstract class can have implemented method
public void displayMessage()
{ message = "Smartophone specification:";
System.out.println(message);
}
// Abstract methods, they should be implemented in concrete class
//also known as subclass
public abstract void PhoneMaker ();
public abstract void phoneSpecs();

}

The abstract class Smartphones has one data field, one implemented method and two abstract methods. The abstract methods have to be implemented by any subclass that inherits from it or make use of it. If the subclass fails to do so it has to declare itself as an abstract class. To implement the abstract methods of an abstract class in a subclass, use the keyword extends. Let's see our first subclass named Android that implements the abstract methods of Smartphones abstract class to meet its specific needs.

public class Android extends Smartphones{
String makers = null;
String osVersion = null;
String screenResolution = null;
String serialNumber = null;
String model = null;
public void PhoneMaker(){
makers = "Samsung";
System.out.println("Phone Maker: " + makers);
}
public void phoneSpecs(){
osVersion = "Android 4.2 Jelly Bean";
screenResolution = "1920 x 1080-pixel";
serialNumber = "123456789";
model = "Galaxy S4";
System.out.println("OS Version: " + osVersion);
System.out.println("Screen Resolution: " + screenResolution);
System.out.println("Serial Number: " + serialNumber);
System.out.println("Phone Model: " + model);
}

The same abstract methods of the abstract class Smartphones can be implemented differently for example by Apple for its iPhone brand to meet its specific needs. Below is the Iphone class that implements the abstract methods of Smartphones abstract class.

public class Iphone extends Smartphones{
String makers = null;
String osVersion = null;
String screenResolution = null;
String serialNumber = null;
String model = null;
public void PhoneMaker(){
makers = "Apple";
System.out.println("Phone Maker: " + makers);
}
public void phoneSpecs(){
osVersion = "iOS 7";
screenResolution = "1136 x 640 -pixel";
serialNumber = "9009997865";
model = "iPhone 7S";
System.out.println("OS Version: " + osVersion);
System.out.println("Screen Resolution: " + screenResolution);
System.out.println("Serial Number: " + serialNumber);
System.out.println("Phone Model: " + model);
}


At this point we have an abstract class named Smartphones and two subclass that inherit from it named Android and Iphone. To test their functionality, you must create an entry point class which is a class with main method that enables java to execute your program. I named the class Testall as shown below.

public class Testall {
public static void main(String[] args) {
Android myPhone = new Android(); //Instance of Android class
myPhone.displayMessage();
myPhone.PhoneMaker();
myPhone.phoneSpecs();
myPhone.hasIntegratedScanner("Yes");
myPhone.hasSpying("Yes");
System.out.println("========================== iPhone Specs =============");
Iphone myiPhone = new Iphone();//Instance of Iphone class
myiPhone.displayMessage();
myiPhone.PhoneMaker();
myiPhone.phoneSpecs();
}
}

I haven't provided you with the examples output. I left it to you. So, to put everything together and to successfully perform a test do the following.

First create a class file and name it Smarptphone.java and copy and paste the code of Smartphones abstract class. Then create two other class files named respctively Android.java and Iphone.java and copy and paste the corresponding code to each class. Finally, create a class file and name it Tesall.java then copy and paste the code of Testall class. I would like to add that copy and paste is not a good way to learn. I suggest that you type the code.

Something very important

Now, you should feel comfortable with abstract class and abstract methods. But wait a second, what will happen if you decide to add more abstract methods to the abstract class Smartphones, known that the abstract class Smartphones is implemented by different clients. Sure, everything will crash and stop working. So what is the solution? This is another story. However, if you are interested in knowing the solution, follow the link below.

the solution is Here.