Java is an object oriented programming
language. Object-oriented programming is at the
core of Java. Object
Oriented Programming or OOP is the
technique to create programs based on the real world. All java programs are
object oriented.
To understand the functionality of OOP
in Java, we first need to understand several fundamentals related to objects.
These include class, method, inheritance, encapsulation, abstraction and polymorphism.
- Classes and Objects:
A class is a blueprint for an
object, and that nearly everything in java is an object. All java code is
defined in a class. A class describes how to make an object of that class type. Class is the central point of
OOP and that contains data and codes with behavior. The class definition
describes all the properties, behavior, and identity of objects present within
that class. In Java everything happens within class and it describes a set of
objects with common behavior.
Objects are the basic unit with
identity and behavior, and object can take care of itself; you don’t have to
know or care how the object does it. An object knows things and does
things. Things an object knows about itself are called instance
variables. They represent the state of an object Things an object
does are called methods. They represent the behavior of an
object.
- Abstraction:
An abstraction is an essential element of
object-oriented programming. An abstraction is the way of converting real
world objects in terms of class. The process of abstraction in Java is used to
hide certain details and only show the essential features of the object. In
other words, it deals with the outside view of an object (interface). For example
creating a class Vehicle and injecting properties into it.
Example:
public
class Vehicle {
public
String colour;
public String model;
}
public String model;
}
- Encapsulation:
Encapsulation is the mechanism that binds together code and
the data it manipulates, and keeps both safe from outside interference and
misuse. This
is an important programming concept that assists in separating an object's
state from its behavior. This helps in hiding an object's data describing its
state from any further modification by external component. In Java there are
four different terms used for hiding data constructs and these are public,
private, protected and package. One way to think about encapsulation is as a protective wrapper
that prevents the code and data from being arbitrarily accessed by other code
defined outside the wrapper.
- Inheritance:
The process
by which one object acquires the properties of another object is known as inheritance. Inheritance
is the property which allows a Child class to inherit some properties from its
parent class. In Java this is achieved by using extends keyword. Only
properties with access modifier public and protected can be accessed in child
class. Though objects are distinguished from each other by some additional
features but there are objects that share certain things common. In object
oriented programming classes can inherit some common behavior and state from
others. Inheritance in OOP allows to define a general class and later to
organize some other classes simply adding some details with the old class
definition. This saves work as the special class inherits all the properties of
the old general class and as a programmer you only require the new features.
This helps in a better data analysis, accurate coding and reduces development
time.
- Polymorphism:
Polymorphism (many forms) is a feature that allows one interface
to be used for a general class of actions. It
describes the ability of the object in belonging to different types with specific
behavior of each type. So by using this, one object can be treated like another
and in this way it can create and define multiple level of interface. Here the
programmers need not have to know the exact type of object in advance and this
is being implemented at runtime. Polymorphism gives us the ultimate
flexibility in extensibility. The ability to define more than one function with
the same name is called Polymorphism. In java, there are two type of
polymorphism: compile time polymorphism (overloading) and runtime polymorphism
(overriding).
When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class.
When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time. Overriding occurs when a class method has the same name and signature as a method in parent class.
Overloading
occurs when several methods have same names with
- Overloading is determined at the compile time.
- Different method signature and different number or type of parameters.
- Same method signature but different number of parameters.
Same method signature and
same number of parameters but of different type.
No comments:
Post a Comment