OOPs and Its Concepts in Java

glintplainvilleΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 1 μήνα)

75 εμφανίσεις



OOPs and Its Concepts in Java

Posted on: November 21, 2006 at 12:00 AM

Object Oriented Programming or OOP is the technique to create programs based on
the real world.

OOPs and Its Concepts in Java







Brief Introduction to OOP

Object Oriented Programming or OOP is the technique to create programs based on the real
world. Unlike procedural programming, here in the OOP programming model programs are
organized around objects and data rather than actions and logic. Objects represent

some
concepts or things and like any other objects in the real Objects in programming language
have certain behavior, properties, type, and identity. In OOP based language the principal aim
is to find out the objects to manipulate and their relation betwe
en each other. OOP offers
greater flexibility and compatibility and is popular in developing larger application. Another
important work in OOP is to classify objects into different types according to their properties
and behavior. So OOP based software app
lication development includes the analysis of the
problem, preparing a solution, coding and finally its maintenance.

Java is a object oriented programming


and to understand the functionality of OOP in Java,
we first need to understand several fundamental
s related to objects. These include class,
method, inheritance, encapsulation, abstraction, polymorphism etc.

Class

-

It is the central point of OOP and that contains data and codes with behavior. In Java
everything happens within class and it describes a

set of objects with common behavior. The
class definition describes all the properties, behavior, and identity of objects present within
that class. As far as types of classes are concerned, there are predefined classes in languages
like C++ and Pascal. B
ut in Java one can define his/her own types with data and code.



Object

-

Objects are the basic unit of object orientation with behavior, identity. As we
mentioned above, these are part of a class but are not the same. An object is expressed by the
varia
ble and methods within the objects. Again these variables and methods are distinguished
from each other as instant variables, instant methods and class variable and class methods.


Methods

-


We know that a class can define both attributes and behaviors. A
gain attributes
are defined by variables and behaviors are represented by methods. In other words, methods
define the abilities of an object.


Inheritance
-

This is the mechanism of organizing and structuring software program. Though
objects are distinguis
hed 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 l
ater 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 b
etter data
analysis, accurate coding and reduces development time.


Abstraction

-

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 ob
ject
(interface).


Encapsulation

-

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 J
ava there are four different terms used
for hiding data constructs and these are public, private, protected and package. As we know
an object can associated with data with predefined classes and in any application an object
can know about the data it needs

to know about. So any unnecessary data are not required by
an object can be hidden by this process. It can also be termed as information hiding that
prohibits outsiders in seeing the inside of an object in which abstraction is implemented.


Polymorphism
-

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 programmer
s need not
have to know the exact type of object in advance and this is being implemented at runtime.




This tutorial will help you to understand about Java OOPS concepts with examples. Java is a
Object Oriented Programming Language (OOPS). Lets we discu
ss about what are the
features of OOPS. There are four main features of OOPS.

1) Encapsulation

2) Inheritance

3) Polymorphism

4) Abstraction

Lets we discuss about the about features in details.

Encapsulation

Encapsulation means putting together all the var
iables (Objects) and the methods into a single
unit called Class. So define a user defined data type called the keyword Class followed by the
class name and inside the class we need the specify the variables that is also called as
attributes and methods.

E
xample :

Class EncaptulationExample {

int obj;

char cObj;

public void javaMethod(){

}

}

Inheritance

Inheritance is mainly used for code reusability. You already have defined an Object or rather
you have already defined set of attributes and characteristics

which you like to make you it
again and expand up on it. So you are making use of already written class and further
extending on that. That why we discussed about the code reusability the concept. In general
one line definition we can tell that deriving a

new class from existing class, it’s called as
Inheritance. You can look into the following example for inheritance concept.

Example 1:

class StudentInfo {

String name;

int rollno;

int get(String n, int r){

name=n; rollno=r; return(0);

}

void showDetails()
{

System.out.println(“Name : “+name);

}

}

class InheritanceExampleDemo extends StudentInfo{

public static void main(String args[]){

StudentInfo studObj = new StudentInfo();

studObj.get(“Eswar”,92);

studObj.showDetails();

}

void displayDetails(){

System.out
.println(“Sample Info Display”);

}

}

Output:

Name : Eswar



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. This lesson explains how state
and behavior are represented within an object, introduces the concept of data encapsulation,
and explains the benefits of desig
ning your software in this manner.

What Is a Class?

A class is a blueprint or prototype from which objects are created. This section defines a class
that models the s
tate and behavior of a real
-
world object. It intentionally focuses on the
basics, showing how even a simple class can cleanly model state and behavior.

What Is
Inheritance?

Inheritance provides a powerful and natural mechanism for organizing and structuring your
software. This section explains how classes inherit state and behavior from their superclasses,
and explains how to derive one class from another using t
he simple syntax provided by the
Java programming language.

What Is an Interface?

An interface is a contract between a class and the outside world. When a class i
mplements an
interface, it promises to provide the behavior published by that interface. This section defines
a simple interface and explains the necessary changes for any class that implements it.

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. This section
explains why this is usefu
l, and introduces you to the Application Programming Interface
(API) provided by the Java platform.






Re: what are the oops concept in java explain with real time examples
Answer

#
1


Encapsulation


Abstraction


polymorphisms


inheritance/delegation




Is This Answer Correct ?


291 Yes

65 No

3

Raja






Re: what are the oops concept in java explain with real time examples
Answer

#
2


OOPS Concepts are mainly 4


1.Abstraction


2.Encapsulation


3.Inheritance


4.Polymorphisam

Abstraction:
-
Hidding non
-
essential features and showing
the

essential

features



(or)

Hidding unnecessary data from the users details,is
called

abstraction.

Real Time example:TV Remote Button


in that number format and power buttons and other
buttons

there.just we are seeing the butttons,we don't see the

button circuits.i.e buttons circutes and wirings all
are

hidden.so i think its good example.


Encapsulation:



Writing Operations and methods stored in a single

class.This is Called Encapsulation


Real Time Example:Medical Capsuals

i.e one drug is store
d in buttom layer and another drug
is

stored in Upper layer these two layers are combined in

single capsual.


Inheritance:


The New Class is Existing from Old Class,i.e
SubClass is

Existing from Super Class.


Real Time Example:


Father and Son Relat
ionship


Polymorphisam:



Sinle Form behaving diffreantly in diffreant

Situations.


Example:
-


Person

Person in Home act is husband/son,


in Office acts Employer.


in Public Good Cityzen.


I think these all are real time
experianceany.is there
any

more answers send me my mail also.


Thanks all

CM




Is This Answer Correct ?


653 Yes

32 No

4

Chandramouli











Re: what are the oops concept in java explain with real time examples
Answer

#
3


1)Abstraction:


Abstraction is a way to remove the association of the

behavior of an object with the actual details behind
the

scenes which implement that object's
behavior. This

'abstraction' is usually accomplished through the use
of

base classes with virtual functions; each derived
function

provides the details that implement the behavior behind
that

abstraction.


eg/we invented flight based on the mechanism of Bi
rds.
So

flight is derived form the base of birds.


2)Encapsulation:


It is a process of binding or wrapping the data and the

codes that operates on the data into a single entity.
This

keeps the data safe from outside interface and misuse.
One

way to think

about encapsulation is as a protective
wrapper

that prevents code and data from being arbitrarily
accessed

by other code defined outside the wrapper.


eg/ Ink is the important component in pen but it is
hiding

by some other material


3)Polymorphism:


a

single function or single operator has different
character

in different place.


eg/A girl plays a role of daughter at home and a
manager at

office


4)Inheritance:



The new classes, known as derived classes, take over
(or

inherit) attribute and behavior o
f the pre
-
existing
classes,

which are referred to as base classes (or Parent
classes).

It is intended to help reuse existing code with little
or no

modification.


eg/ parent
-
child relation




Is This Answer Correct ?


99 Yes

38 No

3

Ramya






Re: what are the oops concept in java explain with real time examples
Answer

#
4


OOPS Concepts are mainly 4


1.Abstraction


2.Encapsulation


3.Inheritance


4.Poly
morphisam

Abstraction:
-
Hidding non
-
essential features and showing
the

essential features



(or)

Hidding unnecessary data from the users details,is
called

abstraction.

Real Time example:TV Remote Button


in that number format and power buttons

and other
buttons

there.just we are seeing the butttons,we don't see the

button circuits.i.e buttons circutes and wirings all
are

hidden.so i think its good example.


Encapsulation:



Writing Operations and methods stored in a single

class.This

is Called Encapsulation


Real Time Example:Medical Capsuals

i.e one drug is stored in buttom layer and another drug
is

stored in Upper layer these two layers are combined in

single capsual.


Inheritance:


The New Class is Existing from Old Class,i.e
SubClass is

Existing from Super Class.


Real Time Example:


Father and Son Relationship


Polymorphisam:



Sinle Form behaving diffreantly in diffreant

Situations.


Example:
-


Person

Person in Home act is husband/son,


in

Office acts Employer.


in Public Good Cityzen.


I think these all are real time experianceany.is there
any

more answers send me my mail mdevendra@gmail.com also.


Thanks all

Devendra.M




Is This Answer Correct ?


67 Yes

18 No

2

Devendra.m






Re: what are the oops concept in java explain with real time examples
Answer

#
5


OOPS Concepts are mainly 4


1.Abstraction


2.Encapsulation


3.Inheritance


4.Polymorphisam


Abstraction:Hiding unneccessary data from user is
called

Abstraction access specifiers like public,private etc
are

used to provide different level in abstraction.



Encapsulation:Taking data and object in a single unity
is

called Encaps
ulation.A class is example for
Encapsulation.


Inheritance:creating anew class from existing class is

called Inheritance.reusability is main advantage in

inheritance.


polymorphism:if single mthod performs different task is

called polymorphism.




Is This Answer Correct ?


39 Yes

20 No

5

Praveena






Re: what are the oops concept in java explain with real time examples
Answer

#
6


1,class

2,encapsulation

3
,polymorphism

4,inheritance

5,Abstraction



class


collection of attributes is called class

Real time example......human body


encapsulation


To encapsulate the some methods,operations in
one

class is called the encapsulation

Real time examp
le...... capsules


polymorphism


To ability to take more than one form

Real time example...... xerox machine

inheritance


one class properties occur from another class
properties

Real time example......land line...will
phone....cellphone

2g..
..3g....




Is This Answer Correct ?


45 Yes

18 No

5

Niventh Mca






Re: what are the oops concept in java explain with real time examples
Answer

#
7


encapsulation

abstraction

interface

polymorphism

imheritance




Is This Answer Correct ?


15 Yes

39 No

5

Atif Hossain






Re: what are the oops concept in java explain with real time examples
Answer

#
8


oops concepts are mainly 3

1.encapsulation

2.inheritance

3.polymorphism



1 :
-

encapsulation refers to binding the data in a
single

class

the data present inside the encapsulation is
secured.


abstraction comes under encapsulaton.


real time example:
-
if you get fever you will take the

medicine to cure.the medicine is the capsule in which
the

powder is present.the powder is a mixture of seve
ral

chemicals as in the same way all the classes are mixed
and

only the output(object) you will see as if a capsule.



2 :
-

inheritance is nothing but extending the class
from

parent to the child


real time example:
-

the prime minister of india

(late)mr.j
awaharlal nehru and his daughter indira
gandhi

and her son rajiv gandhi and his wife sonia gandhi.



3 :
-

polymorphism refers to which it does many things


real time example:
-

consider the electric power which
does

many things .




Is This Answer Correct
?


11 Yes

30 No

0

Suresh






Re: what are the oops concept in java explain with real time examples
Answer

#
9


We have the following oops concepts like

1.Abtsrction

2.Encapsulation

3.Inheritance

4.Polymorphism


Abstraction:


hiding unnecessary data from the users and making the

application as user friendly then it is called as
abstrcation


ex:Any mail account the user is simply filling the form
and

he

don't know ho it is developed.


Encapsulation:


Wrapping the data and code as a single unit is called
as

encapsulation.


ex:Capsule is the best real time example for it.In it
we

will have two types of drugs combined as single
capsule.


Inheritance :

Acqui
ring the properties from one class(base class or
parent

class)to another class(derived class or child class)is

called as inheritance.


Ex:Inventing laptop from desktop.


Polymorphism:


Single entity behaving in different forms is called as

polymorphism


ex:Mouse is for click the buttons and dragging the
bars or

placing the pointer in a particular place.




Is This Answer Correct ?


31 Yes

7 No

0

Murali Krishna Thanneeru






Re: what are the oops concept in java explain with real time examples
Answer

#
10


We have the following oops concepts like

1.Abtsrction

2.Encapsulation

3.Inheritance

4.Polymorphism


Abstraction:


hiding

unnecessary data from the users and making the

application as user friendly then it is called as

abstrcation


ex:Any mail account the user is simply filling the form
and

he don't know ho it is developed.


Encapsulation:


Wrapping the data and code as a s
ingle unit is called
as

encapsulation.


ex:Capsule is the best real time example for it.In it
we

will have two types of drugs combined as single
capsule.


Inheritance :

Acquiring the properties from one class(base class or
parent

class)to another
class(derived class or child class)is

called as inheritance.


Ex:Inventing laptop from desktop.


Polymorphism:


Single entity behaving in different forms is called as

polymorphism


ex:Mouse is for click the buttons and dragging the
bars or

placing the p
ointer in a particular place.