Object Oriented Programming
What Is an Object
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.
Lets’s consider an example of real world view of objects :
In the above diagram the bulb is an general entity, the attributes and functions of each bulb are represented in terms of class, but if we consider live example of a bulb, say philips 15watts bulb, then it turns out to be an object. So, general entities which contains attributes are classes whereas the real entity that is used to carry out operations are object.
same thing happens with object oriented programming as :
class Bulb { int watts; void on() { // code for bulb on } void off() { // code for bulb off } void Brighten() { // code for bulb brightness increase } void Dim() { // code for bulb brightness decrease } }
and after creating a structure for Bulb, if we talk about real world object that represents this structure we have objects defined as :
Bulb Philips = new Bulb(); //(Creating the real Objects) Philips.watts = 15; //(bulb can be of 15 watts) Philips.on(); //(bulb may be turned on) Philips.off(); //(bulb may be turned off) Philips.Brighten(); //(bulb's brightness may be increased) Philips.Dim(); //(bulb's brightness may be Decreased)
What Is a Class?
A class is a blueprint or prototype from which objects are created. as in above example the Bulb is a class which contain some attributes like :-
1. how much of watts the bulb is
2. On, Off
3. how much light it emits
What Is Inheritance?
Inheritance provides a powerful and natural mechanism for organizing and structuring your software. For example if we talk about bulb then the above bulb has 4 functions, On,Off,brighten,Dim.
We want to derive a low cost bulb that contains only watts information and on/off functions only. We don’t want any brighten and dim functionality. We do it like this in general terms :-
What Is an Interface?
An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface.In interface no application work is defined at interface level and which ever class extends and implements interface, immediately override those methods defined in interface.
What Is a Package?
A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. In other terms we say, the package is a collection of java class files and directories which contain other java files, like we have files and folders in our operating system.
What Is Encapsulation?
Encapsulation is a programming mechanism that binds together code and the data it manipulates,and that keeps both safe from outside interference and misuse. In an object-oriented language, code and data can be bound together in such a way that a self-contained black box is created. Within the box are all necessary data and code. When code and data are linked together in this fashion, an object is created. In other words, an object is the device that supports encapsulation.
Java’s basic unit of encapsulation is the class. A class defines the form of an object. It specifies both the data and the code that will operate on that data. Java uses a class specification to construct objects. Objects are instances of a class. Thus, a class is essentially a set of plans that specify how to build an object.
What Is Polymorphism?
Polymorphism is the quality that allows one interface to access a general class of actions. The specific action is determined by the exact nature of the
situation. A simple example of polymorphism is found in the steering wheel of an automobile. The steering wheel (i.e., the interface) is the same no matter what type of actual steering mechanism is used. That is, the steering wheel works the same whether your car has manual steering, power steering, or rack-and-pinion steering. Therefore, once you know how to operate the steering wheel, you can drive any type of car.
More generally, the concept of polymorphism is often expressed by the phrase “one interface, multiple methods.” This means that it is possible to design a generic interface to a group of related activities. Polymorphism helps reduce complexity by allowing the same interface to be used to specify a general class of action.