PROGRAMMING LANGAUGES
STUDY GUIDE
MODULE 7
DATA ABSTRACTION AND OBJECT ORIENTATION
SUBTOPIC 1: ABSTRACTION
Object Oriented Programming
Why abstractions?
• easier to think about - hide what doesn't matter
• protection - prevent access to things you shouldn't see
OOP is currently ruling. It cross-cuts paradigms:
• Imperative OO (C++, Java, C#, Python, etc.)
• Functional OO
• Logical OO
• OO adds:
- Convenient syntax,
- Inheritance,
- Dynamic method binding,
- Encapsulation.
The language needs a way of defining a class: —
- Name,
- Superclasses (incl. Interfaces),
- Fields,
- Methods.
Fields + Methods = Members of the class
• OO supports data hiding / protection:
The 4 kinds of Visibility protection in Java:
- Public
- Protected
- Default (visible to classes in same module)
- Private
Encapsulation and Inheritance
Example:
class circle : public shape { ...
anybody can convert (assign) a circle* into a shape*
class circle : protected shape { ...
only members and friends of circle or its derived classes can convert (assign) a circle* into a
shape*
class circle : private shape { ... only members and friends of circle can convert (assign) a
circle* into a shape*
Encapsulation and Inheritance
Access Specifiers/Modifiers
a) private
b) public
c) default (no keyword)
d) protected
PRIVATE
Variable: a variable that is only
accessible within a class where
it is declared.
Method: a method that is only
accessible within the class
PUBLIC
where it is declared.
DEFAULT (NO KEYWORD)
PROTECTED
Encapsulation and Inheritance
Visibility rules
Public and Private parts of an object declaration/definition
2 reasons to put things in the declaration
▪ so programmers can get at them
▪ so the compiler can understand them
▪ At the very least the compiler needs
▪ to know the size of an object, even though
▪ the programmer isn't allowed to get
▪ at many or most of the fields (members)
▪ that contribute to that size
C++ distinguishes among
public class members accessible to anybody
protected class members accessible to members of this
or derived classes
Private accessible just to members of this class
A C++ structure (struct) is simply a class whose members are public by default
C++ base classes can also be public, private, or protected
EXAMPLE:
class circle : public shape { ...
anybody can convert (assign) a circle* into a shape*
class circle : protected shape { ...
only members and friends of circle or its derived classes can convert (assign) a circle* into a
shape*
class circle : private shape { ...
only members and friends of circle can convert (assign) a circle* into a shape*
Multiple Inheritance
In C++, you can say
class professor : public teacher, public researcher {
...
}
It get all the members of teacher and all the members of researcher
create your own member in the merged class
professor::print () {
teacher::print ();
researcher::print (); ...
}
Or you could get both:
professor::tprint () {
teacher::print ();
}
professor::rprint () {
researcher::print ();
}
Object-Oriented Programming
SMALLTALK is the canonical object-oriented language
• It has all three of the characteristics listed above
• It's based on the thesis work of Alan Kay at Utah in the late 1960‘s
• It went through 5 generations at Xerox PARC, where Kay worked after graduating
• Smalltalk-80 is the current standard
Other languages are described in what follows:
Modula-3
• single inheritance
• all methods virtual
• no constructors or destructors
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