Académique Documents
Professionnel Documents
Culture Documents
com
www.jwjobs.net
www.jntuworldupdates.org
UNIT VII
DATA ABSTRACTION AND OBJECT ORIENTATION
Abstraction Properties
Allow implementers to change an implementation without affecting the users of
the data structure.
Developmental independence (use specs even before an implementation is
written.
Complete separation of specifications from implementation.
Reusability.
Efficiency.
Conceptual clarity (precise client interfaces).
Information hiding (implementation details hidden from client).
User may think in terms of values (no surprise side effects).
Modular reasoning about correctness
1) Object Oriented Programming
Control or PROCESS abstraction is a very old idea (subroutines!), though
few languages provide it in a truly general form (Scheme comes close)
Data abstraction is somewhat newer, though its roots can be found in
Simula67
An Abstract Data Type is one that is defined in terms of the
operations that it supports (i.e., that can be performed upon it) rather
than in terms of its structure or implementation
Objects add inheritance and dynamic method binding
Simula 67 introduced these, but didn't have data hiding
The 3 key factors in OO programming
Encapsulation (data hiding)
Inheritance
Dynamic method binding
www.specworld.in
www.smartzworld.com
Page 1
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
The abstraction provided by modules and module types has at least three important
benefits.
1. It reduces conceptual load by minimizing the amount of detail that the
programmer must think about at one time.
2. It provides fault containment by preventing the programmer from using a
program component in inappropriate ways, and by limiting the portion of a
programs text in which a given component can be used, thereby limiting the
portion that must be considered when searching for the cause of a bug.
3. It provides a significant degree of independence among program components,
making it easier to assign their construction to separate individuals, to modify their
internal implementations without changing external code that uses them, or to
install them in a library where they can be used by other programs.
By deriving new classes from old ones, the programmer can create
arbitrarily deep class hierarchies, with additional functionality at every level
of the tree.
When created with new, an object is allocated in the heap; when created via
elaboration of a declaration it is allocated statically or on the stack, depending on
lifetimecreation causes the invocation of a programmer-specified initialization
routine, known as a constructor. In C++ and its descendants, Java and C#, the
name of the constructor is the same as that of the class itself. C++ also allows the
programmer to specify a destructor method that will be invoked automatically
when an object is destroyed, either by explicit programmer action or by return
from the subroutine in which it was declared. The destructors name is also the
same as that of the class, but with a leading tilde (~).
Public and Private Members
The Access specifier for the OOPs are : Private, Public and Protected.
In Java,
Private:
The most restrictive access level is private.
A private member is accessible only to the class in which it is defined.
Inheritance does not apply on the private members. They are Just like
secrets.
Use private keyword to create private members.
www.specworld.in
2
www.smartzworld.com
Protected:
Page 2
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
Allows the class itself, subclasses, and all classes in the same package
to access the members.
Use the protected access level when it's appropriate for a class's
subclasses to have access to the member, but not unrelated classes.
Protected members are like family secrets.
To declare a protected member, use the keyword protected
Public:
The easiest access specifier is public.
Any class, in any package, has access to a class's public members.
Declare public members only if such access cannot produce
undesirable results if an outsider uses them.
There are no personal or family secrets here; this is for stuff you don't
mind anybody else knowing.
To declare a public member, use the keyword public.
In C++
Declared in the body of the class
May be public or private
Exist throughout the life of the object.
Stored in class object.
Each object has its own copy.
May be objects of any type
Access-specifier private
Makes any member accessible only to member functions of the class.
May be applied to data members and member functions
Default access for class members
Encourages information hiding
As a rule of thumb, data members should be declared private
Member functions should be declared public.
Except member functions that are accessed only by other member
functions of the class.
Often useful to have get and set functions
To access private members in controlled ways
www.specworld.in
www.smartzworld.com
Page 3
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
Tiny Subroutines
Object-oriented programs tend to make many more subroutine calls than do
ordinary imperative programs, and the subroutines tend to be shorter. Many
programmers in fact consider it bad style to declare public fields, because they give
users of an abstraction direct access to the internal representation.
Property and indexer methods in C#
C# provides a property mechanism specifically designed to facilitate the declaration of methods to get and set values. Using this mechanism, our val field
could be written as follows.
class list_node {
...
int val;
public int Val {
get { // presence of get and optional set
return val; // methods means that Val is a property
}
set {
val = value; // value is a keyword: argument to set
}
}
...
}
Users of the list_node class can now access the (private) val field through the
(public) Val property as if it were a field:
list_node n;
...
int a = n.Val; // implicit call to get method
n.Val = 3; // implicit call to set method
A indexer mechanism can make objects of arbitrary classes look like arrays,with
conventional subscript syntax in both l-value and r-value contexts. In C++,
operator overloading and references can be used to provide the equivalent of
indexers, but not of properties
Derived Classes
New classes created from existing ones is called inheritance..
Existing class is called Base class/parent class / Sub Class.
New class is called Derived/Child/Sub Class.
www.specworld.in
www.smartzworld.com
Page 4
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
In C++,
class Shape
{
public:
int GetColor ( ) ;
protected:
// so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes here
};
class Three_D : public Shape
{
// put members specific to 3D shapes here
};
Shape is the base class and two-D & Three-D are derived classes and can
access the method getColor().
In java,
public class derived-class-name extends base-class-name {
// derived class methods extend and possibly override
// those of the base class
}
By deriving new classes from old ones, the programmer can create arbitrarily
deep class hierarchies, with additional functionality at every level of the tree.
www.specworld.in
www.smartzworld.com
Page 5
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
class gp_list_node {
gp_list_node* prev;
gp_list_node* next;
gp_list_node* head_node;
public:
gp_list_node(); // assume method bodies given separately
gp_list_node* predecessor();
gp_list_node* successor();
bool singleton();
void insert_before(gp_list_node* new_node);
void remove();
~gp_list_node();
};
Now we can use this general purpose class to derive lists and queues with
specific
types of fields:
class int_list_node : public gp_list_node {
public:
int val; // the actual data in a node
int_list_node() {
val = 0;
}
int_list_node(int v) {
val = v;
}
};
In Java the general class is represented in the form of Abstract Base Classes
An abstract class is a class that leaves one or more method implementations
unspecified by declaring one or more methods abstract. An abstract method has no
body (i.e., no implementation). A subclass is required to override the abstract
www.specworld.in
www.smartzworld.com
Page 6
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
Overloaded Constructor
A Class can have more than one constructor in a class, as long as each
has a different list of arguments.
C++,
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
rectangle();
// another constructor
void draw();
// draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
rectangle::rectangle()
{
height = 10;
width = 10;
www.specworld.in
7
xpos = 0;
www.smartzworld.com
Page 7
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
ypos = 0;
}
void main()
{
rectangle rc1(3.0, 2.0);
rectangle rc2();
rc1.draw();
rc2.draw();
www.specworld.in
www.smartzworld.com
Page 8
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
end tuple
The this Parameter
This replication would be highly wasteful, however, as the copies would vary only
in the details of address computations. A better technique is to create a single
instance of each module subroutine, and to pass that instance, at run time, the
address of the storage of the appropriate module instance.
www.specworld.in
10
www.smartzworld.com
Page 10
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
y->print_mailing_label();
Virtual and Nonvirtual Methods
public:
virtual void print_mailing_label() = 0;
Abstract Classes
Class that contains one or more abstract methods
Java: called an interface (which has only abstract methods)
Generally not possible to declare object of an abstract class b/c it would be
missing at least one member
But you can do so in C++
Serves as a base for concrete classes.
Concrete class must provide a definition for every abstract method it
inherits
Application to dynamic method binding: allows code that calls methods of
objects of a base class, assuming that the concrete methods will be invoked
at run time
Member Lookup
In dynamic binding each object is represented with a record whose first field
contains the address of a virtual method table (vtable) for that objects class
Our objects are being more complicated for the compiler to manage
Virtual method tables
Reference counts
Etc
www.specworld.in
14
www.smartzworld.com
Page 14
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
Polymorphism
A derived class (D) has all the members of its base class (C)
Class D can be used anytime class C is expected.
If class D does not hide any 15publicly visible members of Cwww.smartzworld.com
then D is a
www.specworld.in
subtype of C.
www.jntuworld.com
Page 15
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
person *x = &s;
person *y = &p;
5) Multiple Inheritance
It means derived class to inherit features from more than one base class. For
Example College and Department are two base classes then the students is a
class which can use features of both the College and Department class.
College
Department
Students
In C++, The multiple inheritance is implemented as
class
Students
public
College,
public
Department
...
}
In Java the multiple Inheritance is not implemented directly but via
interface as,
interface College
{ .}
interface Department
{}
Class students implements College, Department
{
}
www.specworld.in
16
www.smartzworld.com
Here you get all the members of College and all the members of Department
Page 16
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
Ada 95
tagged types
single inheritance
no constructors or destructors
class-wide parameters:
methods static by default
can define a parameter or pointer that grabs the object-specific
version of all methods
base class doesn't have to decide what will be virtual
notion of child packages as an alternative to friends
Java
interfaces, mix-in inheritance
alternative to multiple inheritance
basically you inherit from one real parent and one or more
interfaces, each of which contains only virtual functions and no
data
this avoids the contiguity issues in multiple inheritance above,
allowing a very simple implementation
all methods virtual
Is C++ object-oriented?
Uses all the right buzzwords
Has (multiple) inheritance and generics (templates)
Allows creation of user-defined classes that look just like built-in ones
Has all the low-level C stuff to escape the paradigm
Has friends
Has static type checking
In the same category of questions:
Is Prolog a logic language?
Is Common Lisp functional?
However, to be more precise:
Smalltalk is really pretty purely object-oriented
Prolog is primarily logic-based
Common Lisp is largely functional
C++ can be used in an object-oriented
style
www.specworld.in
18
www.smartzworld.com
Page 18
www.jntuworld.com
www.jntuworld.com
www.jntuworld.com
www.jwjobs.net
www.jntuworldupdates.org
RESOLVE
www.specworld.in
19
www.smartzworld.com
Page 19
www.jntuworld.com
www.jntuworld.com