Draft: Object Oriented Programming (CS F213) Object Oriented Principles, Elements and Object Model - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Object Oriented Programming

aft
(CS F213)
Object Oriented Principles,
Elements and Object Model - 1
Aritra Mukherjee
Dept of CSIS, BITS Pilani, Hyderabad Campus

Dr
Object Oriented Approach
Encapsulation
Inheritance
Polymorphism

Other Important Elements


Abstraction
Modularity, Typing, Binding
Concurency, Persistence

CS F213 a.mukherjee 2
The Object Oriented Approach

Object-oriented programming is a method of implementation in which


programs are organized as cooperative collections of objects , each of which
represents an instance of some class
Characteristics of Object-oriented Programming:
▶ Has Objects, not algorithms, as its basic building blocks
▶ Each object is an instance of some class
▶ Classes are related to each other via inheritance/dependency relationship

CS F213 a.mukherjee 3
The Object Model
The Three pillars or principles of Object Oriented Programming are:
Encapsulation
In object-oriented programming, encapsulation refers to the bundling of data with the
methods that operate on that data, or the restricting of direct access to some of an
object’s components

Inheritance
In object-oriented programming, inheritance is the mechanism of basing an object or
class upon another object or class, retaining similar implementation

Polymorphism
Polymorphism is one of the core concepts of object-oriented programming (OOP) and
describes situations in which something occurs in several different forms
CS F213 a.mukherjee 4
Encapsulation

▶ Encapsulation is the mechanism that binds together code and the data it
manipulates.
▶ Keeps both safe from outside interference and misuse.
▶ It can be thought of as a protective wrapper that prevents code and data from
being arbitrarily accessed by other code defined outside the wrapper.
▶ Access to code and data is tightly controlled through awell defined interface.
▶ You can use it without knowing the details of implementation.
▶ Basis for encapsulation is “class”

CS F213 a.mukherjee 5
Encapsulation

CS F213 a.mukherjee 6
Inheritance

▶ Inheritance is the process by which one object acquires the properties of another
objects. (super/sub)
▶ This is important because support the concept of hierarchical classification.
▶ The knowledge is made manageable by hierarchical classification.
▶ This mechanism makes it possible for one object to be a specific instance of a
more general case.
▶ This also helps in managing complexity. Because we use the same code already
defined and proved in “Superclass”.

CS F213 a.mukherjee 7
Inheritance

CS F213 a.mukherjee 8
Inheritance

CS F213 a.mukherjee 9
Inheritance

CS F213 a.mukherjee 10
Polymorphism

▶ Polymorphism is a feature that allows one interface to be used for general class of
action.
▶ The specific action is determined by the exact nature of the situation.
▶ Expressed by the phrase- “one interface multiple methods”.
▶ This helps in reducing the complexity.

CS F213 a.mukherjee 11
Polymorphism

The Socket Metaphor!


Without Polymorphism

With Polymorphism

CS F213 a.mukherjee 12
Thus...

Encapsulation, Inheritance and


polymorphism work together to produce
a programming environment that
supports development of more scalable
and robust programs than does the
Process-oriented model
CS F213 a.mukherjee 13
Other Important Elements of OOP

▶ Abstraction
▶ Modularity
▶ Typing
▶ Binding
▶ Concurancy
▶ Persistence

CS F213 a.mukherjee 14
Keywords!!! I
1. abstract Specifies that a class or method will be implemented later, in a subclass
2. assert Assert describes a predicate placed in a java program to indicate that the
developer thinks that the predicate is always true at that place
3. boolean A data type that can hold True and False values only
4. break A control statement for breaking out of loops
5. byte A data type that can hold 8-bit data values
6. case Used in switch statements to mark blocks of text
7. catch Catches exceptions generated by try statements
8. char A data type that can hold unsigned 16-bit Unicode characters
9. class Declares a new class
10. continue Sends control back outside a loop
11. default Specifies the default block of code in a switch statement
12. do Starts a do-while loop
CS F213 a.mukherjee 15
Keywords!!! II
13. double A data type that can hold 64-bit floating-point numbers
14. else Indicates alternative branches in an if statement
15. enum A Java keyword is used to declare an enumerated type. Enumerations
extend the base class
16. extends Indicates that a class is derived from another class or interface
17. final Indicates that a variable holds a constant value or that a method will not be
overridden
18. finally Indicates a block of code in a try-catch structure that will always be
executed
19. float A data type that holds a 32-bit floating-point number
20. for Used to start a for loop
21. if Tests a true/false expression and branches accordingly
22. implements Specifies that a class implements an interface
23. import References other classes
CS F213 a.mukherjee 16
Keywords!!! III

24. instanceof Indicates whether an object is an instance of a specific class or


implements an interface
25. int A data type that can hold a 32-bit signed integer
26. interface Declares an interface
27. long A data type that holds a 64-bit integer
28. native Specifies that a method is implemented with native (platform-specific)
code
29. new Creates new objects
30. null This indicates that a reference does not refer to anything
31. package Declares a Java package
32. private An access specifier indicating that a method or variable may be accessed
only in the class it’s declared in

CS F213 a.mukherjee 17
Keywords!!! IV
33. protected An access specifier indicating that a method or variable may only be
accessed in the class it’s declared in (or a subclass of the class it’s declared in or
other classes in the same package)
34. public An access specifier used for classes, interfaces, methods, and variables
indicating that an item is accessible throughout the application (or where the
class that defines it is accessible)
35. return Sends control and possibly a return value back from a called method
36. short A data type that can hold a 16-bit integer
37. static Indicates that a variable or method is a class method (rather than being
limited to one particular object)
38. strictfp A Java keyword is used to restrict the precision and rounding of
floating-point calculations to ensure portability
39. super Refers to a class’s base class (used in a method or class constructor)
40. switch A statement that executes code based on a test value
CS F213 a.mukherjee 18
Keywords!!! V

41. synchronized Specifies critical sections or methods in multithreaded code


42. this Refers to the current object in a method or constructor
43. throw Creates an exception
44. throws Indicates what exceptions may be thrown by a method
45. transient Specifies that a variable is not part of an object’s persistent state
46. try Starts a block of code that will be tested for exceptions
47. void Specifies that a method does not have a return value
48. volatile This indicates that a variable may change asynchronously
49. while Starts a while loop

CS F213 a.mukherjee 19
Abstraction - needs special mention

Basically it deals with hiding the internal details and showing the essential
things to the end-user. It can be done either by abstract class and interfaces
Using Abstract Class
An abstract class contains one or more abstract methods along with concrete methods.
To use an abstract class, we need to extend it to another class and provide the
definition to all the abstract methods declared to that class else the new class also
needs to be declared as abstract. Through abstract classes we can achieve 0–100%
abstraction as the abstract classes may contain concrete methods

CS F213 a.mukherjee 20
Abstraction - needs special mention

Basically it deals with hiding the internal details and showing the essential
things to the end-user. It can be done either by abstract class and interfaces
Using Interfaces
An interface is a set of abstract methods, static methods, and static constants.
▶ Each method is by default public and abstract.
▶ It does not contain any constructor.
▶ Each variable declared in an interface is by default public, static, and final
▶ Similar to abstract classes we cannot create instances of interfaces
Along with abstraction, the interface helps to achieve multiple inheritances in Java.
We can achieve 100% abstraction using interfaces

CS F213 a.mukherjee 21
Modularity and Typing
Modularity
It is the act of partitioning a program into individual components. This is to reduce
the complexity. Modules serve as physical containers in which we declare classes and
objects of our logical view.

Typing
Typing is the enforcement of the class of an object, such that objects of different types
may not be interchanged, or at the most they may be interchanged only in very
restricted ways.
Java , C++ , Python are strongly typed. Python is also dynamically typed. Smalltalk
is untyped.
Violation of type conformance can be detected at compile time. Any object can invoke
some method on any object, even that method is not defined for that object. Violation
is not known until execution.
CS F213 a.mukherjee 22
Typing

Advantages of strongly typed languages


▶ Without this type checking programs can crash in mysterious ways.
▶ Early detection of bugs makes edit-compile-debug cycle effective
▶ Type declaration helps to document programs
▶ Most compilers generate efficient code if types are declared

CS F213 a.mukherjee 23
Binding

Binding refers to the time when names are bound to object types. This concept is
different from strong/untyped.
Examples
Static and Dynamic Binding:
1. Static/early binding : Means that the type of object pointed by reference is
fixed at the time of compilation.
2. Dynamic binding: Object types are not known until runtime. This concept is
different from strong/untyped.

CS F213 a.mukherjee 24
Concurency and Persistence

Concurency
For certain kinds of problems, an automated system may have to handle many different
events simultaneously. Threads.
Single CPU, multiple CPUs.

Persistence
An object in software takes up some amount of space and exists for a particular
amount of time.
Persistence is the property of an object through which its existence transcends time (
i.e. the object continues to exist after its creator ceases to exist), and/or space ( i.e.
the object moves form one address space to another),

CS F213 a.mukherjee 25
Discussion Time!

CS F213 a.mukherjee 26

You might also like