0% found this document useful (0 votes)
249 views

Abstraction and Encapsulation

Encapsulation involves wrapping up complexity within classes and hiding unnecessary details from other parts of the program. It is achieved through access modifiers like public, private, and protected. Abstraction defines essential characteristics that classes extending an abstract class must implement. For example, an abstract Car class could define characteristics like having four doors, a steering wheel, etc. Concrete classes extending Car would implement these characteristics along with any additional features. Both encapsulation and abstraction are important programming principles, but abstraction requires more skill and is essential for large, complex applications.

Uploaded by

Nitin Soni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
249 views

Abstraction and Encapsulation

Encapsulation involves wrapping up complexity within classes and hiding unnecessary details from other parts of the program. It is achieved through access modifiers like public, private, and protected. Abstraction defines essential characteristics that classes extending an abstract class must implement. For example, an abstract Car class could define characteristics like having four doors, a steering wheel, etc. Concrete classes extending Car would implement these characteristics along with any additional features. Both encapsulation and abstraction are important programming principles, but abstraction requires more skill and is essential for large, complex applications.

Uploaded by

Nitin Soni
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Try this link - http://www.itmweb.com/essay550.

htm

Encapsulation is nothing but showing only those things which are required by
wrapping up complexity & hence it is also called that it leads to data hiding but its
not data hiding. Never say its data hiding… so never say its data hiding, its
wrapping up complexity in one capsule that is class & hence Encapsulation…
While abstraction denotes the essential characteristics of an object which
differentiates from other kinds of object...

Abstraction is achieved by making class abstract having one or more methods


abstract. Which is nothing but essential characteristic which should be implemented
by the class extending it.
e.g. when you inventing/designing a car you define a characteristics like car should
have 4 doors, break, steering wheel etc… so anyone uses this design should include
this characteristics. Implementation is not the head each of abstraction. It will just
define characteristics which should be included.

Encapsulation is achieved keeping data and the behaviour in one capsule that is
class & by making use of access modifiers like public, private, protected along with
inheritance, aggregation or composition. So you only show only required things,
that too, only to the extent you want to show. i.e. public, protected, friendly &
private ka funda……
e.g. GM decides to use the abstracted design of car above. But they have various
products having the same characteristics & doing almost same functionality. So
they write a class which extends the above abstract class. It says how gear box
should work, how break should work, how steering wheel should work. Then all the
products just use this common functionality. They need not know how the gear box
works or break works or steering wheal works. Indivisual product can surely have
more features like a/c or auto lock etc…..

Both are powerful; but using abstraction require more skills than encapsulation and
bigger applications/products can not survive with out abstraction.

Encapsulation:
----------------------
Encapsualation is basically, wrapping up of data memebers and methods.
As you said, You 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; }
}
}

Abstraction:-
------------------
Abstraction have the methods which will be common for all the derived class would
need. It contains the skeletion which needs to be implemented by the derived class
also, which will be declared as abstract method.

Example:

abstract class CurryBase


{
public abstract void doCurryMasala();
}

public class FishCurry : CurryBase


{
public void doCurryMasala()
{
// Add Fish Curry specific contents also.
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy