You are on page 1of 27

Class in Java

Class is a Collection of variables and methods (Valid for all OOPS)


Class is user defined data type.
Syntax to declare a class:

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.

1 . By changing the no. of arguments


class Calculation
{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[])
{
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Output :30
40

2 . By changing the datatype


class Calculation2
{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
public static void main(String args[]){
Calculation2 obj=new Calculation2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}
Output :21.0
40

Why Method Overloading is not possible by


changing the return type of method?
In java, method overloading is not possible by changing the return type of the method because
there may occur ambiguity [situation in which the compiler cannot determine which method to
use.]

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 Overloading and Type Promotion


One type is promoted to another implicitly if no matching data type is
found. Let's understand the concept by the figure given below:

Example of Method Overloading with


TypePromotion
class OverloadingCal
{
void sum(int a,long b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[])
{
OverloadingCal obj=new OverloadingCal();
obj.sum(20,20);
//now second int literal will be promoted to long
obj.sum(20,20,20);
}
}
Output :40
60

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).

Example of method overriding


class Vehicle
{
void run(){System.out.println("Vehicle is running");}
}
class Bike extends Vehicle
{
void run(){System.out.println("Bike is running safely");}
public static void main(String args[])
{
Bike obj = new Bike();
obj.run();
}
Output : Bike is running safely

Real example of Java Method Overriding


Consider a scenario, Bank is a class that provides functionality to get rate of
interest. But, rate of interest varies according to banks. For example, SBI, ICICI
and AXIS banks could provide 8%, 7% and 9% rate of interest.

Example of method overriding


class Bank
{
int getRateOfInterest(){return 0;}
}
class SBI extends Bank
{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank
{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank
{
int getRateOfInterest(){return 9;}
}

Example of method overriding [cntd.]


class Test2
{
public static void main(String args[])
{
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
}
}
Output :
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9

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.

Constructor name must be same as its class name.

2. Constructor must have no explicit return type.


.Types of java Constructor:
There are two types of constructor :
1.

Default constructor (no-arg constructor).

2.

Parameterized constructor.

Rule: If there is no constructor in a class, compiler automatically creates


a default constructor.

Purpose of Default Constructor


Default constructor provides the default values to the object like 0, null etc. depending on the type.
//Example of default constructor that displays the default values
class Student
{
int id;
String name;
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
}
}
Output:0 null 0 null

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

Difference between constructor and


method in java
Java Constructor

Java Method

Constructor is used to initialize the state


of an object.

Method is used to expose behaviour of an


object.

Constructor must not have return type.

Method must have return type.

Constructor is invoked implicitly.

Method is invoked explicitly.

The java compiler provides a default


constructor if you don't have any
constructor.

Method is not provided by compiler in any


case.

Constructor name must be same as the


class name.

Method name may or may not be same as


class name.

Java Applet
Java Programs are of two types
1. Applets
2. Application
Applet are java programs which can be Embedded inside web Page

.Difference between Application and Applet:


Application

Applet

They are stand alone program

They are dependent program

They have main method

They do not have main method

They make use of I/O

They cannot make use of I/O

Parameters are passed through


command line

Parameters are passed through html


pages.

Java Applet [Contd]


Hierarchy of Applet:
As displayed in the diagram,
Applet class extends Panel.
Panel class extends Container
which is the subclass of Component.

Lifecycle of Java Applet


Lifecycle of Java Applet:
1.
2.
3.
4.
5.

Applet is initialized.
Applet is started.
Applet is painted.
Applet is stopped.
Applet is destroyed.

.Lifecycle Methods of Applet:


The java.applet.Applet class provides 4 life cycle methods and
java.awt.Component class provides 1 life cycle methods for an applet.

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.

Simple example of Applet by AppletViewer tool:


//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
setBackground(Color.red);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet>
*/
To execute the applet by appletviewer tool, write in command prompt:
d:\>javac First.java
d:\>appletviewer First.java

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.

Displaying Graphics in Applet


java.awt.Graphics class provides many methods for graphics programming.
Commonly used methods of Graphics class:
1. public abstract void drawString(String str, int x, int y):
is
used to draw the specified string.
2. public void drawRect(int x, int y, int width, int height):
draws a
rectangle with the specified width and height.
3. public abstract void fillRect(int x, int y, int width, int height):
is
used to fill rectangle with the default color and specified width and
height.
4. public abstract void drawOval(int x, int y, int width, int height):
is
used to draw oval with the specified width and height.
5. public abstract void fillOval(int x, int y, int width, int height):
is
used to fill oval with the default color and specified width and
height.

Displaying Graphics in Applet


6.

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.

public abstract boolean drawImage(Image img, int x, int y, ImageObserver


observer): is used draw the specified image.

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

You might also like