OOPS CONCEPT
OOPS Concept with Real Life
Example
This is the most asked Question
in a technical interview in any IT domains. OOPs Concept is very important.
OOPs concept with real Life Example that will help you to understand OOPS
concept.
Objects: Object is the basic unit of object-oriented programming.
Objects are identified by its unique name. An object represents a particular
instance of a class. There can be more than one instance of an object. Each
instance of an object can hold its own relevant data.
An Object is a collection of data
members and associated member functions also known as methods.
Classes: Classes are data types based on which objects are
created. Objects with similar properties and methods are grouped together to
form a Class. Thus a Class represent a set of individual objects.
Characteristics of an object are represented in a class as Properties. The
actions that can be performed by objects becomes functions of the class and is
referred to as Methods.
Example #1:
For example consider we have a
Class of Cars under which Santro Xing, Alto and WaganR represents individual
Objects. In this context each Car Object will have its own, Model, Year of
Manufacture, Color, Top Speed, Engine Power etc. which form Properties of the
Car class and the associated actions i.e., object functions like Start, Move, and
Stop form the Methods of Car Class.No memory is allocated when a class is
created. Memory is
allocated only when an object is created, i.e., when an instance of a class is created.
allocated only when an object is created, i.e., when an instance of a class is created.
Example #2:
An architect will have the
blueprints for a house....those blueprints will be plans that explain exactly
what properties the house will have and how they are all layout. However
it is just the blueprint, you can't live in it. Builders will look at the
blueprints and use those blueprints to make a physical house. They can
use the same blueprint to make as many houses as they want....each house will
have the same layout and properties. Each house can accommodate its own
families...so one house might have the Smiths live in it, one house might have
the Jones live in it.
The blueprint is the class...the house is the object. The people living in the house are data stored in the object's properties.
Abstraction: Abstraction means showing essential features and hiding non-essential features to the user.
For Eg. Yahoo Mail...
When you provide the user name and password and click on submit button. It will show Compose,Inbox,Outbox,Sentmails. so and so when you click on compose it will open...but user doesn't
know what are the actions performed internally....It just Opens....that is essential; User doesn't know internal actions ...that is non-essential things...
For Eg. TV Remote.
Remote is an interface between user and TVright. Which has buttons like 0 to 10, on /offetc. but we don’t know circuits inside remote...User does not need to know. Just he is using essential thing that is remote.
The blueprint is the class...the house is the object. The people living in the house are data stored in the object's properties.
Abstraction: Abstraction means showing essential features and hiding non-essential features to the user.
For Eg. Yahoo Mail...
When you provide the user name and password and click on submit button. It will show Compose,Inbox,Outbox,Sentmails. so and so when you click on compose it will open...but user doesn't
know what are the actions performed internally....It just Opens....that is essential; User doesn't know internal actions ...that is non-essential things...
For Eg. TV Remote.
Remote is an interface between user and TVright. Which has buttons like 0 to 10, on /offetc. but we don’t know circuits inside remote...User does not need to know. Just he is using essential thing that is remote.
Encapsulation: Encapsulation means which binds the data and code (or)
writing operations and methods in single unit (class).
For Example:
A car is having multiple parts. Like steering,wheels, engine. Etc.which binds together to form a single object that is car. So, here multiple parts of cars encapsulates itself together to form a single object that is Car.
A car is having multiple parts. Like steering,wheels, engine. Etc.which binds together to form a single object that is car. So, here multiple parts of cars encapsulates itself together to form a single object that is Car.
In real time we are using Encapsulation for security purpose...
Encapsulation = Abstraction +
Data Hiding.
Inheritance: Deriving a new class from the existing class, is called
Inheritance.
Derived (sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class.
Derived (sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class.
For Eg.,
class Address
{
String name;
String H.no;
String Street name;
}
class Latest Address extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
Latest Address la = new Latest Address();
//Assign variable accordingly...
}
}
{
String name;
String H.no;
String Street name;
}
class Latest Address extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
Latest Address la = new Latest Address();
//Assign variable accordingly...
}
}
In the above Example class Latest
Address getting all features from the Address class.
In the Latest Address class we have total 6 properties..3 are inherited from Address class and 3 properties are
incorporated. So In the class Vishal we are declaring the object of class Latest Address and then assign new variables using the properties of the previous base classes... So this is a nice example of inheritance.
In the Latest Address class we have total 6 properties..3 are inherited from Address class and 3 properties are
incorporated. So In the class Vishal we are declaring the object of class Latest Address and then assign new variables using the properties of the previous base classes... So this is a nice example of inheritance.
Polymorphism:
Polymorphism means ability to
take more than one form that an operation can exhibit different behavior at
different instance depend upon the data passed in the operation.
1>we behave differently in front of elders, and friends. A single person is behaving differently at different time.
2> A software engineer can perform different task at different instance of time depending on the task assigned to him .He can done coding , testing , analysis and designing depending on the task assign and the requirement.
3> Consider the stadium of common wealth games. Single stadium but it perform multiple task like swimming, lawn tennis etc.
4> If a girl is married and mother of 2 children doing teaching job then she is a women first, teacher in a school when she is in school, wife of someone at home, mother of her children,, and obvious daughter of someone & may be girl friend of someone (just kidding) means a woman plays different roles at different times dates the polymorphism (many forms).
Summary:
OOPs have following features:
1. Object - Instance of Class
2. Class - Blue print of Object
3. Encapsulation - Protecting our Data
4. Polymorphism - Different behaviors at different instances
5. Abstraction - Hiding our irrelevant Data
6. Inheritance - One property of object is acquiring to another property of object
1. Object - Instance of Class
2. Class - Blue print of Object
3. Encapsulation - Protecting our Data
4. Polymorphism - Different behaviors at different instances
5. Abstraction - Hiding our irrelevant Data
6. Inheritance - One property of object is acquiring to another property of object
More
Practical Example
Preface
These tutorials will provide you a beginner's perspective of Java programming. The first few tutorials would be irrespective of programming language, but at the latter half, I would be stressing more on Web application development in Java. I am preparing these tutorials on the basis of my own experience of learning at TCS. Your suggestions and queries would help me build on this tutorial
Intro
OOP - A term that should flow in your blood if you have to be a good programmer. It is an essential perspective, visualization and an art to understand the concept as a whole. OOP concepts redefine our thoughts (in programming paradigm) and the way we code. The concepts are very easy in theory, but a bit difficult to grasp in real world scenarios. And once you master this perspective, coding in any language would be a piece of cake. So lets get to the basics first(Mind you, the theory is really simple. Its gets confusing when you try to implement these concepts)
OOP
I guess its high time that I mention what OOP is all about. OOP - Object Oriented Programming, is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. - Wikipedia
Got any idea? Bet you haven't. Let me put it into simple words. Object Oriented Programming is
These tutorials will provide you a beginner's perspective of Java programming. The first few tutorials would be irrespective of programming language, but at the latter half, I would be stressing more on Web application development in Java. I am preparing these tutorials on the basis of my own experience of learning at TCS. Your suggestions and queries would help me build on this tutorial
Intro
OOP - A term that should flow in your blood if you have to be a good programmer. It is an essential perspective, visualization and an art to understand the concept as a whole. OOP concepts redefine our thoughts (in programming paradigm) and the way we code. The concepts are very easy in theory, but a bit difficult to grasp in real world scenarios. And once you master this perspective, coding in any language would be a piece of cake. So lets get to the basics first(Mind you, the theory is really simple. Its gets confusing when you try to implement these concepts)
OOP
I guess its high time that I mention what OOP is all about. OOP - Object Oriented Programming, is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. - Wikipedia
Got any idea? Bet you haven't. Let me put it into simple words. Object Oriented Programming is
·
a way to code,
·
its an approach
towards coding,
·
its an approach to
problem solving,
·
is a real life
simulation of programming methodology
Now that I have
said OOP is an approach to programming, there are a few core
concepts that make up this methodology. I am not listing them down at one go,
instead I'll be explaining it one by one.
Please note that these
concepts are applicable for most of the programming languages. This tutorial
explains these concepts with respect to real life scenario.
Classes and Object
What is the difference between C and
C++? C++ is C with classes! So now what are Classes? Classes
are a combination of related attributes (variables, properties) and functions.
Going to a real life scenario, it would look like the following.
|
Car, Bike
and Auto-rickshaw all have a set of properties and
functions(methods). So these would constitute the concept of classes. It would
have the variables Wheels, Doors, Colors etc. and a few set a
functions like Steer, Accelerate, Brake etc. To go a bit into
the coding part, the class car would look like this
class Car
{
int wheel;
int door;
string color;
steer() { .. Function description..}
accelerate() { .. Function description..}
brake() { .. Function description .. }
}
{
int wheel;
int door;
string color;
steer() { .. Function description..}
accelerate() { .. Function description..}
brake() { .. Function description .. }
}
This
would how a sample class would look. So now on to the next term, Objects.
What is an Object? An Object is simply an instance of this class. For example, BMW X5 would be an object of class Car. I don't think more explanation on this is required, please leave a comment if you feel it is required.
What is an Object? An Object is simply an instance of this class. For example, BMW X5 would be an object of class Car. I don't think more explanation on this is required, please leave a comment if you feel it is required.
Data Abstraction
Tough word? Nah,
not really! Its an easy concept. Data Abstraction means you
hide data(keep data abstract) from the user. Or in other words, only essential
information are exposed to a user. There is a popular example for data
abstraction. When you drive a car, to shift gears, all you have to do is move
the Gear stick. You don't have to know how the bearings are working in the
background. Similarly which building an application, you need not
show unnecessary details to the user.
Keep it simple!
Data Encapsulation
You already know this concept. No? Data
Encapsulation can be defined as Wrapping up of Data and
related functions. That was what we exactly did above in Classes and Objects.
The properties (data) and their related functions are combined into a single
entity called Class.
Inheritance
This concept takes the literal meaning of Inheritance. Certain properties are inherited to us from our parents. This perspective is brought onto the programming methodology. I would explain this concept with the above example of Cars and Bikes. These classes share some common properties and functions, like Wheels, Doors, Color etc. So why not put these into a parent class called Vehicles and inherit these properties with specific values in each class? That's exactly what happens here. You write a class called Vehicle which would look like this
Inheritance
This concept takes the literal meaning of Inheritance. Certain properties are inherited to us from our parents. This perspective is brought onto the programming methodology. I would explain this concept with the above example of Cars and Bikes. These classes share some common properties and functions, like Wheels, Doors, Color etc. So why not put these into a parent class called Vehicles and inherit these properties with specific values in each class? That's exactly what happens here. You write a class called Vehicle which would look like this
class Vehicle
{
int wheels;
int doors;
string colors;
steer();
accelerate();
brake();
}
{
int wheels;
int doors;
string colors;
steer();
accelerate();
brake();
}
Note that you won't
give the description for the functions over here, as each class(Car or Bike)
have their own methods to Steeror Accelerate. Now
if you write the class as
class Car extends
Vehicle (This is the Java syntax, might vary for other languages)
{
...Class description...
}
{
...Class description...
}
This class would have all the above mentioned properties, and
can set their own values here. The hierarchy would look like shown in the image
Polymorphism
Don't get strangled by the name, its a simple concept. It's a concept which we can interpret a function in more than one way. To give an example, suppose a function to calculate the Area of a geometric figure(Circle, Square etc.). Ideally, one would create separate functions with different names for the figures. Using the concept of Polymorphism, you can have the same name for the function, yet do different functions with it. You will get used to this concept at a later point of time, so I'm not going into its depth now.
Messaging
Messaging is the core concept of communication among classes. To give an idea of it, let me put up a scenario. Suppose we have two classes, Driver and Car. The Driver has to communicate with the Car to do functions like accelerate, brake etc. So how is this interaction done? Yes, Car has a function accelerate, but some message should be sent from Driver to Car right? That's exactly what messaging is all about. Once you get into this, you would be doing messaging every now and then.
Conclusion
Finally! Make sure that you are used to all these concepts. In the upcoming tutorials, you would be seeing these concepts implemented in a real life scenario. Before ending up, let me note down the points to remember.
Polymorphism
Don't get strangled by the name, its a simple concept. It's a concept which we can interpret a function in more than one way. To give an example, suppose a function to calculate the Area of a geometric figure(Circle, Square etc.). Ideally, one would create separate functions with different names for the figures. Using the concept of Polymorphism, you can have the same name for the function, yet do different functions with it. You will get used to this concept at a later point of time, so I'm not going into its depth now.
Messaging
Messaging is the core concept of communication among classes. To give an idea of it, let me put up a scenario. Suppose we have two classes, Driver and Car. The Driver has to communicate with the Car to do functions like accelerate, brake etc. So how is this interaction done? Yes, Car has a function accelerate, but some message should be sent from Driver to Car right? That's exactly what messaging is all about. Once you get into this, you would be doing messaging every now and then.
Conclusion
Finally! Make sure that you are used to all these concepts. In the upcoming tutorials, you would be seeing these concepts implemented in a real life scenario. Before ending up, let me note down the points to remember.
·
OOP Concepts
·
Classes and Objects
·
Data abstraction
·
Data encapsulation
·
Inheritance
·
Polymorphism
·
Messaging