java

java

Wednesday 14 December 2011

OOPS concept in java

OBJECT ORIENTATION IN JAVA

There are three features that makes java to say object oriented are as follows:
1. Inheritance
2. Encapsulation
3. polymorphism
Inheritance:
inheritance is used for reusability like a dictionary.a dictionary is made only once thereafter its only enhanced by inheriting the previous words. lets have an example of inheritance.
class A
{
public void getData()
{
System.out.println("Hi, I am in class A");
}
}
class B extends A
{
public static void main()
{
B obj=new B();
obj.getData();
}
}
there is two classes in the above example named A and B.class A has a user defined method called getData().in the method getData() a string is printed "Hi, I am in class A" by println() method which is system defined method found in class name System.
Now,you have another class B which has no method ,so how the controls will flow from class A to B.controls means whatever is written to be performed by the function.very simple just used extends keyword which means class B inherits all methods and variable of class A.
Now ,its time to see the output create a main() method in class B as shown in the example.just create object of class B.now call method getData() by obj.getData().class B does not have getData() method but it will print the string "Hi, I am in class A" just because of keayword extends.
compilation an execution:
for windows user:
click open cmd and go to your directory where your pragram is saved and type.
javac B.java

(it means you are invoking java compiler.)
if no error then type to run program.
java B
for linux user:
go to your program directory and follow the commands below.
akhtar@akhtar-laptop:~$ cd /media/myDisk
akhtar@akhtar-laptop:/media/myDisk$ javac B.java
akhtar@akhtar-laptop:/media/myDisk$ java B
Hi, I am in class A
akhtar@akhtar-laptop:/media/myDisk$

one thing to be noted that java.lang; package is automatically added by JVM in your program,so don't use import of packages until it is necessary because it will increase the size of your program.
Encapsulation:
Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by an interface.
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this feature Encapsulation gives maintainability, flexibility and extensibility to our code.
Example:
Let us look at an example that depicts encapsulation:
/* File name : EncapTest.java */
public class EncapTest
{
private String name;
private String idNum;
private int age;
public int getAge()
{
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}
The public methods are the access points to this class. fields from the outside java world. Normally these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.
The variables of the EncapTest class can be access as below:
/* File name : RunEncap.java */
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName()+ " Age : "+ encap.getAge());
}
}
This would produce following result:
Name : James Age : 20
Benefits of Encapsulation:
* The fields of a class can be made read-only or write-only.
* A class can have total control over what is stored in its fields.
* The users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.

polymorphism:
polymorphism means polymorphic behavior means more than one in nature.for ex:
Class Example
{ public void getData(int x,int y) // it means when method is called it needs two parameter of integer type.
{
return x+y; // method give the addition of both integer to the object who called the method.
}
public void getData(String x,String y) // it means when method is called it needs two parameter of integer type.
{
return x+y; // method give the cocatination of both string to the object who called the method.
}
public staic void main(String []str)
{
Example obj=new Example();
obj.getData(2,4);
obj.getData("Akhtar"+"Nawaz");
}
}
in the above example you have a class name example.you have two methods in Example class name getData().now u would be confuse to see two method with same name in the same class.you can put any no of method having same name in the same class but only one condition that there signature must be defferent.signature means input parameter.like
getData(int x,int y). //method has to signature int x and int y.
getData(String x,String y).
signature can be different by 3 types:
1.number of parameter
2.types of parameter
3.sequence of parameter.
this concept is known as method overloading.
now when your call the method by giving two parameter of integer type the getdata() method is adding the integer,but when giving string the method concatinate them.so its showing dual behaviour addition on integer but concatination on string,its polymorphism.

1 comment: