Full width home advertisement

Post Page Advertisement [Top]

For implementing object oriented model, Java provide three  mechanism :

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction
1. Encapsulation: It is a mechanism of hiding data. It hide the visiblity of fields and field can be changed by using public methods so that the code can be protect from unauthorized access by outer world.
Example:


public class EncapTest{

   private String name;
   private int age;

   public int getAge(){
      return age;
   }

   public String getName(){
      return name;
   }

   public void setAge( int mAge){
      age = mAge;
   }

   public void setName(String mName){
      name = mName;
   }


}

2. Inheritance: It is a mechansim by which a class can acquire the properties(methods and attributes) of another class by using a keyword "extends".

Example:
public class mobile
{
String os;
public double getOs()
{
return os;
}
}


public class smartPhone extends mobile
{
String network;
public String getNetwork()
{
return network;
}

}

smartPhone class have all the properties that itself have as well as class mobile have.

3. Polymorphism: It means that the ability to process objects of various types and classes through a single and uniform interface.
Method have different types and numbers of parameter are known as polymorphism.

Example: I have to make a method which can add two string or add two numbers or display a string.

class PolTest
{
public static void main(String args[])
{
PolTest p=new  PolTest();
p.myFun("hello"); // it prints: hello
p.myFun("Java ","Language"); //it prints: Java Language
p.myFun(2,3); //it prints: 5
}
public void myFun(String a1)
{
System.out.println(a1);
}

public void myFun(String a1, String a2)
{
System.out.println( a1 + a2);
}

public void myFun(int a1, int a2)
{
System.out.println(a1+a2);
}
}

This is also called Java overloading method. See Here


4. Abstraction: Abstraction is an ability to hide the implementation details and still showing all the functionality.
Abstraction can be achieved by making a class abstract by using keyword abstract or by using keyword interface.
abstract give 0%-100% abstraction, while interface give 100% abstraction. Abstraction class can't be initialized. It must be extends or implements to a non-abstract class.
All the methods which are abstract must be override in non-abstract class.

Rules for Abstract Class:

  1. One or more method must be abstract in abstract class.
  2. Abstract class can't initialized.
  3. Child class(which extends abstract class) must override abstract  methods.
Rules for Interface Class
  1. All methods of interface class must be abstract.
  2. It can't also initialized.
  3. It uses "implements" keyword for making its child class.
  4. Child class(which implements interface class) must override all the methods.
Example:
abstract class Student
{
   public String name;
   public String address;
   public abstract String toString();
 
   public void getName()
   {
      System.out.println(name);
   }
 
   public void getAddress()
   {
      System.out.println(address);
   }

}

class Main extends Student
{
public String toString()
   {
      return "Name: " + name + ", Address: " + address;
   }
 
   public static void main(String args[])
   {
Main m=new Main();
m.name = "ABC";
m.address = "XYZ";
m.getName();
m.getAddress();
System.out.println(m.toString());
}
}

No comments:

Post a Comment

Bottom Ad [Post Page]

| Designed by Colorlib