Showing posts with label INTERFACE. Show all posts
Showing posts with label INTERFACE. Show all posts

Wednesday 27 January 2016

Difference between abstract classes and interfaces

  1. Abstract classes doesn't support multiple inheritance but interface supports same.
  2. Abstract class can have final/non final and static/non static variables, but interfaces has only static and final variables.
  3. abstract keyword is used to create an abstract class and it can be used with methods also whereas interface keyword is used to create interface and it can’t be used with methods.
  4. Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.
  5. Abstract classes can have methods with implementation whereas interface provides absolute abstraction and can’t have any method implementations.
  6. Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.
  7. We can run an abstract class if it has main() method but we can’t run an interface because they can’t have main method implementation.

Example of abstract classes and interfaces in java

interface I{
void x();
void y();
void z();
}
abstract class SampleAbstract implements I{
public void z(){System.out.println("Inside z");}
}
class Sample extends SampleAbstract {
public void x(){System.out.println("Inside x");}
public void y(){System.out.println("Inside y");}
public void z(){System.out.println("Inside z");}
}
class TestOne{
public static void main(String args[]){
I a=new Sample ();
a.x();
a.y();
a.z();
}}  

Output:
Inside x
Inside y
Inside z



Tuesday 26 January 2016

Interface in java


An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. Its a java mechanism to achieve full abstraction. In other way interface is the mechanism to achieve fully abstraction and multiple inheritance in java. An interface can contain any number of methods

The interface keyword is used to declare an interface. Here is a simple example to declare an interface

Declaring Interfaces:
import java.lang.*;

interface syntax:
public interface NameOfInterface
{
//Any number of final, static fields
//Any number of abstract method declarations\
}

interface example:
interface Car{
public void drive();
public void travel();
}

Relationship between classes and interfaces
A class extends another class, an interface extends another interface but a class implements an interface.

Example of interface
interface sports{
void score();
}
class Sample implements sports{
public void score(){System.out.println("Score here");}
public static void main(String args[]){
Sample obj = new Sample ();
obj.score();
 }
}  

Output:
Score here

And through interfaces we can achieve multiple inheritance in java.

Monday 3 March 2014

AWT CONTROLS IN JAVA:BUTTONS

AWT BUTTON Class

Create AWT Buttons with simple Example..........

The java.awt package comes with many GUI components of which Button is the most important and most frequently used. A button functionality (what for it is) can be known by its label like OK or Cancel etc. A mouse click on the button generates an action.

Following is the class signature

public class Button extends Component implements Accessible

Any GUI component with event handling can be broadly divided into 8 steps.

  • Import java.awt and java.awt.event packages
  • Extending frame or applet and implementing appropriate listener interface that can handle    the events of the component successfully.
  • Set the layout.
  • Create components
  • Register or link the component with the listener interface
  • Beautification of the components (optional)
  • Adding the components to the frame or applet
  • Override all the abstract methods of the listener interface