Monday, 11 December 2017

Object Oriented Programming(OOP) Explained with Java Examples (Part 3)

In this lesson, we are going to talk about Abstract Classes.
But before we discuss abstract classes, let us consider a scenario like this:

You have an attribute Address and GetAddress in the Person class. What happens when you also have an attribute Address and GetAddress in the Patient class?
I Java, if an attribute or method in a subclass have the same name, return type and parameters as the one in the superclass, then the one in the subclass overrides the one in the superclass. Make sure you understand this point.

Abstract Classes and Methods
Abstract class is a class that contains one or more abstract method. An abstract method is a method that is declared, but does not provide any implementation.
You cannot create objects from abstract classes (abstract classes cannot be instantiated).
This means that an abstract class must have a subclass to provide implementation for the abstract methods.

Example
Let us take an example of a class Shape. This class would have an abstract method called Draw(). The subclasses would be Line, Rectangle, and Triangle. So open Netbeans and follow the procedure

Step 1: Create an application in NetBeans and name it AbstractLesson

Step 2: Create a class called Shape. Remember to place in a new package. Give the package a name MyClasses.

Step 3: Add two properties, name and color of String type

Step 4: Add a constructor that takes the two properties

Step 5: Add the method Area(), with no implementation

If you have performed the steps correctly, then your file would be like shown in the picture below:


Step 6: Generate the constructor the this class

Step 7: Next add a class Triangle, with extends Shape with two properties, length and height

Step 8: Add a method Draw(). This method would have just one line of implementation as shown below

return  1/2 * width * height

If you have created this class correctly, then it would look like the one shown below:



Step 9: Now add the Rectangle class by following the method for Triangle. If you have done it correctly, then your file would look like the one shown below.


Let's now test the program and explain how it works

Step 10: Open the main program and import the three classes using this statment after the package statement.

import MyClasses.Shape;
import MyClasses.Rectangle;
import MyClasses.Triangle;


Then try to run the following code one after the other and observe the outcome.
        public Shape myShape = new Shape();  //fails
        public Triangle myTriangle = new Triangle("T1", "Blue"); //correct      
        public Rectangle myRectangle = new Rectangle("R1","Green");  //correct

        public Shape NewShape = new Triangle("T2","Red");  //correct

Explanation: The first line fails because you cannot instantiate an abstract class
The next lines works
The last last line works two because by the principle of inheritance: "a triangle is a shape"

Note: If you have an abstract method in any class, then that class becomes an abstract class and you must declare it as abstract. However, you can have an abstract class that does not contain an abstract method.

When do you Need Abstract Classes
You need abstract class when:
  • It does not make sense to create objects of that class. Here it does not make sense to instantiate a shape because you don't know how it would look like.
  • When objects of the subclasses of that class type behaves in different ways
  • When you need to use an abstract method

Final Notes
  • An abstract class cannot be instantiated
  • You cannot have an abstract method inside a non-abstract class.
In the next Lession, we would examine the Static and the Final keywords.