You are on page 1of 11

//polymorphism

class Circle
{
double rad;
final double pi=3.14;
Circle(double r)
{
rad=r;
}
double area()
{
return pi*rad*rad;
}
double circum()
{
return 2*pi*rad;
}
}
class Cylinder extends Circle
{
double ht;
Cylinder(double r,double h)
{
super(r);
ht=h;
}
double area()
{
return circum()*ht;
}
double volume()
{
return area()*ht;
}
}
class Polytest
{
public static void main(String args[])
{
double area,surface_area,volume;
Circle cir=new Circle(5.5);
Cylinder cyl=new Cylinder(3.2,12.5);
area=cir.area();
System.out.println("area of the circle is"+area);
surface_area=cyl.area();
System.out.println("area of the cylinder is"+surface_area);
volume=cyl.volume();
System.out.println("volume of the cylinder is"+volume);
}
}
Output:
area of the circle is94.985
area of the cylinder is251.20000000000005
volume of the cylinder is3140.0000000000005
//inner class
class OuterClass
{
public OuterClass()
{
System.out.println("Outer Class");
//constructor for inner class
new InnerClass();
}
//defining Inner Class
private class InnerClass
{
InnerClass()
{
System.out.println("Inner Class or Local Class");
}
}

}
class inner
{
public static void main(String[] args)
{
new OuterClass();
}
}
Output:
Outer Class
Inner Class or Local Class

//Anonymous class
/*An anonymous class is a local class without a name.
//An anonymous class is defined and instantiated in a single succinct expression using the new
operator.
//While a local class definition is a statement in a block of Java code, an anonymous class definition
is an expression, which means that it can be included as part of a larger expression, such as a
method call.
//When a local class is used only once, consider using anonymous class syntax, which places the
definition and use of the class in exactly the same place. */
public class Anonymous
{
public static void main(String[] args)
{
// Here is where the anonymous class is defined and instantiated
Runnable runner = new Runnable()
{
int Total = 0;
public void run()
{
for (int i=0; i<10000000; i++)
{
Total++;
}
System.out.println(Integer.toString(Total));
}
};
// Here is where the anonymous class is defined and instantiated
runner.run();
}
}

OUTPUT:

10000000

//static inner class

class OuterClass
{
public OuterClass()
{
System.out.println("Outer Class");
//constructor for inner class
new InnerClass();
}
//defining Inner Class
private static class InnerClass
{
InnerClass()
{
System.out.println("Inner Class or Local Class");
}
}

}
class Staticinner
{
public static void main(String[] args)
{
new OuterClass();
}
}
OUTPUT

10000000
//Event Handling and Actions

import java.awt.*;
import java.awt.event.*;

public class AwtEvent extends Frame implements ActionListener


{
Label lbl;
public static void main(String argv[])
{
AwtEvent t = new AwtEvent();
}

public AwtEvent()
{
super("Event in Java awt");
setLayout(new BorderLayout());
try
{
Button button = new Button("INSERT_AN_URL_HERE");
button.addActionListener(this);
add(button, BorderLayout.NORTH);
Button button1 = new Button("INSERT_A_FILENAME_HERE");
button1.addActionListener(this);
add(button1, BorderLayout.SOUTH);
lbl = new Label("Roseindia.net");
add(lbl, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch (Exception e){}
setSize(400,400);
setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
Button bt = (Button)e.getSource();
String str = bt.getLabel();
lbl.setText(str);
}
}
Output:

//Mouse Handler
//mouse event

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MouseListenerDemo extends JFrame


{
public MouseListenerDemo()
{
initComponents();
}
JTextArea textArea = new JTextArea();
protected void initComponents()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);

textArea.setText("Press the mouse button...");


textArea.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
textArea.setText("MouseListenerDemo.mouseClicked");
}

public void mousePressed(MouseEvent e)


{
textArea.setText("MouseListenerDemo.mousePressed");
}
public void mouseReleased(MouseEvent e)
{
textArea.setText("MouseListenerDemo.mouseReleased");
}

public void mouseEntered(MouseEvent e)


{
textArea.setText("MouseListenerDemo.mouseEntered");
}

public void mouseExited(MouseEvent e)


{
textArea.setText("MouseListenerDemo.mouseExited");
}
});

getContentPane().add(textArea);
}

public static void main(String[] args)


{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new MouseListenerDemo().setVisible(true);
}
});
}
}

Output
:

//Object
Cloning
public class
MainClass2 {
public static void
main(String[] args)
{
Employee emp1
= new
Employee("MNO",
"A");
emp1.setSalary(40000.0);
Employee emp2 = emp1;
System.out.println("Before cloning");
System.out.println("Unchanged Value");
System.out.println(emp1);
System.out.println(emp2);
emp1.setLastName("ABC");
System.out.println("changed Value");
System.out.println(emp1);
System.out.println(emp2);
emp2=(Employee) emp1.clone();
System.out.println("After cloning");
System.out.println("Unchanged Value");
System.out.println(emp1);
System.out.println(emp2);
emp1.setLastName("XYZ");
System.out.println("changed Value");
System.out.println(emp1);
System.out.println(emp2);
}
}

class Employee {
private String lastName;

private String firstName;

private Double salary;

public Employee(String lastName, String firstName) {


this.lastName = lastName;
this.firstName = firstName;
}

public String getLastName() {


return this.lastName;
}

public void setLastName(String lastName) {

this.lastName = lastName;
}

public String getFirstName() {


return this.firstName;
}

public void setFirstName(String firstName) {


this.firstName = firstName;
}

public Double getSalary() {


return this.salary;
}

public void setSalary(Double salary) {


this.salary = salary;
}

public Object clone() {


Employee emp;
emp = new Employee(this.lastName, this.firstName);
emp.setSalary(this.salary);
return emp;
}

public String toString() {


return this.getClass().getName() + "[" + this.firstName + " " + this.lastName + ", "
+ this.salary + "]";
}
}

Output:

Before cloning
Unchanged Value
Employee[A MNO, 40000.0]
Employee[A MNO, 40000.0]
changed Value
Employee[A ABC, 40000.0]
Employee[A ABC, 40000.0]
After cloning
Unchanged Value
Employee[A ABC, 40000.0]
Employee[A ABC, 40000.0]
changed Value
Employee[A XYZ, 40000.0]
Employee[A ABC, 40000.0]

//Type Parameters

public class GenericMethodTest


{
// generic method printArray
public static < E > void printArray( E[] inputArray )
{
// Display array elements
for ( E element : inputArray ){
System.out.printf( "%s ", element );
}
System.out.println();
}
public static void main( String args[] )
{
// Create arrays of Integer, Double and Character
Integer[] intArray = { 1, 2, 3, 4, 5 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

System.out.println( "Array integerArray contains:" );


printArray( intArray ); // pass an Integer array

System.out.println( "\nArray doubleArray contains:" );


printArray( doubleArray ); // pass a Double array

System.out.println( "\nArray characterArray contains:" );


printArray( charArray ); // pass a Character array
}
}

Output:

Array integerArray contains:


12345

Array doubleArray contains:


1.1 2.2 3.3 4.4

Array characterArray contains:


HELLO

//JLabel
import javax.swing.*;
import java.awt.*;

class MainFrame2 extends JFrame


{
MainFrame2()
{
MainPanel2 mp=new MainPanel2();
add(mp);
}
public static void main(String s[])
{
MainFrame2 mf=new MainFrame2();
mf.setVisible(true);
mf.setDefaultCloseOperation(EXIT_ON_CLOSE);
mf.pack();
}
}
class MainPanel2 extends JPanel
{
MainPanel2()
{
JLabel l=new JLabel("Label");
add(l);
}
}
//JTextArea
import javax.swing.*;
import java.awt.*;

class MainFrame1 extends JFrame


{
MainFrame1()
{
MainPanel1 mp=new MainPanel1();
add(mp);
}
public static void main(String s[])
{
MainFrame1 mf=new MainFrame1();
mf.setVisible(true);
mf.setDefaultCloseOperation(EXIT_ON_CLOSE);
mf.pack();
}
}
class MainPanel1 extends JPanel
{
MainPanel1()
{
JTextArea l=new JTextArea(2,20);
l.setText("Text Area");
add(l);
}
}
//Jtextfield
import javax.swing.*;
import java.awt.*;

class MainFrame3 extends JFrame


{
MainFrame3()
{
MainPanel3 mp=new MainPanel3();
add(mp);
}
public static void main(String s[])
{
MainFrame3 mf=new MainFrame3();
mf.setVisible(true);
mf.setDefaultCloseOperation(EXIT_ON_CLOSE);
mf.pack();
}
}
class MainPanel3 extends JPanel
{
MainPanel3()
{
JTextField l=new JTextField("Text Field");
add(l);
}
}

You might also like