Showing posts with label interface. Show all posts
Showing posts with label interface. Show all posts

Get all interface from a class object

This program will print name of interface that implemented by this class

**********************************************************
COMPLETE SOURCE CODE FOR : ListingAllInterfaceFromClassObject.java
**********************************************************


import java.awt.event.ActionListener;//INTERFACE 1
//Runnable-INTERFACE 2
import java.awt.event.ActionEvent;

public class ListingAllInterfaceFromClassObject implements ActionListener,Runnable
{
public void actionPerformed(ActionEvent evt)
{
//NO IMPLEMENTATION
}

public void run()
{
//NO IMPLEMENTATION
}

public static void main(String[]args)
{
try
{
Class temp=Class.forName("ListingAllInterfaceFromClassObject");
Class[]allInterface=temp.getInterfaces();
int allInterfaceLength=allInterface.length;
int temp2=0;
while(temp2!=allInterfaceLength)
{
System.out.println("INTERFACE "+(temp2+1)+" :"+(allInterface[temp2].getSimpleName()));
temp2=temp2+1;
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


**********************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************

Determine what is represent by a class object, whether an interface or a class ???

This tutorial will use CreateInterface.java to determine whether class object from it, is an interface or a class. You can get it at Java Interface ( Part 1). Before you compile and execute source code below, you must make sure CreateInterface.class is locate same location with this java file.

***********************************************************
COMPLETE SOURCE CODE FOR : DetermineClassObjectIsClassOrInterface.java
***********************************************************


public class DetermineClassObjectIsClassOrInterface
{
public static void main(String[]args)
{
try
{
Class temp=Class.forName("CreateInterface");
if(temp.isInterface())
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN INTERFACE");
}
else
{
System.out.println("THIS CLASS OBJECT IS REPRESENT AN CLASS");
}
}
catch(Exception exception)
{
exception.printStackTrace();
}
}
}


***********************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************

Java Interface (Part 2)

This tutorial will show you, how to use a java interface that we create in my previous post. When we want to use a java interface, we must overwrite abstract method that contain in the interface. Source code below will overwrite abstract method in java interface (CreateInterface) called print(). Before we can use a java interface we must put implements keyword followed by interface name. You also must import interface into your program. In the example below, i don't import the interface because, it's location is same with this java file.

*******************************************************
COMPLETE SOURCE CODE FOR : UseJavaInterface.java
*******************************************************


public class UseJavaInterface implements CreateInterface
{
public void print()
{
System.out.println("HI");
}
}


*******************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************

Java Interface (Part 1)

Source code below will create a java interface, named CreateInterface. When you want to create an interface, you just change "class" keyword after "public" keyword to "interface" keyword and after that followed by your java file name. In a java interface, all method is only abstract method. You don't need to put access modifier to abstract method in a java interface. This is because all of it will be set to public. So when you want to overwrite an abstract method from an interface you must know, access modifier that you must use is public, not private or protected. After that put return type followed by name for the abstract method. In source code below, abstract method is print(). What is java abstract method ??? The answer is, an abstract method is a method without body. If you can see, abstract method print() end after ")" ,not contain any implementation code. Where to put implementation code for a java abstract method ??? You will put it's implementation code in java concrete class that implements this interface by overwrite it.

*****************************************************************
COMPLETE SOURCE CODE FOR : CreateInterface.java
*****************************************************************


public interface CreateInterface
{
void print();
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Create graphical user interface using classes in java.awt package


In this source code, awt component that include is :
FRAME
BUTTON
PANEL
LABEL
TEXTAREA
TEXTFIELD
CHECKBOX
RADIO BUTTON

layout manager is :
GRID LAYOUT
FLOW LAYOUT

others :
COLOR

****************************************************************
COMPLETE SOURCE CODE FOR : CreatingInterfaceUsingAwt.java
****************************************************************


import java.awt.Frame;//FOR FRAME IN AWT
import java.awt.Button;//FOR BUTTON IN AWT
import java.awt.Panel;//FOR PANEL IN AWT
import java.awt.TextArea;//FOR TEXTAREA IN AWT
import java.awt.TextField;//FOR TEXTFIELD IN AWT
import java.awt.Label;//FOR LABEL IN AWT
import java.awt.List;//FOR LIST IN AWT

//FOR CHECKBOX AND RADIOBUTTON IN AWT
import java.awt.CheckboxGroup;
import java.awt.Checkbox;

//LAYOUT MANAGER
import java.awt.FlowLayout;
import java.awt.GridLayout;

import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CreatingInterfaceUsingAwt
{
public static void main(String[]args)
{
Frame a=new Frame("All Of This Component Is Build Using Java AWT");
a.setLayout(new FlowLayout());

WindowListener cl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);//WHEN CLICK WINDOW CLOSE BUTTON, APPLICATION WILL EXIT
}
};

a.add(new Button("THIS IS BUTTON"));//ADD A BUTTON WITH LABEL "THIS IS BUTTON"
a.add(new Label("THIS IS A LABEL"));//ADD A LABEL WITH SPECIFIED TEXT "THIS IS A LABEL"

//CREATE A PANEL THAT MAY CONTAIN OTHER AWT COMPONENT
Panel b=new Panel(new FlowLayout());//CREATE A PANEL AND SET IT'S LAYOUT
b.setBackground(Color.BLACK);//SET BACKGROUND COLOR FOR THIS PANEL TO BLACK
b.add(new Button("BUTTON_1 IN PANEL"));
b.add(new Button("BUTTON_2 IN PANEL"));
b.add(new Button("BUTTON_3 IN PANEL"));
a.add(b);//ADD THIS PANEL TO FRAME

a.add(new TextArea("THIS IS TEXT AREA"));//ADD A TEXT AREA WITH INITIAL TEXT "THIS IS TEXT AREA"
a.add(new TextField("THIS IS TEXT FIELD"));//ADD A TEXT FIELD WITH INITIAL TEXT "THIS IS TEXT FIELD"

//CREATE CHECKBOX
Panel d=new Panel(new GridLayout(3,1));
d.setBackground(Color.RED);
d.add(new Checkbox("CHECKBOX 1",null,false));//CREATE A CHECKBOX c=checkbox group, false=initial state of checkbox
d.add(new Checkbox("CHECKBOX 2",null,false));//if false, no check for the checkbox in initially
d.add(new Checkbox("CHECKBOX 3",null,false));
a.add(d);//ADD THIS PANEL TO FRAME

//CREATE RADIO BUTTON
Panel e=new Panel(new GridLayout(3,1));
e.setBackground(Color.BLUE);
CheckboxGroup c=new CheckboxGroup();//CREATE A CHECKBOX GROUP
e.add(new Checkbox("RADIOBUTTON 1",c,false));//c=checkbox group
e.add(new Checkbox("RADIOBUTTON 2",c,false));
e.add(new Checkbox("RADIOBUTTON 3",c,false));
a.add(e);//ADD THIS PANEL TO FRAME
//What is different between radio button and checkbox ???
//The different is at CheckboxGroup.
//For radio button, we must set to certain checkbox group(In this case we set c for checkbox group in checkbox argument after checkbox name)

//CREATE LIST
List f=new List(5,true);//If you want to disable multiple selection, you just change true to false
f.add("LIST 1");
f.add("LIST 2");
f.add("LIST 3");
f.add("LIST 4");
f.add("LIST 5");
a.add(f);//ADD THIS PANEL TO FRAME

a.addWindowListener(cl);
a.setSize(800,600);
a.setVisible(true);
}
}


****************************************************************
JUST COMPILE AND EXECUTE IT
****************************************************************

RELAXING NATURE VIDEO