In
Object Oriented Programming program or an application is divided and organised
using the objects. Objects are the parts of a program using which we organise
different tasks that are meant to be achieved by the application. In OOP
several modules and task are again organised using the objects.
Objects are integral part of any object oriented
programming languages and as Java is OOP language it fully supports the concept
of objects. But there are some other features of OOP.
Features of Object Oriented
Programming (JAVA)
Any
language which is object oriented has following set of features including Java:
1. Abstraction: Abstraction means releasing only
essential and useful features and hiding other unwanted feature or internal
implementation. Abstraction is not a practical phenomenon but it is a purely
logical concept. In OOP the phenomenon of Abstraction is achieved through the
use of encapsulation, yet another feature of OOP.
Ex: To understand the concept of Abstraction let’s take an example of
calculator. You are not aware of the internal implementation of calculator but
still you can perform all the calculations required by and which are supported
by the calculator. In this all the internal implementation or unnecessary
details which are not required and are not meant to be for user are hidden but
all the essential features only of calculations are exposed to user.
2. Encapsulation: Encapsulation means the wrapping of
internal implementation which includes functionality and data, as single unit.
In Java one of the members of Java programming language ‘class’ is used to
encapsulate the internal implementation (data and functionality).
3. Polymorphism:
Polymorphism means performing multiple tasks of same type with same
name. Polymorphism allows us to use the already written method more than once
and thus decreases maintenance overhead.
Ex: In a program of calculation area of
different shapes like square, rectangle, circle etc we can use method with name
as ‘CalculateArea’.
4. Inheritance: Inheritance is the process by which we
can reuse the features implemented in one class, for another class. By
implementation we can reduce the number of lines in our code so it avoids time
and maintenance problem also.
Ex: In a specific application we can define one class as ‘User’ and we can
extend the features of this class to other class like ‘NormalUser’, ‘Editor’,
‘Administrator’ etc. Here some of the features like username and password can
be extended newly defined classes like ‘NormalUser’, ‘Editor’, ‘Administrator’.
In this case class ‘User’ is called as parent class and other newly defined classes
which are inheriting the features of this class are called as child classes.
5. Class: Class is very important concept in Java.
Class acts as blue print for the object. It provides the structure and other
support for the object. By using the structure defined in class we can define
new members of the class which are called as objects. Class serve as data type
for the object as keyword ‘int’ serve as data type for 10 or any other integer
value. The class is
defined type in Java and it is not available in other OOP languages.
In Java java.lang.Object
will be parent for other classes by default.
How to define a class in java?
Like any other programming language java also has specific syntax to
declare each member of Java programming language. So the syntax for declaring a
class in java is:
[Modifier(s)] class
[extends] implements[interface(s)]
6. Object: An object is instance of class. We can
create as many instances as we want (specifically required by application).
Object is used to instantiate the class; without object class is nothing in a
real application. We create reference in memory to store the entities which are
defined in class. Thus class provides structure to create an entity and an
object represents that particular entity in memory and in real time. Object has
state and behaviour.
How to create an Object in java?
The syntax to create object in Java is:
OR
new
In above declaration syntax the first represents the
class for which we are going to create the object.
The new object will be stored
in memory and it can be used to later to reference the object in different
contexts.
Ex: This example shows how to create and class and object in
Java:
Class Human{ //This statement in Java is responsible for creating a class.
String name;
String nationality;
String gender;
int age;
}
Human h1=new Human(); //This statement in Java is responsible for
creating a object of class Human.
So in above example we have just declares the class and created new
member of or instance of class which will be represented by reference h1 in
later part of the program. The ‘new’ keyword in above code snippet is defined
keyword in Java and is used to create instances of class or a new object. This
is not a complete Java program and this program just aims to provide you
insight to the Object and class and how object and class are created in Java.
There some rules that every programmer should follow while writing the
class and objects in java; but we will see those rules in later parts.
0 comments:
Post a Comment