Thursday, September 24, 2015

Java Keywords

(interface, abstract, import, implements)
http://www.try2catch.in

Interface: Interface is same as like a class. It is also contains methods and variables. But, behavior of the member is different. To declare a class as interface we just need to use interface keyword in place of class keyword. In case of use of the members of interface, you need to use implements keyword to get all the members in class. We could never make any object of an interface. This keyword is only use with a class to which we want to declare as interface. All the members are public.

1)   Variables: Variable of an interface is always public static and final.

2)   Methods: Methods of an interface are always abstract. In other words, they have only declaration not implementation.

package com.demo;

public interface InterfaceDemo {

       public static final String name = "Try Catch";

       public String showName();

}
  


Abstract: Abstract can be a class or method. It contains all normal member of a concrete class. To declare a class or method as abstract, we need to use abstract keyword. If a class is declared as abstract, it is not mandate that it should have at least one abstract method. To use any member of abstract class, we need to use extends keyword. But if a method is declared as abstract then the class must be an abstract class. Abstract method should not be private. Abstract class may have non abstract methods. It can have private members also. Abstract keyword can be use with class and method.



package com.demo;

public abstract class AbstractDemo {

       private String name = "Try Catch";

        abstract String showName();

       private void getName() {

              System.out.println(name);
       }

}

Implements: This keyword works same as extend keyword but to inherit the properties of an interface.

package com.demo;

public abstract class AbstractDemo implements InterfaceDemo {

@Override
public String showName() {
       // TODO Auto-generated method stub
       return null;
}


}

Import: To inherit the properties of any class, we need to give the full path of the class directory in form of packages.

package com.demo;

import com.demo.*;
public abstract class AbstractDemo  {


}


No comments:

Post a Comment

Thank you for your comments.