Common Interfaces in Java

0
599
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Each container object has a Layout manager which is an instance of any class that implements LayoutManager interface.Thus each component(Frame,Panel,Label etc) will have its default layout manager associated with it which may be changed by developer when an instance of that container is created.

Each layout manager keeps track of its child components.The layout manager is notified each time one adds a component to the container.Java supports five different Layout Managers.They are as follows:

  1. FlowLayout
  2. BorderLayout
  3. GridLayout
  4. GridBagLayout
  5. CardLayout.
  6. GridBagConstraint

The interface LayoutManager specifies the methods that all the above layout manager must implement.A LayoutManager is responsible for laying out components by setting the size and position of each component of a container.

The interface structure of the LayoutManager is given as:

public abstract interface java.awt.LayoutManager {
//methods
public abstract void addLayoutComponent(String name,Component comp);
//When we add a component to the container we call addLayoutComponent() 
public abstract void layoutContainer(Container target);
public abstract Dimension minimumLayoutSize(Container target);
//returns the minimum width and height for the LayoutManager to lay components
public abstract Dimension preferredLayoutSize(Container target);
//returns the optimal width and height for the LayoutManager to lay components
public abstract void removeLayoutComponent(Component comp);
//we call removeLayoutComponent() when a component is removed.
} 

Interface MenuContainer in AWT in Java

The MenuContainer interface specifies the methods that all menu related containers must implement.Note that menu containers are not required to be full fledged container object.

The interface structure of MenuContainer is given as:


public interface java.awt.MenuContainer{
//methods:
public abstract Font getFont();
public abstract boolean postEvent(Event evt);
public abstract  void remove(MenuComponent comp);
}
 

Interface WindowPeer in AWT in Java

The WindowPeer Interface specifies the methods that all implementations of Abstract Window Toolkit must define while working with Window.

The architecture of the interface WindowPeer is given as :

public interface java.awt.peer.WindowPeer extends java.awt.peer.ContainerePeer{
//methods:
public abstract void toBack();
//sends this window to the back
public abstract void toFront();
//Brings the window to the front.
} 

Interface ButtonPeer in AWT in Java

The ButtonPeer Interface specifies the methods that all implementations of Abstract Window Toolkit must define while working with Window.

The architecture of the interface ButtonPeer is given as :


public interface java.awt.peer.ButtonPeer extends java.awt.peer.ContainerePeer{
//methods:
public abstract void setLabel(String label);
//changes the button's label to the String argument
} 

Interface CanvasPeer in AWT in Java

The CanvasPeer interface specifies the methods that all implementations of Abstract Window Toolkit must define while working with Canvas.In general canvas implementations only need to implement methods required of all component peers.

The architecture of the interface CanvasPeer is given as :


public interface java.awt.peer.CanvasPeer extends java.awt.peer.ComponentPeer{
}
 

Interface DataInput In java

The DataInput interface is implemented by streams that can read primitive java data types from a Stream in a machine dependent manner.This is popular Interface example in java.

The interface DataInput is given as:

public interface java.io.DataInput{
//methods:
public abstract boolean readBoolean();
public abstract byte readByte();
public abstract char readChar();
public abstract double readDouble();
public abstract float readFloat();
public abstract void readFully(byte b[])
public abstract void readFully(byte b[],int offSet,int length);
public abstract int readInt();
public abstract String readLine();
public abstract long readLong();
public abstract short readShort();
public abstract int readUnsignedByte();
public abstract int readUnsignedShort();
public abstract String readUTF();
public abstract int skipBytes(int n);
} 

All these methods can throw EOFException if this stream reaches the end before skipping all bytes also can throw IOException if an I/O error occurs.

Interface DataOutput in Java

The DataOutput interface is implemented by streams which can write primitive data types to an output stream in a machine independent manner.This is popular Interface example in java.

The structure of the DataOutput interface is given as:


public interface java.io.DataOutput{
//methods
public abstract void write(byte b[]);
public abstract void write(byte b[],int offSet,int length);
public abstract void write(int i);
public abstract void writeBoolean(boolean b);
public abstract void writeByte(int v);
public abstract void writeBytes(String s);
public abstract void writeChar(int b);
public abstract void writeChars(String s);
public abstract void writeDouble(double d);
public abstract void writeFloat(float f);
public abstract void writeInt(int b);
public abstract void writeLong(long l);
public abstract void writeShort(int b);
public abstract void writeUTF(string str);
}
 

In addition,all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

Interface URLStreamHandlerFactory in Java

Interface URLStreamHandlerFactory defines a factory for URL stream protocol handlers.It is used by URL class to create a URL Stream handler for a specific protocol.This is popular Interface example in java.

The structure of the class URLStreamHandlerFactory is defined as:


public interface java.net.URLStreamHandlerFactory{
//methods
public abstract URLStreamHandler createURLStreamHandler(String protocol);
//returns a url stream handler for the specific protocol like ftp,http etc
}

Interface Observer in Java

A class can implement the observer interface when it wants to be informed of the observable objects changes.This is popular Interface example in java.

The interface structure is given as


public interface java.util.observer{
//methods
public abstruct void update(Observable obj,Object arg);
this method is called whenever the observed object is changed.An application calls an observable object's notifyObservers() method to have all object's observers notified of the changes.
}
 

Enumeration in Java:

Even though java supports the eight predefined built in primitive types and many non primitive types,but still can not cover all possible types of classes or situation.To support this java provides enum(Short of Enumerated types).

It is a special type of class.An enum is type of fixed list of possible values.The values needs to be defined when the enum is created.An enum is not primitive data type.Each enumerated type constant is a public final static member variables.

The values of the variable is an object belonging to the enumerated class.There is one such object for each enumerated type constant and there are the only objects of the class that can ever be created.

It is really thse objects that represent the possible values of the enumerated type.An object that implements the Enumeration interface generates a series of elements,one at a time.The enumerated type constants are actually variables that refer to these objects.

Successive calls to the nextElement() method which returns the successive elements of the Enumeration series, methods are provided to enumerate through the elements of a vector, the key of a hashtable and the values in a hashtable.Enumerations are also used to specify the input streams from a sequenceInputStream.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here