Professional Documents
Culture Documents
class <class_name>
{
data member;
method;
}
Example
class Student1
{
int id; //data member (also instance variable)
String name; //data member(also instance variable)
public static void main(String args[])
{
Student1 s1=new Student1();
//creating an object of Student
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Method Overloading
Method with same name but different parameter list.
Advantages of Method Overloading:
Increases the reliability of the program
Different ways to overload the method:
There are two ways to overload the method in java
1. By changing the number of arguments
2. By changing the data type.
class Calculation3
{
int sum(int a,int b){System.out.println(a+b);}
double sum(int a,int b){System.out.println(a+b);}
public static void main(String args[])
{
Calculation3 obj=new Calculation3();
int result=obj.sum(20,20); //Compile Time Error
}
}
Method Overriding
Writing the method in the child class which has same
name and signature as that of parents.
Rules for Java method Overriding:
1. method must have same name as in the parent class
2. method must have same parameter as in the parent class.
3. must be IS-A relationship (inheritance).
Constructor in Java
Constructor in java is a special type of method that is used to initialize
the object.
Java constructor is invoked at the time of object creation.
Rules for creating java Construction:
There are basically two rules defined for the constructor.
1.
2.
Parameterized constructor.
In the above class,you are not creating any constructor so compiler provides you a default constructor.Her
0 and null values are provided by default constructor.
Parameterized Constructor
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Output:
111 Karan
222 Aryan
Java Method
Java Applet
Java Programs are of two types
1. Applets
2. Application
Applet are java programs which can be Embedded inside web Page
Applet
Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4
life cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only
once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
5. public void paint(Graphics g): is used to paint the Applet. It provides
Graphics class object that can be used for drawing oval, rectangle, arc etc.
java.applet.Applet class
For creating any applet java.applet.Applet class must be inherited. It provides 4
life cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is
stop or browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only
once.
java.awt.Component class
The Component class provides 1 life cycle method of applet.
5. public void paint(Graphics g): is used to paint the Applet. It provides
Graphics class object that can be used for drawing oval, rectangle, arc etc.
public abstract void drawLine(int x1, int y1, int x2, int y2):
draw line between the points(x1, y1) and (x2, y2).
is used to
7.
8.
public abstract void drawArc(int x, int y, int width, int height, int
startAngle, int arcAngle): is used draw a circular or elliptical arc.
9.
public abstract void fillArc(int x, int y, int width, int height, int startAngle,
int arcAngle): is used to fill a circular or elliptical arc.
10. public abstract void setColor(Color c): is used to set the graphics current color
to the specified color.
11. public abstract void setFont(Font font): is used to set the graphics current font
to the specified font.
Java AWT
Java AWT (Abstract Windowing Toolkit) is an API to
develop GUI or window-based application in java.
Java AWT components are platform-dependent i.e.
components are displayed according to the view of
operating system. AWT is heavyweight i.e. its components
uses the resources of system.
The java.awt package provides classes for AWT api such
as TextField, Label, TextArea, RadioButton, CheckBox,
Choice, List etc.
AWT Hierarchy
Swing Hierarchy