OOP2
OOP2
Senior-lecturer
[email protected]
Astana IT University
▪ Object-Oriented Programming
▪ Advantages of OOP
▪ Data abstraction
▪ Encapsulation principles
▪ Data hiding
▪ The Public and Private Modifiers
▪ OOP or Object Oriented Programming is a good programming practice to
create manageable projects more easily.
▪ Procedural programming means writing code without objects.
▪ OOP enlightens any language for better coding, for best performance and
for writing very big projects without worrying a lot about managing them.
▪ OOP gives you facilities to create reusable objects that you or other
developers can use in their projects without reinventing them again and
again.
▪ OOP removes the hassles and difficulties of writing and managing big
applications.
▪ Reusability
▪ Refactoring
▪ Extensibility
▪ Efficiency
▪ An object is an entity which has bundles of properties and methods
and can interact with other objects.
▪ An object can be sufficient or it may have dependencies over other
objects.
▪ But an object is usually developed to solve a specific set of problems.
▪ So when other developers suffer from the same set of problems, they
can just incorporate your class to their project and use it without
affecting their existing workflow.
▪ When you need to refactor your projects, OOP gives you the
maximum benefit because all objects are small entities and contain
its properties and methods as a part of itself.
▪ So refactoring is comparatively easier.
▪ If you need to add features to your project, you can achieve best
results from OOP.
▪ You can extend the class and create a totally new class that retains all
the necessary properties and methods of the parent class from which
it has been derived, and then expose new features.
▪ The concept of object oriented programming is actually developed
for better efficiency and ease of development process.
▪ Because you first split your problem into a small set of problems and
then find solutions for each of them, the big problem is solved
automatically.
▪ DRY – Don`t repeat yourself
▪ KISS – Keep it short simple
▪ And so on..
▪ Think how to name a class
▪ Determine what member variables and member functions the class
should have
▪ Focus on the meaning of the operations (behavior), to avoid over-
specification
▪ Data abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or
implementation
▪ The representation details are confined to only a small set of
procedures that create and manipulate data, and all other access is
indirectly via only these procedures
▪ Example: in sort() function we do not need any information of how this
function is implemented, which algorithm it uses to sort.
▪ Controlling visibility of names
▪ Encapsulation is a mechanism of wrapping the data (variables) and
code acting on that data (methods) together as a single unit.
▪ In encapsulation the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their
current class, therefore it is also known as data hiding.
▪ Data hiding is one of the important features of Object Oriented
Programming which allows preventing the functions of a program to
access directly the internal representation (or state of an object) of a
class type.
▪ The access restriction to the class members is specified by the
access specifiers within the class body.
▪ A programmer using a method that you have defined does not need
to know the details of the code in the body of the method.
PUBLIC PRIVATE
▪ It is considered as a good programming practice to make all instance
variables private, so that it is not accessible outside of the class
definition.
▪ Therefore, if there is a need to have an indirect access to change or
get a private instance variable, you may provide getter and setter
methods
▪ This methods are also called Accessors and Mutators
▪ It describes the idea of bundling data and methods that work on that
data within one unit
▪ Encapsulation is an OOP concept where object state (class fields)
and it's behavior (methods) is wrapped together
▪ Encapsulation means drawing a boundary around something.
▪ Completely unencapsulated example
▪ Encapsulated example
▪ Further Encapsulation
Java: How to Program (Early Objects), 11th Edition, by Paul Deitel and Harvey Deitel,
Pearson
▪ Chapter 1.5