OOP Features: Class
OOP Features: Class
Abstraction,
Encapsulation,
Inheritance
Polymorphism.
OOP Features
Object Oriented Programming (OOP) is a programming model where programs are organized around
objects and data rather than action and logic.
OOP allows decomposition of a problem into a number of entities called objects and then builds data and
functions around these objects.
1. The software is divided into a number of small units called objects. The data and functions are built
around these objects.
2. The data of the objects can be accessed only by the functions associated with that object.
3. The functions of one object can access the functions of another object.
Class
A class is the core of any modern Object Oriented Programming language such as C#.
A class is a blueprint of an object that contains variables for storing data and functions to perform
operations on the data.
A class will not occupy any memory space and hence it is only a logical representation of data.
To create a class, you simply use the keyword "class" followed by the class name:
Object
Objects are the basic run-time entities of an object oriented system. They may represent a person, a place
or any item that the program must handle.
1/10
7/18/2017 Introduction to Object Oriented Programming Concepts in C#
A class will not occupy any memory space. Hence to work with the data represented by the class you
must create a variable for the class that is called an object.
When an object is created using the new operator, memory is allocated for the class in the heap, the
object is called an instance and its starting address will be stored in the object in stack memory.
When an object is created without the new operator, memory will not be allocated in the heap, in other
words an instance will not be created and the object in the stack contains the value null.
When an object contains null, then it is not possible to access the members of the class using that object.
All the programming languages supporting Object Oriented Programming will be supporting these three
main concepts,
1. Encapsulation
2. Inheritance
3. Polymorphism
Abstraction
Abstraction is "To represent the essential feature without representing the background details."
Abstraction lets you focus on what the object does instead of how it does it.
Abstraction provides you a generalized view of your classes or objects by providing relevant information.
2/10
Abstraction is the process of hiding the working style of an object, and showing the information of an
Object in an understandable manner.
Real-world Example of Abstraction
Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as in the following: Nokia 1400 (Features: Calling, SMS) Nokia 2700
(Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features: Calling, SMS, FM Radio, MP3, Camera, Video Recording, and Reading E-mails)
Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes
a call to any number and can send SMS.
So that, for a mobile phone object you will have the abstract class as in the following,
Abstraction means putting all the variables and methods in a class that are necessary.
Example
If somebody in your collage tells you to fill in an application form, you will provide your details, like name,
address, date of birth, which semester, percentage you have etcetera.
If some doctor gives you an application to fill in the details, you will provide the details, like name,
address, date of birth, blood group, height and weight.
Age, name and address, so you can create a class that consists of the common data. That is called an
abstract class.
3/10
Encapsulation
Wrapping up a data member and a method together into a single unit (in other words class) is called
Encapsulation.
Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to
an object into that object.
Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the
property of encapsulating members and functions.
Encapsulation means hiding the internal details of an object, in other words how an object does
something.
Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is
implemented.
Encapsulation is a technique used to protect the information in an object from another object.
Hide the data for security such as making the variables private, and expose the property to access the
private data that will be public.
So, when you access the property you can validate the data and set it.
Example 1
4/10
Example 2
It is encapsulated with a cover and we can operate it with a remote and there is no need to open the TV
to change the channel.
Here everything is private except the remote, so that anyone can access the remote to operate and
change the things in the TV.
Inheritance
Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Polymorphism
5/10
Example 2
A person behaves the son in a house at the same time that the person behaves an employee
Polymorphism in .Net
Abstraction Encapsulation
1. Abstraction solves the 1. Encapsulation solves the problem in the
problem at the design level. implementation level.
2. Encapsulation means hiding the code and data
2. Abstraction hides unwanted
into a single unit to protect the data from the
data and provides relevant data.
outside world.
3. Abstraction lets you focus on 3. Encapsulation means hiding the internal
what the object does instead of details or mechanics of how an object does
how it does it something.
Real-world Example
You have a Mobile Phone; you can dial a number using keypad buttons. You don't even know how these
are working internally. This is called Abstraction. You only have the information that is necessary to dial a
number, but not internal working the mobile.
But how does the Mobile Phone work internally? How is the keypad buttons connected with internal circuit?
That is called Encapsulation.
6/10
7/10