Any interview I have been in, either as a interviewer or interviewee, a question about Object Oriented Programming has always come up. Almost always the question is stated as follows...
What are the basic principles of Object Oriented Programming or what are the tenets of Object Oriented Programming.
What are the basic principles of Object Oriented Programming or what are the tenets of Object Oriented Programming.
The answers vary from person to person. Most of the developers with some experience in OOP know what they were talking about, but struggle in putting their answers in words. So, I decided to go ahead and describe what I understand them to be. Keep in mind, these are not the only definitions.
Basically in a nutshell, there are four basic tenets or core principles of Object Oriented Programming.
- Abstraction
- Data Encapsulation
- Inheritance
- Polymorphism
Abstraction
Abstraction allows for simplicity. As a consumer you do not need to worry about all the implementation details that go in whatever it is you are consuming. For example, when you turn the radio in your car, you mainly care about volume, channel, and a few other items. You don't need to worry about how the radio works and receives the programming over the air. In the world of programming, abstraction is a similar concept. A class could be implemented in various ways, but the consuming application doesn't have to worry about all the intricate details. All the consumer has to know is the public face of the class to use that class and get the expected results back. For example, if you want to generate an invoice for a customer at the end of the month, you will simply pass on customer information, invoice period and expect the class to return you the results back. How the class gathers the data, communicates with other classes or database etc. is of no concern to you.
Abstraction allows for simplicity. As a consumer you do not need to worry about all the implementation details that go in whatever it is you are consuming. For example, when you turn the radio in your car, you mainly care about volume, channel, and a few other items. You don't need to worry about how the radio works and receives the programming over the air. In the world of programming, abstraction is a similar concept. A class could be implemented in various ways, but the consuming application doesn't have to worry about all the intricate details. All the consumer has to know is the public face of the class to use that class and get the expected results back. For example, if you want to generate an invoice for a customer at the end of the month, you will simply pass on customer information, invoice period and expect the class to return you the results back. How the class gathers the data, communicates with other classes or database etc. is of no concern to you.
Abstraction allows for modular code development and code reuse. Same class can be used by various calling methods without having to worry about the class and its implementation details.
Data Encapsulation
Data Encapsulation is simply what the name suggests. All the relevant data is encapsulated within the class. The calling object never has to worry about how the data is manipulated within the callee, only that the data is returned back in the format it was expected. Encapsulation is implemented by classifying certain properties and methods as public and the outside world can only communicate via only those public methods/properties. For example, in an Employee class, you may declare a public property called "Employee Name" as of type String. The caller will expect and will get a string back. How the Employee class assembles the Employee Name to return back to the caller is of no concern to the caller.
Data Encapsulation is simply what the name suggests. All the relevant data is encapsulated within the class. The calling object never has to worry about how the data is manipulated within the callee, only that the data is returned back in the format it was expected. Encapsulation is implemented by classifying certain properties and methods as public and the outside world can only communicate via only those public methods/properties. For example, in an Employee class, you may declare a public property called "Employee Name" as of type String. The caller will expect and will get a string back. How the Employee class assembles the Employee Name to return back to the caller is of no concern to the caller.
Inheritance
Inheritance allows for abstracting certain common characteristics of an entity in a way that multiple entities that exhibit those same characteristics can "inherit" from the common entity without having to declare them. Inheritance is a "Type Of" or "Kind Of" relationship. For example, An Audi is a type of Car. A BMW is a type of Car. A male and a female are a "Type Of" person. Basically what that means is that both Audi and BMW will exhibit certain common characteristics that all cars have. Similarly both male and feamle will exhibit certain characteristics that are common to all persons.
With this understanding, we can define a class called "Car" and a class called "Person" and have "Audi" and "BMW" classes inherit from "Car" and "Male" and "Female" classes inherit from "Person" class. This will allow us to promote some properties to higher level classes that would otherwise have to be defined in each class.
Inheritance allows for abstracting certain common characteristics of an entity in a way that multiple entities that exhibit those same characteristics can "inherit" from the common entity without having to declare them. Inheritance is a "Type Of" or "Kind Of" relationship. For example, An Audi is a type of Car. A BMW is a type of Car. A male and a female are a "Type Of" person. Basically what that means is that both Audi and BMW will exhibit certain common characteristics that all cars have. Similarly both male and feamle will exhibit certain characteristics that are common to all persons.
With this understanding, we can define a class called "Car" and a class called "Person" and have "Audi" and "BMW" classes inherit from "Car" and "Male" and "Female" classes inherit from "Person" class. This will allow us to promote some properties to higher level classes that would otherwise have to be defined in each class.
Polymorphism
Polymorphism, when used correctly enables tremendous code reuse. Polymorphism can be used at the class/object level or at the method level. The most common use I have seen is at the method level. At the method level, it manifests itself either in method overriding or method overloading. Method overloading means you can create two or more methods with the same name but with different signature. Method Overriding means you can declare a method in a base class, make it overridable and then override it in individual classes that inherit from the base class. Method Overloading is also called compile time polymorphism because compiler determines which overloaded method to use during compile time. Method Overriding is also called run time polymorphism because which method will be used is determined at run time.
Polymorphism, when used correctly enables tremendous code reuse. Polymorphism can be used at the class/object level or at the method level. The most common use I have seen is at the method level. At the method level, it manifests itself either in method overriding or method overloading. Method overloading means you can create two or more methods with the same name but with different signature. Method Overriding means you can declare a method in a base class, make it overridable and then override it in individual classes that inherit from the base class. Method Overloading is also called compile time polymorphism because compiler determines which overloaded method to use during compile time. Method Overriding is also called run time polymorphism because which method will be used is determined at run time.
Object level or inheritance level polymorphims occurs when a method is implemented in base class and all the derived classes use the same implementation. For example, you are writing a stream to either file or memory or network location. All the various stream options are implemented in their own class, but they all inherit from the base class called "Stream" and "Serialize" method is implemented in this base class. All the derived classes will be able to use the same "Serialize" method without having to create their own.
There is much more to Object Oriented Programming and indeed even to these 4 basic tenets, but hopefully this will help you get started!
We will be discussing more about Object Oriented Fundamental in the future.
We will be discussing more about Object Oriented Fundamental in the future.
Thank you and your comments are welcome!
No comments:
Post a Comment