What Is Object-Oriented Programming ?
What Is Object-Oriented Programming ?
Data:
Speed
Height of seat
Status of the bell
Operations:
Pedal Faster
Adjust Height of Seat
Ring Bell
Encapsulation
Encapsulation is an important part of OO programming, but it's not
difficult. In Java Encapsulation is implemented by a class, all a
class is the generic form of an object. So in our example the class is
bicycle while the object (an instance of the class) is, perhaps
MyBicycle. Within the object there are so called instance variables,
in our example MyBicycle will have instance variables for speed,
height of seat and status of the bell. So when an operation (in Java a
method) operates on an object it changes the instance variables for
that object. If the Pedal Faster method was called for the object
MyBicycle then the instance variable of speed, within MyBicycle, would
be altered accordingly.
Polymorphism
Polymorphism is the ability of objects to react differently when
presented with different information, known as parameters. In a
functional programming language the only way to complete two different
tasks is to have two functions with different names. Object-oriented
languages, which include Java, allow different methods to be run
depending on what type of parameters are specified. So if our bicycle
were to be involved in a collision with a object the object would be
passed as a parameter to a collision method, obviously collision with
a fly will have very different affects to collision with a lorry. The
fly may cause the bicycle to gain a speck of blood while collision
with a lorry may cause a sudden loss of speed and major damage.