Home Java Beginners Object Oriented Programming in Java

 
 

Share on Google+Share on Google+

Object Oriented Programming in Java

Advertisement
OOPS acronym for Object Oriented Programming is a model or concept that works around objects and data. Programming language like Java that follow the OOP concept are used to create programs based on the real world.

OOPS acronym for Object Oriented Programming is a model or concept that works around objects and data. Programming language like Java that follow the OOP concept are used to create programs based on the real world. By the name OOPS suggest that it works around objects very similar to that exist in our real world. Every object has a certain behavior, properties, type, and identity. There can be more than two objects with same behavior, properties, type, and identity, which can be clubbed together.

The principal aim of OOPS is to find the behavior, properties, type, and identity of an object and than use it to create applications or programs.

OOPs involves analysis of the problem, preparing a solution, coding and maintenance.

Other languages like C and C++ do not completely follow OOPS concept while Java does. Java was designed based on C and C++. But the developers who developed the language wanted it to be better than C and C++ and also easy to code in. Everything in Java is object or can be made object. For ex. we can convert primitive data types into object using the wrapper class.

In Java class is a group of similar object. Objects here can have same behavior, properties, type or identity. Object in Java is an instance of a class.

For a language to be an Object Oriented Programming Language, it must follow:

Inheritance: Inheritance means creating a new class that has the same feature and behavior as of an existing class. In a way it means extending a class to a sub class for reusing the existing code and adding some additional features.

Example of Inheritance:

class A{
public void fun1(int x){
System.out.println("Int in A is :" + x);
}
}
class B extends A{
public void fun2(int x,int y){
fun1(6); // prints "int in A"
System.out.println("Int in B is :" + x " and "+y);
}
}
public class inherit{
public static void main(String[] args){
B obj= new B();
obj.fun2(2);
}
}

Encapsulation: Encapsulation is the ability to bind the property and method of the object and also operate them. Encapsulation helps in hiding the data within the class so it cannot be used by anything outside the class and helps in protecting the data. The data is available only through methods.

Example of encapsulation:

class Check{
private int amount=0;
public int getAmount(){
return amount;
}
public void setAmount(int amt){
amount=amt;
}
}
public class Mainclass{
public static void main(String[] args){
int amt=0;
Check obj= new Check();
obj.setAmount(200);
amt=obj.getAmount();
System.out.println("Your current amount is :"+amt);
}
}

Polymorphism: Polymorphism means providing different functionality by the functions having the same name based on the signatures of the methods. Compile time polymorphism is supported through the method overloading concept in java.

There are two types of polymorphism:

  • Compile-time polymorphism
  • Runtime Polymorphism

Example of Polymorphism:

class A{
public void fun1(int x){
System.out.println("The value of class A is : " + x);
}
public void fun1(int x,int y){
System.out.println("The value of class B is : " + x + " and " + y);
}
}
public class polyone{
public static void main(String[] args){
A obj=new A();
// Here compiler decides that fun1(int) is to
be called and "int" will be printed.
obj.fun1(2);
// Here compiler decides that fun1(int,int)is
to be called and "int and int" will be printed.
obj.fun1(2,3);
}
}
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 18, 2014

Related Tutorials

Discuss: Object Oriented Programming in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com