By Team Escalera
on 31 Oct 2017 03:17 PM
  • asp .net training in lucknow
0 comments

Inheritance

It can be defined as an “Is – A” relationship.

A Class has Parameters and Methods. There can be another class with same parameters as the Main class.

In this case the Main Class is known as Base Class and the Other Class is known as derived class as it will “Inherit” the methods and parameters from the Base Class.

Let Shape is a Class and Base, RadiusHeight and Width are the parameters of this class.

A Class Named Circle can inherit the Radius Parameter of this Class to calculate area.

A Class Named Square can use either Height or Width to calculate area.

A Class Named Rectangle can use either Height & Width to calculate area.

A Class Named Triangle can use Height & Base to calculate area.

Hence there is no need to declare the parameters again and again. One piece of code can be reused multiple times.

Why inheritance?

You have the answer:

  1. A piece of code can be re-used.
  2. It is an important pillar in Object Oriented Programming.
  3. Reusable code saves time and ensures faster execution of program.

The code

USING SYSTEM;

USING SYSTEM.COLLECTIONS.GENERIC;

USING SYSTEM.LINQ;

USING SYSTEM.TEXT;

USING SYSTEM.THREADING.TASKS;

 

NAMESPACE CONSOLEAPPLICATION3

{

    USING SYSTEM;

    NAMESPACE INHERITANCEAPPLICATION

    {

        CLASS SHAPE

        {

            PROTECTED INT WIDTH;

            PROTECTED INT HEIGHT;

            PROTECTED DOUBLE RADIUS;

            PROTECTED INT BASELENGTH;

            PUBLIC VOID USEWIDTH(INT W)

            {

                WIDTH = W;

            }

            PUBLIC VOID USEHEIGHT(INT H)

            {

                HEIGHT = H;

            }

            PUBLIC VOID USERADIUS(INT R)

            {

                RADIUS = R;

            }

            PUBLIC VOID USEBASELENGTH(INT B)

            {

                BASELENGTH = B;

            }

 

        }

 

        // LET’S CREATE DERIVED CLASSES

        CLASS CIRCLE : SHAPE

        {

            PUBLIC DOUBLE GETAREA()

            {

                RETURN (3.14 * RADIUS * RADIUS);

            }

        }

        CLASS SQUARE : SHAPE

        {

            PUBLIC INT GETAREA()

            {

                RETURN (HEIGHT * HEIGHT);

            }

        }

        CLASS RECTANGLE : SHAPE

        {

            PUBLIC INT GETAREA()

            {

                RETURN (WIDTH * HEIGHT);

            }

        }

        CLASS TRIANGLE : SHAPE

        {

            PUBLIC DOUBLE GETAREA()

            {

                RETURN (.5 * BASELENGTH * HEIGHT);

            }

        }

 

        CLASS RECTANGLETESTER

        {

            STATIC VOID MAIN(STRING[] ARGS)

            {

 

                CIRCLE CIR = NEW CIRCLE();

 

                CIR.USERADIUS(5);

 

                // PRINT THE AREA OF THE OBJECT.

                CONSOLE.WRITELINE(“AREA OF CIRCLE IS : {0}”,  CIR.GETAREA());

                CONSOLE.READKEY();

 

           // SIMILARLY YOU CAN CALCULATE OTHER AREAS BY CREATING OBJECTS     

 

            }

        }

    }

 

}

Comments (0)

Leave A Comment