Encapsulating Java OOP. Why Object.doSomething (); better than doSomething (Object) ;?

I am trying to explain the concepts of oop in java.

The basic principle in oop is that objects have methods; therefore Object.method (); works. I contrast this with process programming, in which the (Object) method needs to be done.

Is this called encapsulation?

What are the benefits of the oop method?

+4
source share
5 answers

In the Object.doSomething () script, the object will have full control over its properties that are used in the method.

But in another call to doSomething (Object), you must make all the properties of the object public so that they are available in the method. This is not a safer operation.

+2
source

This is a big question with an answer that fills several books, but, in short, class members have access modifiers (public, private, protected). Private members may have access to other members of the class, such as a method, but not from external functions.

+4
source

Another 2 benefits of OOP are reuse and polymorphism.

REUSE: If you use doSomething(Object) in one file or in one program, this may work fine for this program. Now imagine that you need to use your Object in another program. You will need to duplicate the doSomething() method in your new program (maybe copy and paste it). This may work, but it is bad practice and makes keeping this logic a nightmare. If the logic doSomething() is a function inside the Object , then this logic "lives" with the object.

polymorphism: Imagine another case where an Object is one of many such types. If you use interfaces, many objects can implement the doSomething() function according to their specific needs.

Example:

 interface ICar { void doSomething(); void getFuel(); } class GasCar : ICar { public void doSomething() { //do something a gas car would do } public void getFuel() { //logic to pull gas out of a tank } } class ElectricCar : ICar { public void doSomething() { //do something an electric car would do } public void getFuel() { //logic to pull fuel out of a battery } } 
+1
source

OOP advocates encapsulation; as a result, state and behavior are encapsulated in a class representing the object. Depending on what level of encapsulation will occur to you, use encapsulation based on a static (class) or instance (at the object level).

0
source

method (Object) is a paradigm that works with data structures. Data structures are a grouping of information fields that are semantically consistent and related (that is, Struct Person {FirstName, LastName, DateOfBirth}).

Object-oriented programming is one step above data structures. In OOP, we not only group related data fields, but also include functions (methods, member functions) that are associated with the data (and that act on the data in the right way).

Encapsulation is to keep some of the elements closed to objects. The goal is to "hide" the internal work from the outside world and protect the state of the object from "corruption" or from assigning incorrect values. OOP languages ​​provide several "access modifiers" that are used to indicate whether a particular category of objects (instances of child classes, classes in the same "package / namespace / library", any other class, etc. can access this member). .d.).

object.method () usually requests an object to do something, which may include access to a field that is not accessible outside the class.

Above was the definition and explanation of how the concept of a member function (method) and the concept of encapsulation go hand in hand.

 Referrences: http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 http://en.wikipedia.org/wiki/Object-oriented_programming 
0
source

Source: https://habr.com/ru/post/1394452/


All Articles