OOPs ! I defined it again ....
All the interviews that I have given and conducted till date always had (and will continue to have) the usual suspect : 'Define concepts/principles of Object Oriented Programming". Most of the interviewers arrive at a fair enough conclusion about the interviewee depending upon his explanation on the aforementioned question. Before I dive into crisp definitions of each of the concept, I want to point out three important entities often misunderstood or misquoted or misconceived while reading books.
C&C (common and crap) answers:
"An Object is an instance of a class ..... Class is from which objects are created/derived". Practically i.e. looking from coding point of view it is a correct definition, but unfortunately doesn't explain much about the exact use of object and class. To put it in layman terms "X equals Y and Y equals X". When one gets to designing an application, this definition will not help.
"An interface is Java's solution to support multiple inheritance". Agreed, but that is use of interface and not the definition.
C&C answers again (this time crisp and correct) :
Object: An object is an entity that has the following four characteristics, viz: Identity, State, Behaviour and Properties
Class: A class provides an Identity to the object and models the state, behaviour and properties of the object. A class defines a skeleton for objects and acts as a blueprint from which object can be created.
Interface: Interface is a contract between a class and the outside world. The behaviour of an object i.e. the methods form the object's interface to outside world. By implementing an interface, a class promises to provide the behaviour published by the Interface. Interfaces help in extensibility.
The four Object Oriented Principles:
Inheritance: Mechanism to structure and organize our software. Provides an ability to a sub-calss to inherit the properties, state and behaviour of the base class. Inheritance enables us to define a more general class and derive specialized classes from it. This helps in better data analysis.
Encapsulation: Separating object's interface from its implementation. Hiding object data and allowing modification of object data by publishing method. Controlling the access to data and methods that manipulate the data using access modifiers.
Polymorphism: The ability of a reference of a base class to denote an object of its own class or any of its subclasses at runtime. This is a definition that many of you might be reading for the first time. The common definitions being "one name many forms" or "method overloading and overriding". But on deeper introspection, the above is possible when a reference of a base class can denote different objects at runtime. The only exeception being method overloading as this is compile time polymorphism i.e. which method will be executed can be determined at compile time itself.
Abstraction: Simplifying complexities by seggregating/breaking them into smaller units and modelling a structure/heirarchy chain by abstracting common behaviour into classes.
All the OOP principals work hand-in-hand. It is very difficult to write a code that just implements the Abstraction principal and so forth.
I hope you have a better perspective of the principles now. I will write more in the blogs-to-come. Till then ... happy digestion.
No comments:
Post a Comment