-->

Header Ads

What is need of abstract class and interface/how can we implement them?

Abstract class and method:

when we work on real life project where hundreds of developer from different location working together then it gives flexibility to manage project. Different methods coded by different member so they don’t need to think about whole project, they just code inside methods. Abstraction also provides template view of any project.it helps to design and integrate different modules of project. 

           Abstraction is important feature of OOPS.it reduces the complexity of program. Abstract class doesn’t provide 100% abstraction because here we can also define concrete method (method which has body or some code) inside of class. We don’t need to define complete methods in abstract class. It shows some sort of guideline of class. 
There are some characteristics of abstract method and class:

1.If any class has abstract method then we have to define class as abstract class.
2.Abstract class can have both the concrete methods and abstract methods.
3.A method declared without body is called an abstract method. Such method has to be declared as abstract.
4.Abstract class cannot be instantiated.
5.If any class extends an abstract class then it must define all the methods of super class, otherwise it has to be declared abstract.

Syntax:


 abstract class class_name {   …………..    }
                  or
 class abstract class_name {………………}

Example:

abstract class geometry 
   {
     int z;
     int multipication(int x,int y)
   {  //concrete method
        return x*y;
      }
     abstract void show();//abstract method
  } 
class area extends geometry
  {
    int triangle(int x,int y)
 {
     
     z=(int)(.5f*multipication(x,y));
     return z;
    }
  void show()
  {
   System.out.println("the area of triangle="+z);
   }
 }
class abstract1
  {
   public static void main(String arg[])
   {
     area a= new area();
     a.triangle(8,10);
     a.show();
     }
}

   Output:  

the area of triangle is =40


 When to use: 

abstract methods are useful when we want to implement same method for different way or different purpose. Two or more sub classes sometimes need to implement in different ways then it is vary useful.

Interface: 

 we can say interface is facility which fulfill the concept of multiple inheritance in java.java doesn’t support multiple inheritance so interface fulfill this drawback. Interface is pure abstract class where we can only declare methods and constant variables. 

There are some properties of interface:

1.Interface definition start with keyword interface.
2.When we want use interface then use keyword implements to inherit interface
3.A class can have more than one interface.
4.Methods inside interface must not be static,final.
5.Methods inside interface are by defaults public and abstract.
6.Variables inside interface are implicitly public static final.
7.One interface can extend another interface.
8.Interface can be nested i.e. one interface can be part of another interface.
9.Interface cannot implements class.

Syntax:

      

 interface interface_name{………….}



Example:

interface method1
{
 void define();
 void show();
 }
 interface const_var1
 {
  final int pmax=99;
  final int pmin=0;
  }
class money implements method1,const_var1
{
      int rupees,paisa;
   money()
   {
    rupees=0;paisa=0;
   }
      money(int a,int b)
         {
    rupees=a; paisa=b;
    } 
       public void check()
             {
               if(paisa>pmax)
               System.out.println("paise can not be more than"+pmax);
               if(paisa<pmin) 
               System.out.println("paise can not be less than"+pmin);
              }
       public void define()
           {
          rupees=34;paisa=98;
           }
       public void show()
           {
           System.out.println("paise="+paisa+"rupees="+rupees);
           } 
}  

class interfacedemo1{
 
 public static void main(String ar[])
 {
  money m1=new money(100,100);
  money m2=new money(30,-20);
  money m3=new money();
     System.out.println(m1.rupees+""+m1.paisa);
     m1.check();
     System.out.println(m2.rupees+""+m2.paisa);
  m3.define();
  m3.show();
  
  
 }
}

 Output: 

    100 100
    paisa can not be more than 99
    30-20
    paisa=98 rupees=34


When to use: 

actually when our program need to implement concept of polymorphism i.e. one thing use in different manner in different places. Then use interface rather than this there are many reasons behind use of interface.


No comments

Powered by Blogger.