Escolar Documentos
Profissional Documentos
Cultura Documentos
characters. These twin brothers looks alike but they hold different characters.
Inheritance: Look at the image below you will get a point, Yes, the derived class can access
base class properties and still the derived class has its own properties.
example:
class base_class() {
def bproperty1()
def bproperty2()
def bproperty3()
}
class derived_class() {
def dproperty1()
}
baseclass_obj1 = base_class()
derivedclass_obj2 = derived_class()
Now derivedclass_obj2 can access dproperty1 also the 3 base class properties. Here the access
modifiers play a role that which property can be accessed by the derived class.
Class
Let say you are going to buy a car, the showroom manager asks you some details which are
color,model,engine type(Petrol/Diesel),Dashboard and seat color.
From the above situation we can understand about class and objects. The class here is the "CAR"
and you are actually getting an object of the class, it may be whatever brand with the inputs you
provided to the showroom manager.
So whenever we need to create an object(buying a car), we need to give some details(mandatory
input ie., attribute of the class) to create the object.
Constructor:
Now you have provided the inputs needed for the car, these inputs will be processed by the
manufacturing plant(constructor) and return you the car(object). So whenever you create an
object a function called constructor is called to create that object.
Encapsulation:
Encapsulation helps us to show/hide what user can access. Let say the car class has some of its
own functions like accelerate, change the gear position, apply break, start/stop engine etc.., these
are the functions which user can access which means it has been set as Public. There can also be
some functions which car required like to know the fuel level and gear position status to start the
engine which the car itself handles on its own, so this has been set as Private.
Polymorphism:
Lets take the example of some words which can be used in different situations with different
meaning. 'watch' - watch can be used as a verb as well as a noun. "He is wearing a watch." In this
it is used as a noun. "Watch your actions." In this it is used as a verb. Thus we see that the same
word can be used in different situations differently. Likewise same operators or function names
are used with various meanings in various situations..
Inheritance: It is very simple. we ourselves are an example of this. technically, we are derived
from our parents and they are derived from grandparents and so on. But there is a difference. the
derived class is always bigger than the base class but in our case, the child is always smaller than
parents.
Encapsulation: How do u think a car runs? Is there only steering, brake and whatever you are
able to see directly?? NO. There is many more parts inside to make it run. Engine, wires, tank
etc. but we don't see it. Thus we can say that car is basically a collective unit of many parts
which help it to run. Same way, in classes, we have functions and data members wrapped up
together.
Abstraction: Lets continue with the above example. Suppose you start going for driving classes.
Does the instructor teach you about the functioning of the engine?? NO. Why ?? Because he
knows that for driving, you don't need to know about the functioning of engine. all those are
kept hidden from you.
1. Car Vs Class
Car: Collection of several properties like staring, break, clutch, etc each things are used for
particular purpose, all these properties are differ from each car like some car have power
staring ,however we have called its a car.
Class: its a collection of functions and variables. The functions and variables are differing from
each class. Each function is used for particular purpose however we have called its a class
2. Car Key Vs Object
Car Key: key is used for run the car. So many dummy keys can use for run a car.
Object: Object is used for run the class or invokes the class. So many objects can create for a
single class.
3. Birds Vs Abstractions
Birds: we invented flight based on the mechanism of Birds. So flight is derived form the base of
birds.
Abstraction: Abstraction is a way to remove the association of the behavior of an object with the
actual details behind the scenes which implement that object's behavior. This 'abstraction' is
usually accomplished through the use of base classes with virtual functions; each derived
function provides the details that implement the behavior behind that abstraction.
4. Pen Vs Encapsulation
Pen: Ink is the important component in pen but it is hiding by some other material
Encapsulation: is a process of binding or wrapping the data and the codes that operates on the data into a single
entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a
protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the
wrapper.
5. Crocodile Vs Polymorphism
Crocodile: live indifferently on land or in the water. In water its Moment very fast compare to land. An
animal lives in different character in different place.
Polymorphism: a single function or single operator has different character in different place.
OOPS Features
* The object oriented programming (OOP) is a programming model
where Programs are organized around object and data rather than
action
and
logic.
The Program is divided into number of small units called Object. The
data and function are build around these objects.
The data of the objects can be accessed only by the functions
associated with that object.
The functions of one object can access the functions of other object.
"Object
is
Object
Software
is
bundle
an
of
related
instance
variable
of
and
methods.
"
class
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, which is called as an object.
When an object is created by using the keyword new, then memory
will be allocated for the class in heap memory area, which is called
as an instance and its starting address will be stored in the object in
stack memory area.
When an object is created without the keyword new, then memory
will not be allocated in heap I.e. instance will not be created and
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.
class Employee
{
}
Syntax to create an object of class Employee:-
essential
feature
without
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 object
by providing relevant information.
Abstraction is the process of hiding the working style of an object,
and showing the information of an object in understandable manner.
Real world Example of Abstraction: Suppose you have an object Mobile Phone.
Suppose you have 3 mobile phones as 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, Reading E-mails)
Abstract information (Necessary and Common Information) for the
object "Mobile Phone" is make a call to any number and can send
SMS."
so that, for mobile phone object you will have abstract class like
following:abstract class MobilePhone
{
public void Calling();
public void SendSMS();
}
Encapsulation is like your bag in which you can keep your pen, book
etc. It means this is the property of encapsulating members and
functions.
class Bag
{
book;
pen;
ReadBook();
}
Encapsulation means hiding the internal details of an object, i.e.
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 the other object.
Hide the data for security such as making the variables as private,
and expose the property to access the private data which would be
public.
So, when you access the property you can validate the data and set
it.
Example:
class Demo
{
private int _mark;
public int Mark
{
get { return _mark; }
set { if (_mark > 0) _mark = value; else _mark = 0; }
}
}
Real world Example of Encapsulation:Let's take example of Mobile Phone and Mobile Phone Manufacturer
Suppose you are a Mobile Phone Manufacturer and you designed and
developed a Mobile Phone design(class), now by using machinery
you are manufacturing a Mobile Phone(object) for selling, when you
sell your Mobile Phone the user only learn how to use the Mobile
Phone but not that how this Mobile Phone works.
This means that you are creating the class with function and by
making object (capsule) of it you are making availability of the
functionality of you class by that object and without the interference
in the original class.
Example-2:
TV operation
It is encapsulated with cover and we can operate with remote and
no need to open TV and change the channel.
Here everything is in private except remote so that anyone can
access not to operate and change the things in TV.
Inheritance:
When a class acquire the property of another class is known as
inheritance.
Inheritance is process of object reusability.
For example, A Child acquire property of Parents.
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Output:
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Polymorphism:
object
is
called
As phone
As camera
As mp3 player
As radio
Encapsulation
data.
what the object does instead of how internal details or mechanics of how
it does it
in terms of implementation.
For Example:-
The easier way to understand Abstraction and encapsulation is as follows:Real World Example:-
Take an example of Mobile Phone:You have a Mobile Phone, you can dial a number using keypad buttons. Even
you don't know how these are working internally. This is called Abstraction.
You have the only information that is needed to dial a number. But not its
internal working of mobile.
But how the Mobile Phone internally working?, how keypad buttons are
connected with internal circuit? is called Encapsulation.
Summary:
"Encapsulation is accomplished by using Class. - Keeping data and methods
that accesses that data into a single unit"
"Abstraction is accomplished by using Interface. - Just giving the abstract
information about what it can do without specifying the back ground details"
"Information/Data hiding is accomplished by using Modifiers - By keeping
the instance variables private or protected."