Tuesday, March 25, 2008

4 major principles of Object-Oriented Programming

Encapsulation, Data Abstraction, Polymorphism and Inheritence.

Encapsulation
What is encapsulation? Well, in a nutshell, encapsulation is the hiding of data implementation by restricting access to accessors and mutators. First, lets define accessors and mutators:
Accessor An accessor is a method that is used to ask an object about itself. In OOP, these are usually in the form of properties, which have, under normal conditions, a get method, which is an accessor method. However, accessor methods are not restricted to properties and can be any public method that gives information about the state of the object.
Mutator Mutators are public methods that are used to modify the state of an object, while hiding the implementation of exactly how the data gets modified. Mutators are commonly another portion of the property discussed above, except this time its the set method that lets the caller modify the member data behind the scenes.
By hiding the implementation of our Person class, we can make changes to the Person class without the worry that we are going to break other code that is using and calling the Person class for information. If we wanted, we could change the fullName from a String to an array of single characters (FYI, this is what a string object actually is behind the scenes) but they callers would never have to know because we would still return them a single FullName string, but behind the scenes we are dealing with a character array instead of a string object. Its transparent to the rest of the program. This type of data protection and implementation protection is called Encapsulation.

Abstraction
An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.
Software developers use abstraction to decompose complex systems into smaller components. In short, data abstraction is nothing more than the implementation of an object that contains the same essential properties and actions.

Inheritance
Rather than duplicate functionality, inheritance allows you to inherit functionality from another class, called a superclass or base class.

Polymorphism
Polymorphism means one name, many forms. Any object that can pass more than one IS-A test can be considered polymorphic.

No comments:

Post a Comment