Name: Introduction to inheritance
Need to know: The examples are in C#.NET however the topics discussed should be easily portable to any OO language.
In this tutorial I hope to give you an insight into a specific area of Object Oriented Programming. That area is Inheritance.
Inheritance in a nutshell is: a way to create new classes (therefore objects) by using a class(es) that has already been defined.
Brief overview of classes and objects
A class is a programming construct used to group related attributes and methods.
An object is an instance of a class.
Example: a class may be Person with attributes like name, age, location and methods like wakeUp, goToWork and so on.
An object of that class could be you, me, anybody.
Step 1 - Our parent class.
Below is a class which I just created. It does not know it yet but ParentClass will soon be a parent! Here is the class.
Listing 1 explained:Code:public class ParentClass { public ParentClass() { Console.WriteLine("I am the Constructor of ParentClass."); } public int addNumbers(int num, int num2) { return num + num2; } }
That class has a Constructor and one method. The method named addNumbers returns the value of num + num2.
You should note that because the class ParentClass has a constructor, when we create an instance of the class the constructor will execute.
Step 2 - Our child class.
The main benefit of creating a new class from an already defined class is you can reuse existing code.
Lets say we want to be able to add 2 numbers together in another class. Why rewrite the same functionality when we have a class that already does that?
So, we want to create a new class that can access ParentClass's methods and attributes. Heres how.
Listing 2 explained:Code:public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("I am the Constructor of ChildClass which is infact a ParentClass."); } public static void Main() { ChildClass child = new ChildClass(); Console.WriteLine(child.addNumbers(5, 12).ToString()); Console.ReadLine(); } }
The first line
creates a new class called ChildClass which inherits the methods and attributes of ParentClass.Code:public class ChildClass : ParentClass
ChildClass has a Constructor and a Main() method. When Main() is called because this class is a child of ParentClass, the constructor of ParentClass is executed first, then the Constructor of ChildClass and then addNumbers.
The output of executing this file is:
Theres one more thing I want to show you.I am the Constructor of ParentClass.
I am the Constructor of ChildClass which is infact a ParentClass.
17
As you can see, ChildClass does not have a method named addNumbers so it uses the addNumbers method in ParentClass. That is good, it saves repeating code.
However, what if for a particular instance of a class I want to do something different in addNumbers but I still want to inherit the methods and attributes of ParentClass (for example if the ParentClass has 10 methods, your child class needs to use 9 methods but one method needs to have different logic).
It would be stupid to copy the 9 methods directly into the new child class, so you use the already created class. You need to create 1 new method in the child class. Heres how:
Listing 3 explained.Code:public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("I am the Constructor of ChildClass which is infact a ParentClass."); } public new int addNumbers(int num, int num2) { return num - num2; } public static void Main() { ChildClass child = new ChildClass(); Console.WriteLine(child.addNumbers(5, 12).ToString()); Console.ReadLine(); } }
This listing is exactly the same as Listing 2 except I have created a method called addNumbers. Note the keyword new in the
line. This tells the class that this method has the same name as it's parents method.Code:public new int addNumbers(int num, int num2)
So when I run this file the output is:
The ChildClass has a method named addNumbers so it executes that rather than using it's Parents method of the sane name.I am the Constructor of ParentClass.
I am the Constructor of ChildClass which is infact a ParentClass.
-7
I hope this has helped someone gain an understanding of classes and inheritance.


LinkBack URL
About LinkBacks






Reply With Quote
