Chapter 5,
INHERTITANCE AND POLYMORPHISM
One of the most important concept of object oriented programming languages is inheritance.
inheritance helps in reusing the code, hence helping the programmer save time in the coding process by making use of the already written logic/code in multiple situations
The class inheritance:
Inheritance in class level may be represented in the following way.
A base class which contains fields, methods etc, can be inherited into a derived class, and the derived class will contain all the fields / methods defined in the base class.
For example:
using System;
namespace Logiphix
{
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("BaseClass Object created");
}
public void printMessage()
{
Console.WriteLine("This method was defined inside BaseClass");
}
}
public class DerivedClass : BaseClass
{
public DerivedClass ()
{
Console.WriteLine("DerivedClass Object created");
}
}
public class InheritanceExample
{
static void Main()
{
DerivedClass dcObj = new DerivedClass();
dcObj.printMessage();
Console.ReadKey();
}
}
}
Explanation:
The above example defines three classes, namely BaseClass , DerivedClass, InheritanceExample.
The BaseClass will act as the parent class in this example and this class has a method defined in it called printMessage this method will print an message on the console as "This method was defined inside BaseClass"
The DerivedClass will act as the child class and inherits the parent class BaseClass, when doint so it also inherits all its member methods and fields. hence inheriting the method printMessage as well.
the third class InheritanceExample contains the main method for this application and creates an object of the class DerivedClass when the object is created using the new keyword , note that the base class constructor is called first and then the derived class constructor is called.
Finally when the printMessage method is called with respect to the derived class object, the method is succesfully executed, even though the method was defined inside the base class and not inside the dreived class. this shows us that the method was inherited from the BaseClass(which is the parent class) into the DerivedClass class(which becomes the child class)
The output for the code is as below
INHERTITANCE AND POLYMORPHISM
One of the most important concept of object oriented programming languages is inheritance.
inheritance helps in reusing the code, hence helping the programmer save time in the coding process by making use of the already written logic/code in multiple situations
The class inheritance:
Inheritance in class level may be represented in the following way.
A base class which contains fields, methods etc, can be inherited into a derived class, and the derived class will contain all the fields / methods defined in the base class.
For example:
using System;
namespace Logiphix
{
public class BaseClass
{
public BaseClass()
{
Console.WriteLine("BaseClass Object created");
}
public void printMessage()
{
Console.WriteLine("This method was defined inside BaseClass");
}
}
public class DerivedClass : BaseClass
{
public DerivedClass ()
{
Console.WriteLine("DerivedClass Object created");
}
}
public class InheritanceExample
{
static void Main()
{
DerivedClass dcObj = new DerivedClass();
dcObj.printMessage();
Console.ReadKey();
}
}
}
Explanation:
The above example defines three classes, namely BaseClass , DerivedClass, InheritanceExample.
The BaseClass will act as the parent class in this example and this class has a method defined in it called printMessage this method will print an message on the console as "This method was defined inside BaseClass"
The DerivedClass will act as the child class and inherits the parent class BaseClass, when doint so it also inherits all its member methods and fields. hence inheriting the method printMessage as well.
the third class InheritanceExample contains the main method for this application and creates an object of the class DerivedClass when the object is created using the new keyword , note that the base class constructor is called first and then the derived class constructor is called.
Finally when the printMessage method is called with respect to the derived class object, the method is succesfully executed, even though the method was defined inside the base class and not inside the dreived class. this shows us that the method was inherited from the BaseClass(which is the parent class) into the DerivedClass class(which becomes the child class)
The output for the code is as below
No comments:
Post a Comment