Wednesday, 22 January 2014

OOP Demo: Abstract Classes, Inheritance, Polymorphism, Encapsulation

In OOP, an abstract class is a class object that acts as the base/main class for all other classes to inherit from. In an abstract class, there is no implementation allowed, only declaration. Also, even if you make a constructor inside an abstract class, creating an instance of an abstract class is not allowed (i.e. if you try and do this, you will get compiler errors: A newObject = new A(); where A is an abstract class)

An abstract class is simply that of its namesake, its abstract. There's nothing specific about it, but it will have properties and functions that other classes can use/have a relationship to the base abstract class object.

The following code shows an abstract class named Shape, with subclasses Square, Triangle, and Circle, who all use inheritance to inherit 1 function from the abstract class. All the subclasses will use the single function called 'CalculateArea()' where the polymorphism comes in.

Polymorphism in a plain definition, is a function that can do different things depending on whichever class is using it. In other words: one function, many uses. In the example below, all objects implement a CalculateArea() function, note that finding the area for different shapes, the implementation is different, but the function name stays the same.

Encapsulation is simply protecting any properties, functions, or that is subject to change from outside code. We can also create a class object within a class object to hide an object from outside access that don't need to use the object. In the example below, we give properties like length and width of a square to be private so that when we declare a Square object, we initialize using the public constructor like so: Shape square = new Square(l, w);

abstract class Shape//this abstract Shape class is our base class to inherit from
    {//abstract classes are created by using the 'abstract' keyword
        public abstract int CalculateArea();
       /*abstract classes may or may not have abstract  functions/fields, in this case we'll make an abstract function called CalculateArea, and please look closely at the syntax in C#, if you're doing  Java it can be different.*/
    }
//we have a subclass called Square with its own unique characteristics, it inherits from Shape
    class Square : Shape
    {
        private int length;
        private int width;
        public Square() { }//empty constructor
        public Square(int length, int width)//overloading, we will pass in a length & width
        {
            this.length = length;
            this.width = width;
        }
        public override int CalculateArea()//pay close attention here: any functions inherited from an abstract or virtual class MUST contain the 'override' keyword
        {
            int area = this.length * this.width;
            return area;//and must return the same type
        }
    }
//the same routine is done for the next classes that inherit from Shape
    class Triangle : Shape
    {
        private int baseLength;
        private int height;
        public Triangle() { }
        public Triangle(int baseLength, int height)
        {
            this.baseLength = baseLength;
            this.height = height;
        }
        public override int CalculateArea()
        {
            int area = this.baseLength * this.height / 2;
            return area;
        }
    }
    class Circle : Shape
    {
        private double radius;
        public Circle() { }
        public Circle(double r)
        {
            this.radius = r;
        }
        public override int CalculateArea()
        {
            int area = (int)(Math.Pow(this.radius, 2) * Math.PI);
            return area;
        }
    }
class Program
{
     static void Main()
    {
//we create new objects of type shape and we initialise with a specific shape
            Shape square = new Square(5,10);
//so a Shape called square is initialised by giving by saying new Square(length, width)
//we do this because we want to create a new Shape and calculate its area by specifying that its a //square
//the same is done for the triangle and circle  
            Shape triangle = new Triangle(10, 32);
            Shape circle = new Circle(7);

//we write out the areas of the shapes created
            Console.WriteLine("Area of square: {0} units", square.CalculateArea());
            Console.WriteLine("Area of triangle: {0} units", triangle.CalculateArea());
            Console.WriteLine("Area of circle: {0} units", circle.CalculateArea());


Console.ReadLine();//keep the window open
    }
}

An illustration the hierarchy:

No comments:

Post a Comment