Object Oriented Programming (OOPS)
Object oriented programming is a
method of implementation in which programs are organized as co-operative
collections of objects, each of which represents an instance of class and whose
classes are members of a hierarchy of classes united via inheritance
relationship.
Thus a program that appears to be
an object oriented program but has any of the above elements missing is not an
object-oriented program. OOP follows the
concepts of combining data and functions operating on that data into a single
unit (object).
An object can be defined as a
user defined data type containing both data and functions that manipulate that
data. The data field or variables in the
objects are referred to as data members or member variables. The functions contained in an object are
referred to as member functions or as methods.
In object oriented methodology an object is an abstract entity that
embodies the characteristics of a real world object.
Objects and Classes
Object oriented programming is
moddled on the observation that in the real world, objects are made up of many
kinds of smaller objects. However, the
capability to combine objects is only a general aspect of object-oriented
programming. It also includes concepts
and features that make the creation and use of objects easier and more
flexible. The most important of these
features is the class.
A class is a template for multiple objects
with similar features. Classes embody
all the features of a particular set of objects. An instance of a class is another word for an
actual object. If classes are an
abstract representation of an object an instance is its concrete
representation.
The difference between an
instance and an object is that object is a general term, but both instances and
objects are the concrete representation of class.
Behavior and Attributes
Every class written in a Java
Program is made of two components: - attributes and behavior.
Attributes
Attributes are the individual
things or qualities that differentiate one object from another and determine
the appearance, state, or other qualities of that class. Attributes are defined by variables. Because each instance of a class can have
different values for its variable, each variable is called as an instance
variable.
Attributes of a class object also
can include information about an object’s state. Thus in a class attributes are defined by
variables.
Instance Variables
These defines the attributes of
an object. The class defines the kind of
attribute, and each instance stores its own value for that attribute.
Each attribute has a single
corresponding instance variable.
Changing the value of a variable changes the attribute of that
object. Instance variables may be sent
when an object is created and stay constant throughout the life of the object
or they may be able to change at will as the program runs.
In addition to instance variables
there are also class variables, which apply to the class itself and all its
instances.
Class variables
A class variable is an item of
information that defines an attribute of an entire class. The variable applies to the class itself and
to all of its instances, so only one value is stored, no matter how many
objects of the class have been created.
Behavior
A class’s behavior determines
what instances of that class do when their internal state changes or when that
instance is asked to do something, by another class or object. Behavior is the way objects can do anything
to themselves or have anything done to them.
Methods
Methods are the functions defined
inside the classes that operate on instances of those classes. Just as there are instances and class
variables, there are also instance and class methods. Instance methods apply and operate on an
instance; class methods apply and operate on a class.
Inheritance
It is the language construct that
allows the classes to be related to one another so that a subclass can inherit
features if it’s super class. Each class
has a super class and each class can have one more sub class. Classes further down in the hierarchy are
said to inherit from classes further up hierarchy.
Sub classes inherit all the
methods and variables from their super classes.
At the top of the Java class hierarchy is the class object; all the
classes inherit from this one super class.
Objects is the most general class hierarchy; it defines behavior
specific to all objects in the Java Class hierarchy.
Sub classing
It involves creating a new class
that inherits from some other class in the hierarchy. Using sub classing you only need to define
the difference between your class and its parent; the additional behavior is
available to your class through inheritance.
Encapsulation
Java provides the ability to
limit access to pats of a programs. A
programmer can provide an abstraction without letting the user of that
abstraction see inside it. It prevents
the user from accidentally breaking a program by changing its internal state in
an incorrect way. It also prevents
them from writing code that
depends on the implementation details of such an abstraction. As a result of
encapsulation the code you write tends to be easier to maintain and modify;
also it tends to be modular.
Overloading
Overloading refers to same name
for multiple methods within a class. The
only stipulation is that the argument list and return type for each method must
be unique. The selection of the correct
method to call is based upon the types of arguments in a call expression.
Polymorphism
It is the ability to deal with
multiple types based on a common feature.
Packages
Packages in java are a way of
grouping together related classes and interfaces. Packages enable modular groups of classes to
be available only if they are needed and eliminate potential conflicts between
class names in different groups of classes.
Interface
An interface is a collection of
methods without actual definition that indicate that a class has a set of
behaviors in addition to the behaviors the class gets from its super
classes. Interfaces like classes are declared
in source files one interface to a file.
Like classes they are also compiled into a .class files. One of few difference between them is that an
interface can not be instantiated; new can create only an instance of class.
Example 1
class Animal{
int
legs;
String
color;
char
gender;
public void putData(int l, String
c, char g){
legs = l;
color = c;
gender= g;
}
public void showData( ){
System.out.println(legs +color
+gender);
}
class Animal
{
public static void main(String
args[])
{
Animal Dog = new Animal( );
Animal Monkey = new Animal( );
Dog.putData(4,"Black",'M');
Dog.showData( );
Monkey.putData(4,"Red",'F');
Monkey.showData( );
}
}
}
No comments:
Post a Comment