Pages

Sunday, 28 June 2015

Class Inheritance

Class Inheritance

Above image shows you the structure of cats family. We can see that cat's anchestor shared some same behaviour to its .... . We take the same term in object oriented programming and we call it as inheritance. Inheritance allows you to define a new class that incorporates and extends an already declared class.
  • You can use an existing class, called the base class, as the basis for a new class, called the derived class. The members of the derived class consist of the following:
    • The members in its own declaration
    • The members of the base class
  • To declare a derived class, you add a class-base specification after the class name. The class-base specification consists of a colon, followed by the name of the class to be used as the base class. The derived class is said to directly inherit from the base class listed.
  • A derived class is said to extend its base class, because it includes the members of the base class plus any additional functionality provided in its own declaration.
  • A derived class cannot delete any of the members it has inherited.
Here's the syntax to inherit a class from a base class
class [Derived Class] : [Base Class]
{ 
 ...  
}
Lets take a look at our person class. A person can be a student or an employee. So student and employee will derive all of behaviour and property from person class.
class person
{
 public string firstName;
 public string lastName;
 public ushort age;

 public void greet()
 {
  Console.WriteLine("Hello "+ firstName + " " + lastName);
 }

 public void talk(string words)
 {
  Console.WriteLine(firstName+" "+ lastName + " say "+ words);
 }
}

class student : person
{
 public string school;
}

class employee : person
{
 public string company;
}

Look at the main example to know how to access inherited class:
class Program
{
 static void Main(string[] args)
 {
  employee me=null;
  student mySon = null;

  me = new employee();
  me.firstName =  "Yang";
  me.lastName = "Sopiana";
  me.company = "Company.inc";
  me.age = 29;
  me.greet();

  mySon = new student();
  mySon.firstName = "Zidni";
  mySon.lastName = "Ilman";
  mySon.school = "Elementary School";
  mySon.age = 6;
  mySon.greet();
 }
}

The code will display following output:
Hello Yang Sopiana
Hello Zidni Ilman

employee and student class are inherited from person class. That's way all property person has both of employee and student will have too. employee has additional field named company and student has another additional field named school.
Lets see our Main
Line 8 Assigning memory space for employee class to object variable me.
Line 9 Assign me's firstName field with "Yang".
We can see in employee declaration, there's no such field. But since employee is inherited from person class, firstName field will be available for employee class. Other field will be inherited too like we can see in line 10 and line 12.
Line 11 Assign company's firstName field with "Company.inc".
This field is employee's special field. You can not access this field from other classes except employee class.
Line 13 Invoke greet method.
We can see in employee declaration, there's no such method. But since employee is inherited from person class, greet method will be available for employee class.
Line 15-20 The comands to access student member. To be remembered that student class was inherited from person class. So every field and method person class has will be available in student class.

0 comments:

Post a Comment