We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Monday, July 13, 2015

Abstract Classes and Abstract Methods

A Method without any method body is known as an Abstract method. What it contains is only the signature or declaration. To define an Abstract method we require to use an Abstract modifier on the method.
The class under which these abstract methods will be defined is referred to as an Abstract class, which should also be declared using Abstract modifiers

    abstract class Class1
    {
        public abstract void Add(int x, int y);
    } 

The concept of Abstract methods is near similar to the concept of Method overriding , wherein overriding parent class declares any of its methods as virtual giving a permission to the child class for re-implementing the Method whereas in case of Abstract methods parent classes declares any of its methods as abstract asking the child classes to implement the Abstract methods, which was mandatory for child classes.


Abstract Classes

An abstract class can contain both abstract and nonabstract methods in int. But any of its child class if it wants to consume the non-abstract methods, first it should provide the implementation for all abstract methods.

*An Abstract class is never useful to itself because we can't create the object of it.

*Add a class "AbsParent.cs" and write the following by making the class as Abstract.
abstract class AbsParent
    {
        public void Add(int x, int y)
        {
            Console.WriteLine(x + y);
        }
        public void Sub(int x, int y)
        {
              Console.WriteLine(x - y);
        }
        public abstract void Mul(int x, int y);
        public abstract void Div(int x, int y);
    }


*Add a class "AbsChild.cs" and write the following
    class AbsChild : AbsParent
    {
        public override void Mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public override void Div(int x, int y)
        {
            Console.WriteLine(x / y);
        }
        static void Main()
        {
            AbsChild obj = new AbsChild();
            obj.Add(100, 45); obj.Sub(43, 34);
            obj.Mul(454, 45); obj.Div(43, 23);
            Console.ReadLine();
        }

    }

Even if we cannot create the object of an abstract class, it is possible to create a reference of it using child class object and with that reference, we can call the non-abstract methods of an abstract class as well as abstract methods of the abstract class that are implemented under the child class.

To test this rewrite the code under child class Main method as following
AbsChild obj=new AbsChild ();
AbsParent p=obj;
p.Add (100, 68); p.Sub (43, 32);
p.Mul (55, 454); p.Div (43, 32);
Console.ReadLine ();

*Abstract classes can be used for providing properties to its child classes as well as for imposing restrictions on child classes also.


*In the above example "Figure"(Diagram) is abstract class that contains all the common attributes that can be used under different figures like Rectangle,Circle,Triangle,Cone...etc and also it contains two abstract methods for imposing the restrictions on all its child classes which should be implemented by each child separately according to the type of Figure it is.

Note: - Abstract classes can always be Parent classes only. So they will always sit in the top of the hierarchy.

*Add a class "Figure.cs" and write the following


public abstract class Figure
    {
        public double width, height, radius;
        public const float pi = 3.14f;
        public abstract double GetArea();
        public abstract double GetPermieter();

    }
*Add a class "Rectangle.cs" and write the following
    public class Rectange : Figure
    {
        public Rectangle(double width, double height)
        {
            //Here using 'this' or 'base' will be same
            this.width = width;

            base.height = height;
        }
        public override double GetArea()
        {
            return width * height;
        }
        public double GetPermieter()
        {
            return 2 * (width + height)       
}


    }
*Add a class "Circle.cs" and wirte the following
    public class Circle : Figure
    {
        public Circle(double radius)
        {
             this.radius = radius;
        }

        public override double GetArea()
        {
            return pi * radius * radius;
        }

        public override double GetPerimeter()
        {
            return 2 * pi * radius;
        }

    }


*Add a class "TestFigure.cs" and write the following
   class TestFigures
    {
        static void Main()
        {
            Rectangle r = new Rectangle(12.45, 43.23);
            Console.WriteLine(r.GetArea());
            Console.WriteLine(r.GetPermieter());
            Circle c = new Circle(32.43);
            Console.WriteLine(c.GetArea());
            Console.WriteLine(c.GetPerimeter());
            Console.ReadLine();

        }


    }
Interface
             It is also a 'type' as a class but can contain only Abstract members in it.
Interface --> Only Abstract Members
Class --> Only Non-Abstract Members
Abstract Class --> Both Abstract and Non-Abstract Members

All the rules and guidelines we have discussed in the case of abstract class apply to an interface also. That is the implementation of the abstract methods that are declared inside the interface should be implemented by its children.

Interfaces are used in the Distributed application development, which is applications that execute in Client-Server Architecture.
Multiple Inheritance is possible with this interface, that is a class can have only one class as its immediate parent. But the same class can have any number of interfaces as its parent.
Inheritance can be categorized into two types
a) Implementation Inheritance
b) Interface Inheritance

* If a class is inheriting from another class we will call it as implementation inheritance


* In Java and .NET Lang’s, this is only single, whereas if the class is inheriting from an interface we call it as interface inheritance. But this is Multiple in all object-oriented Lang’s.


Syntax for Interface

[<modifiers>] interface <Name>
{
-Abstract member declarations
}

*Interfaces can't contain any variable declarations

*Default scope of members in the interface is public
*By default interface members are abstract, which doesn't require explicit declaration.
*Just like a class can inherit from another class, an interface can inherit from another interface but not from class.

*Add an Interface item "Inter1.cs" and write the following

    Interface Inter1

    {

void Add(int x,int y);

void Sub(int x,int y);

void Test();

    }

* Add another Interface item "Inter2.cs" and write the following

Interface Inter2

    {

void Mul(int x, int y);

void Div(int x,int y);

void Test();


    }
To implement the method of both these interfaces add a class "InterClass.cs" and write the following.
class InterClass : Inter1, Inter2
    {
        public void Add(int x, int y)
        {
            Console.WriteLine(x + y);
        }

        public void Sub(int x, int y)
        {
            Console.WriteLine(x - y);
        }
        public void Mul(int x, int y)
        {
            Console.WriteLine(x * y);
        }
        public void Div(int x, int y)
        {
            Console.WriteLine(x / y);

        }

      

        void Inter1.Test

{
            Console.WriteLine("Declared under Interface1");
        }
            void Test2.Test()
        {
            Console.WriteLine("Declared under Interface2");
        }
        static void Main()
        {

            InterClass obj = new InterClass();
            obj.Add(43, 334); obj.Sub(34, 32);
            obj.Mul(43, 23); obj.Div(32, 223);
        
            Inter1 i1 = obj; Inter2 i2 = obj;
            i1.Test(); i2.Test();
            Console.ReadLine();
        }

    }



Earlier we have been discussed that multiple inheritances are not supported through classes. Because of the Ambiguity problem whereas interfaces multiple inheritances are supported as the problem of ambiguity if it occurs here it can be resolved in two different ways.

Note: - In the case of class to class inheritance child class is consuming members of its parent class. So if ambiguity comes into the picture, there is a confusion in executing the method of parent classes. So there is a restriction for multiple inheritances through classes, but in the case of Interface to class inheritance class is not consuming its parent members, it is only implementing them.


The two solutions to resolve the problems are

1) Implement the Ambiguous method of both interfaces only for a single time under the class, where both interfaces assume their own method is implemented and executes. Here you can call the method directly by using the object of the class.

2) We can also implement the Ambiguous method of both interfaces separately under the class by prefixing the method with the name of the interface. In this context, while invoking the method, we need to use the reference to the interface to which the method belongs.


Partial Classes

It is an application, which allows splitting of a class and defining it under multiple files. So that huge volumes of code can be put on different files, where the Manageability becomes easier.
This also allows Multiple Programmers to work on the same class at the same time.
While defining partial classes we can split them into any number of files, but on each file, the 'class' name should be the same and we need to use the modifier 'partial'.
In case of partial classes if we want to inherit from any other class we need to do it only in one single file, which takes to the complete class. Because here the class is physically separated into multiple files, but Logically it is one only.

*Add a 'Codefile' in the project naming it as "Part1.cs" and write the following.

partial class Parts
        {
               public void Method1()
            {
                Console.WriteLine("Method1");
            }
            public void Method2()
            {
                Console.WriteLine("Method2");
            }

        }


*Add a 'Codefile' "Part2.cs" and the write the following.
partial class Parts
            {
                public void Method3()
                {
                    Console.WriteLine("Method3");
                }

                public void Method4()
                {
                    Console.WriteLine("Method4");
                }

            }
*Add a 'class' "TestParts. Cs" and write the following.

   class TestParts
                {

                    static void Main()
                    {
                        Parts p = new Parts();
                        p.Method1(); p.Method2();
                        p.Method3(); p.Method4();
                        Console.ReadLine();
                    }

                }

No comments: