Summary
The following article is the second of a three-part article series that presents definitions and samples for different Object-Oriented Programming (OOP) concepts and its implementation in .NET. The first part examined the concepts of classes, objects, and structures. This part examines the concepts of inheritance, abstraction, and polymorphism. The third and last part will examine the concepts of interface, multiple interface inheritance, collections, and overloading.
Introduction
In Part 1 of Object-Oriented Programming Concepts and .NET, I defined the concepts of class, object, and structure. In addition to defining the concepts, I explained real world samples and presented sample code in C# and VB.NET to create classes and structs. The first article also explains objects as independent building blocks.
In Part 2 of Object-Oriented Programming Concepts and .NET, I will explain the concepts of inheritance, abstraction, and polymorphism. I will also present a Unified Model Language (UML) class diagram to represent an object model that will help as a visual aid to explain some concepts. The purpose of this article is to explain a series of relationships between objects.
Inheritance
In the real world there are many objects that can be specialized. In OOP, a parent class can inherit its behavior and state to children classes. This concept was developed to manage generalization and specialization in OOP and is represented by a is-a relationship.
The following OOP terms are commonly used names given to parent and child classes in OOP:
- Superclass: Parent class.
- Subclass: Child class.
- Base class: Parent class.
- Derived class: Child class
The most common real world sample to explain inheritance is the geometric shapes object model. Squares, circles, triangles, rectangles, pentagons, hexagons, and octagons are geometric shapes. The following figure shows a sample set of geometric figures:

Figure 1: Geometric shapes
The concept of generalization in OOP means that an object encapsulates common state and behavior for a category of objects. The general object in this sample is the geometric shape. Most geometric shapes have area, perimeter, and color. The concept of specialization in OOP means that an object can inherit the common state and behavior of a generic object; however, each object needs to define its own special and particular state an behavior. In Figure 1, each shape has its own color. Each shape has also particular formulas to calculate its area and perimeter.
Inheritance makes code elegant and less repetitive. If we know that all shapes have color, should we program a color attribute for each shape? The answer is no! Would it be a better idea to create a shape class that has a color attribute and to make all the specialized shapes to inherit the color attribute? The answer is yes!
An object model for this sample could have a shape parent class and a derived class for each specific shape. The following UML class diagram shows the set of classes needed to model the geometric shapes sample. Observe the field, properties, and methods for each class,

Figure 2: Class Diagram
The Shape class is the parent class. Square, Rectangle, and Circle are derived classes that inherit from Shape. The triangle-connector in the diagram represents an is-a relationship.
The .NET framework has many base classes. Everything is derived from System.Object. You can create almost anything you imagine using the built-in functionality provided in the .NET Framework Class Library.
To create a derived class in C#, the class declaration should be done as:
class child: parent
To create a derived class in VB.NET, the class declaration should be done as:
Classchild
Inheritsparent
EndClass
Multiple inheritance
Multiple inheritance is the possibility that a child class can have multiple parents. Human beings have always two parents, so a child will have characteristics from both parents.
In OOP, multiple inheritance might become difficult to handle because it allows ambiguity for the compiler. There are programming languages such as C++ that allow multiple inheritance; however, other programming languages such as Java and the .NET Framework languages do not allow multiple inheritance. Multiple inheritance can be emulated in .NET using Multiple Interface Inheritance, which I will explain in Part 3 of this series.
Sealed class
A sealed class is a class that does not allow inheritance. Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.
To create a sealed class in C#, the class declaration should be done as:
sealedclassShape
To create a sealed class in VB.NET, the class declaration should be done as:
NonInheritable ClassShape
Abstraction
Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).
An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.
Figure 2 shows a Shape class, which is an abstract class. In the real world, you never calculate the area or perimeter of a generic shape, you must know what kind of geometric shape you have because each shape (eg. square, circle, rectangle, etc.) has its own area and perimeter formulas. The parent class shape forces all derived classes to define the behavior for CalculateArea() and CalculatePerimeter(). Another great example is a bank account. People own savings accounts, checking accounts, credit accounts, investment accounts, but not generic bank accounts. In this case, a bank account can be an abstract class and all the other specialized bank accounts inherit from bank account.
To create an abstract class in C#, the class declaration should be done as:
abstractclassShape
To create an abstract class in VB.NET, the class declaration should be done as:
MustInheritClassShape
To following code shows a sample implementation of an abstract class:
Code
using System;
namespace DotNetTreats.OOSE.OOPSamples
{
public abstract class Shape
{
private float _area;
private System.Drawing.Color _color;
private float _perimeter;
public float Area
{
get
{
return _area;
}
set
{
_area = value;
}
}
public System.Drawing.Color Color
{
get
{
return _color;
}
set
{
_color = value;
}
}
public float Perimeter
{
get
{
return _perimeter;
}
set
{
_perimeter = value;
}
}
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}
}
Listing 1: The Shape abstract class in C#
Polymorphism
Polymorphism allows objects to be represented in multiple forms. Even though classes are derived or inherited from the same parent class, each derived class will have its own behavior. Polymorphism is a concept linked to inheritance and assures that derived classes have the same functions even though each derived class performs different operations.
Figure 2 shows a Rectangle, a Circle, and Square. All of them are shapes and as shapes their area and perimeter can be calculated; however, each shape calculates its area in a specialized way. Declaring a member as abstract allows polymorphism. The Shape class defines the CalculateArea() and CalculatePerimeter() methods as abstract, this allows each derived class to override the implementation of the parent's methods.
To following sample code shows an implementation of a derived class (rectangle). The specific CalculateArea() and CalculatePerimeter() methods for the rectangle class illustrate polymorphism:
Code
using System;
namespace DotNetTreats.OOSE.OOPSamples
{
class Rectangle: Shape
{
private float _height;
private float _width;
public rectangle(float height, float width)
{
_height = height;
_width = width;
}
public float Height
{
get
{
return _height;
}
set
{
_height = value;
}
}
public float Width
{
get
{
return _width;
}
set
{
_width = value;
}
}
public override void CalculateArea()
{
this.Area = _height * _width;
}
public override void CalculatePerimeter()
{
this.Perimeter = (_height * 2) + (_width * 2);
}
}
}
Listing 2: Polymorphism represented in the Rectangle's methods
Virtual keyword
The virtual keyword allows polymorphism too. A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.
To create a virtual member in C#, use the virtual keyword:
publicvirtualvoidDraw()
To create a virtual member in VB.NET, use the Overridable keyword:
PublicOverridableFunctionDraw()
Override keyword
Overriding is the action of modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.
To override a member in C#, use the override keyword:
publicoverridevoidCalculateArea()
To override a member in VB.NET, use the Overrides keyword:
PublicOverridesFunctionCalculateArea()
Conclusion
Inheritance allows developers to manage a generalization and specialization relationship between objects. OOP concepts such as abstraction and polymorphism help to define better object models where object hierarchies are designed with reusability in mind. In this article, I examined the concept of inheritance, abstraction, and polymorphism. The third and last part of this series will examine the concepts of interface, multiple interface inheritance, collections, and overloading.
Note The sample source code* for this article works only in Visual Studio 2005.
Reference
- Matt Weisfeld, The Object-Oriented Thought Process, SAMS, 2000.
- Robin A. Reynolds-Haertle, OOP with Microsoft Visual Basic .NET and Microsoft Visual C# .NET Step by Step, Microsoft Press, 2002.
Asad MoosaPosted Jun 7, 2020, 8:38 AM
Where is Part 3 where you want to mention "Multiple inheritance can be emulated in .NET using Multiple Interface Inheritance, which I will explain in Part 3 of this series."? There are no organized links which is why there is a very confusing search.
Dinesh GabhanePosted Nov 7, 2019, 8:32 AM
Very Nice Article
Subin ThomasPosted Jan 31, 2019, 12:00 AM
This article is good to clear the concepts. very good
Joe WilsonPosted Jan 11, 2017, 7:55 AM
Thank you very much for this article.
BeginnerPosted Nov 1, 2016, 4:20 AM
Very hepfull
Ramesh PalaniappanPosted Aug 18, 2016, 8:15 AM
Good one
Soumalya DasPosted Aug 7, 2016, 1:01 PM
Really very good one
kalu singh raoPosted Jul 7, 2016, 8:38 AM
Nice...
Bhuvanesh MohankumarPosted Apr 19, 2016, 2:31 PM
Good one
shaban aliPosted Apr 8, 2016, 2:59 AM
Nice
Vignesh ManiPosted Mar 21, 2016, 5:08 PM
Nice
Kashif SohailPosted Mar 16, 2016, 11:23 AM
very very great explaination
Sonu ChaudharyPosted Feb 25, 2016, 6:29 AM
share more article
Asfend YarPosted Feb 21, 2016, 9:52 AM
keep it up
Asfend YarPosted Feb 21, 2016, 9:52 AM
very valuable information
Asfend YarPosted Feb 21, 2016, 9:51 AM
thanks for sharing
Asfend YarPosted Feb 21, 2016, 9:51 AM
nice
Jithil JohnPosted Feb 16, 2016, 2:12 AM
Good one
Sr KarthigaPosted Feb 12, 2016, 4:31 AM
Nice one sir
sridhar thotaPosted Jan 19, 2016, 6:49 AM
good explanantion..
Nanddeep NachanPosted Jan 11, 2016, 12:56 PM
Nice share
Ankur MistryPosted Nov 29, 2015, 9:27 AM
Good one
Mohamed Gani MnPosted Nov 6, 2015, 11:08 AM
Better to touch
Sibeesh VenuPosted Oct 21, 2015, 1:12 AM
Nice Share
Mukesh KumarPosted Oct 21, 2015, 12:36 AM
Good Work, Great
Mohammed IbrahimPosted Oct 20, 2015, 11:02 PM
nice
Priyaranjan K SPosted Oct 20, 2015, 12:28 PM
Nice Share
Nilesh JadavPosted Oct 20, 2015, 11:31 AM
Good one !!
Avi TiwariPosted Oct 20, 2015, 7:41 AM
Good Article. I have observed that a class defined as Abstract may not have any abstract method and still it will compile successfully but the vice versa not true.
Banketeshvar NarayanPosted Oct 16, 2015, 3:32 AM
nice article
VINAY KUMAR GUPTAPosted Oct 1, 2015, 9:58 AM
Nice article
kamal upadhyayPosted Sep 29, 2015, 7:34 AM
Good one
Pramod GuptaPosted Sep 18, 2015, 6:54 AM
like it
Harshad PansuriyaPosted Sep 12, 2015, 3:45 AM
Nice Share
Ahtasham HassanPosted Aug 22, 2015, 4:50 PM
good one :p
Yashwanth MuthineniPosted Aug 22, 2015, 2:44 AM
Nice Share...
SharadPosted Jul 17, 2015, 3:38 AM
good one...
Govinda Rajulu YemineniPosted Jul 14, 2015, 5:10 AM
Good one
Rajeev kumar GuptaPosted Jun 18, 2015, 1:34 PM
Good article
Abdul Momin Tasleem AnsariPosted Jun 11, 2015, 6:12 AM
Nice Explaination...
Mohammad KhalidPosted Jun 9, 2015, 6:11 AM
This is really good article. Thanks for sharing.
Ravi PatelPosted May 29, 2015, 3:24 AM
well explanation
Shailesh UkePosted May 27, 2015, 10:13 AM
well done that's good
T ByrdPosted Jan 8, 2013, 7:50 AM
This is excellent and has helped my understanding. When will part 3 be published?
arun stalinPosted Sep 22, 2012, 2:41 AM
http://www.c-sharpcorner.com/UploadFile/AuthorImage/DefaultAuthttp://www.c-sharpcorner.com/UploadFile/AuthorImage/DefaultAuthhttp://www.c-sharpcorner.com/UploadFile/AuthorImage/DefaultAuthorImage.giforImage.gifhorImage.gif
Former memberPosted Jul 3, 2012, 3:28 PM
need more examples
keyur soniPosted Jun 15, 2012, 6:55 AM
this is excellent
Murty VenkatPosted May 15, 2012, 2:17 AM
Very helpful
niranjan reddyPosted Dec 7, 2010, 10:39 AM
http://www.programcall.com/csnet/3/oops-class-and-object.aspx
unnam hyndaviPosted Sep 21, 2010, 1:06 AM
very nice
Antony RichardPosted Aug 29, 2010, 1:32 PM
The article above is short and sweet. Why?, I already know Oops concept. I was not in touch with it for along time. So what I need is a small touch and that is what I got. Well got it.
GowthamPosted Aug 13, 2010, 12:29 PM
Hi thank you very much the tutorial is very good and I've learned a lot can you please tell me where I can find the third part
Govind EmandiPosted Aug 10, 2010, 8:09 AM
Good Explanation
Avdhesh JoshiPosted Aug 9, 2010, 1:58 AM
Thank you very much. By this article my concept clear with .net Concepts. Thanks again.
Deepak PathakPosted Jun 9, 2010, 10:28 AM
can any one send the exact concrete definitions of interface, multiple interface inheritance, collectionsoverloading,inheritance, abstraction, and polymorphism.
Pankaj KumarPosted May 24, 2010, 4:21 PM
Info is real great in Part 1 and 2 and I am looking forward to read Part 3 Article. Can anybody provide me the link ?
Arun KumarPosted May 3, 2010, 5:14 AM
Thanks a lot.. It helps me lot to crack interviews
Santosh PandeyPosted Apr 15, 2010, 3:07 PM
Nice article to understand the OOP terminology.. Thanks Very much
Lata PatelPosted Apr 7, 2010, 2:02 AM
Thanks..the definition of Abstract class, overriding concepts is clear now.
saif emonPosted Mar 5, 2010, 8:09 AM
i need more sample code that i can feel the amusement of C#.
maheshPosted Mar 2, 2010, 3:00 AM
any body who r experienced could explain OOP
Dharmendra KumarPosted Sep 3, 2009, 6:40 AM
vbnvbnvb
satyamPosted Aug 13, 2009, 10:38 PM
I want to learn .Net Concepts
sak thiPosted Jun 10, 2009, 5:35 AM
iam a beginer its very usefull to me. thanx
ved sharmaPosted May 10, 2009, 3:17 AM
Its nice article by which we can learn more easily the oops concepts.n thanx for this article.
jeneesh velayudhanPosted Feb 16, 2009, 1:11 AM
It was very nice and easy to understand article!
raghav khungerPosted Nov 1, 2008, 10:55 AM
Very Good Article!
venu raoPosted Mar 1, 2008, 1:36 PM
i can't find the third part ,can any one help me getting out this
gurjeet sainiPosted Feb 12, 2008, 3:36 AM
Part 1 & 2 are very helpful to us what third the last part in which you are going to explain "interface, multiple interface inheritance, collections, and overloading" Please let us know
KUMARPosted Dec 15, 2007, 8:47 AM
Hi, it very good reference article, could you pls send me third part too?
Chris GrechPosted Dec 8, 2007, 1:28 PM
Great stuff...explained very well...
sujeet kumareditedPosted Nov 21, 2007, 10:30 PMEdited Nov 21, 2007, 10:33 PM
I found these two article is really help full for us. 1. OOPS Concepts and .NET Part 1 2. OOPS Concepts and .NET Part 2 But at same time I foud "OOPS Concepts and .NET Part 3" is missing from the site. could any one plz give me the link for "OOPS Concepts and .NET Part 3" Thanx Sujeet
swethaeditedPosted Oct 25, 2007, 4:29 PMEdited Oct 25, 2007, 10:39 PM
The first two parts are very very useful.Did anyone find the third part? If anybody have please send the third part link... Thanks Swetha
SrikanthPosted Oct 12, 2007, 2:52 AM
Hi, I have a Parent Class A and a child Class B(inherited from A). I've created an object of both 'a' from A and 'b' from B. It seems i can do b=a; //possible. but a=b: not possible why? If you can explain me it would be of great help for me thanks.
Gopi RamanaPosted Sep 22, 2007, 11:03 AM
Great Article on OOPS with simple example.
bobPosted Sep 10, 2007, 3:19 PM
Does the 3rd part exists?
Srinivasan HariharanPosted Aug 6, 2007, 1:22 PM
Very nice article. It is useful , refresh your memory
gurjeet sainiPosted Jul 29, 2007, 5:42 AM
Hi Erika This article is very helpful and hope third part will be like this one.When you will provide third part of this series. Thanks Gurjeet Saini
Kunal YandePosted Jun 16, 2007, 2:46 AM
Hello Erica, You do this topic very easy and helpful. Some juniour ask me to explain abouve topic and i am helpless to how can i explain them. So I search net and I got ur Article.It was very easy explain and to understand also. Thanks. Kunal
ashok kumarPosted Jun 14, 2007, 1:22 AM
very nice material abt basic inheritance and also explained in a simple way.........................
venugopalachary darojuPosted Jun 13, 2007, 8:33 AM
1 and 2nd parts are very good. but could you provide 3rd part link.
arvindPosted May 22, 2007, 12:20 PM
Where can i get part three of this article?
Ravi KhandelwalPosted May 7, 2007, 2:50 AM
Hi frnds, I want "Part I" of this article (I joined little late). Can anyone tell me, how can I get the Part I? Thanks
Ravi KhandelwalPosted May 7, 2007, 2:39 AM
Hi frnds, I want "Part I" of this article (I joined little late). Can anyone tell me, how can I get the Part I? Thanks
Ritesh SinghPosted Apr 30, 2007, 5:01 AM
it is very very good, i also Part3
Jeff KentPosted Apr 29, 2007, 1:08 AM
Good reference material!
Sachin RankaPosted Apr 25, 2007, 7:58 AM
very very impressive articles but yeah i wld like to get teh third part which explains abt interfaces also.