Java Abstract class part2



Java Abstract Class continued In the previous java abstract class tutorial, I explained what is the abstract class and abstract methods. Then I provided a step by step example. Everything went smooth until I decided to add more abstract methods to my Smartphones abstract class which surly resulted in application crush and errors. If you missed the previous tutorial, I suggest you read it first since there will be a straight relationship between this tutorial and the previous one. The abstract class tutorial can be found Here.

Here is the scenario: We added some abstract methods to Smartphones abstract class in order to handle new added features to Smartphones such as an integrated barcode scanner, an integrated RFID reader and even a spying application. Once we added those abstract methods, the application stop working and throwing errors. Why? Because the added abstract methods to the Smartphones abstract class must be implemented by all the subclasses that inherit from it (imagine the subclasses are huge, it will be painful to do the change). Thus, those added abstract methods were not implemented and this will result in program or application to crush unless all the clients implement them. In this case, we are forcing the clients to implement these abstract methods. The move is certainly not very professional. So, what is the solution?

The solution is to create another abstract class that extends or inherits form Smartphones abstract class. In this way, the clients have the choice to upgrade their applications by using the newly created abstract class or the clients continue to use the old version and keep their applications unchanged.

I created a new abstract class named Newfeatures that extends or inherits from the Smartphones abstract class. Now, the clients who want to upgrade to the new version, they must implement the added abstract methods by extending the Newfeatures abstract class instead of Smartphones abstract class. If you missed the previous tutorial about the abstract class, maybe you are wondering what does the Smartphones abstract class look like. Here it is.

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 Newfatures abstract class which inherits from Smartphones has two abstract methods. If it was not a tutorial about abstarct class, I would declare it as an Interface. In the next tutorial I will deal with java interface.

The Newfeatures abstract class is implemented as shown below.

public abstract class Newfeatures extends Smartphones{
public abstract void hasIntegratedScanner(String YesNo);
public abstract void hasSpying(String answer);
}

The rest of the code will be based on the Newfeatures abstract class. If you want to see clearly the difference between how the application was created using Smartphones abstract class and how it's created using the Newfeatures abstract class, then read first the previous tutorial about abstract class. Let's implement Android class, Iphone class and the main class Testall using the Newfeatures abstarct class.

The following is the Android class.

public class Android extends Newfeatures{
String makers = null;
String osVersion = null;
String screenResolution = null;
String serialNumber = null;
String model = null;
//Implementation of abstract class PhoneMaker
public void PhoneMaker(){
makers = "Samsung";
System.out.println("Phone Maker: " + makers);
}
//Implementation of abstract class phoneSpecs
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);
}
//Implementation of abstract class hasIntegratedScanner
public void hasIntegratedScanner(String YesNo){
if(YesNo.equals("Yes"))
{
System.out.println("Itegrated Scanner: Yes");
}
else {
System.out.println("Itegrated Scanner: No");
}
}
//Implementation of abstract class hasSpying
public void hasSpying(String answer){
if(answer.equals("Yes")){
System.out.println("Has Spy Program: Yes");
}
else{
System.out.println("Has Spy Program: No");
}
}
}


The Iphone class that inherits form the Newfeatures abstract class is shown in the code below.

public class Iphone extends Newfeatures{
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);
}
public void hasIntegratedScanner(String YesNo){
if(YesNo.equals("Yes"))
{
System.out.println("Itegrated Scanner: Yes");
}
else {
System.out.println("Itegrated Scanner: No");
}
}
public void hasSpying (String answer){
if(answer.equals("Yes")){
System.out.println("Has Spy Program: Yes");
}
else{
System.out.println("Has Spy Program: No");
}
}
}

At this point we have an abstract class named Newfeatures , that inherits from Smartphones abstract class. And we have two subclass that inherit from Newfeatures 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 and it's implemented as shown below. But wait, before jumping to Testall class implementation, it's important to follow the steps below.

How to perform the test

To successfully perform a test do the following.
First create a class file and name it Smartphones.java and copy and paste the code of Smartphones abstract class.Just after that, create a class file and name it Newfeatures.java and copy and paste the code of Newfeatures class. Then create two other class files and name them 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. Now all the steps are completed, here is the Testall class implementation.

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();
myiPhone.hasIntegratedScanner("Yes");
myiPhone.hasSpying("Yes");
}
}


I hope that this post was helpful to you.