I am struggling to explain oop concepts in java.
A major tenet in oop is that objects have methods; so Object.method(); works.
I am contrasting this with procedural programming in which one must do method(Object).
Is this called encapsulation?
What are the advantages of the oop way?
2 more advantages of OOP are re-use and polymorphism.
ReUse:
If you use
doSomething(Object)in one file or one program, it may work fine for that program. Now, imagine that you need to use yourObjectin another program. You will need to duplicate thedoSomething()method in your new program (probably copy and paste it). This may work, but is bad practice and makes maintaining that logic a nightmare. If thedoSomething()logic is a function insideObjectthen that logic “lives” with the object.Polymorphism:
Imagine another case where Object is just one of many similar types. If you take advantage of Interfaces, many objects can implement the
doSomething()function to suit their specific needs.Example: