extent and implementation of class in JAVA

View previous topic View next topic Go down

extent and implementation of class in JAVA

Post  Skyfall on Mon Jul 15, 2013 10:19 pm

extends is for extending a class.

implements is for implementing an interface

The difference between an interface and a regular class is that in an interface you can only specify methods.

Also java doesn't support multiple inheritance for classes. This is solved by using multiple interfaces

public interface Inter{
   public void do();
   public String doThis(int number);
}

public class sub implements Inter{
    public void do(){
      //specify what must happen
    }

    public String doThis(int number){
      //specfiy what must happen
    }
}

now extending a class

public class SuperClass{
   public int getNb(){
        //specify what must happen
       return 1;
    }

    public int getNb2(){
        //specify what must happen
       return 2;
    }
}

public class SubClass extends SuperClass{
     //you can override the implementation
     @Override
     public int getNb2(){
       return 3;
    }
}

in this case

 Subclass s = new SubClass();
 s.getNb(); //returns 1
 s.getNb2(); //returns 3

 SuperClass sup = new SuperClass();
 sup.getNb(); //returns 1
 sup.getNb2(); //returns 2
avatar
Skyfall

Posts : 4
Join date : 2012-08-16
Location : india

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum