Objects
An entity that has state and behavior is
known as an object e.g., chair, bike, marker, pen,
table, car, etc.
Three characteristics:-
State:- value of an object
Behavior:- functionality of an object
Identity:- Unique ID
For Example, Pen is an object. Its name is
Reynolds; color is white, known as its state. It is
used to write, so writing is its behavior
Class
A Class in java is a blueprint which
includes all the data.
It describe the state and behavior of an
object
Rules to Create a Class
A Java class must have the class keyword
followed by the class name, and class must be
followed by a legal identifier.
There should not be any spaces or special
characters used in a class name except the dollar
symbol($) and underscore(_).
A Java class can only have public or default
access specifier.
The class’s members must be always
declared within a set of curly braces {}.
Class containing the main() method is known
as the Main class as it will act as the entry point
to your program
Program
class Lamp
{
boolean isOn;
void turnOn()
{
// initialize variable with value true
isOn = true;
System.out.println("Light on? " + isOn);
}
void turnOff()
{
// initialize variable with value false
isOn = false;
System.out.println("Light on? " + isOn);
}
}
class Mainfile
{
public static void main(String[] args)
{
// create objects l1 and l2
Lamp l1 = new Lamp();
Lamp l2 = new Lamp();
// call methods turnOn() and turnOff()
l1.turnOn();
l2.turnOff();
}
Output
Constructor
A constructor is a block of codes similar
to the method
A constructor is a special member
method which can be called automatically
when we created an object.
Rules for constructor
Constructor name must be the same as its class name
A Constructor must have no explicit return type
A Java constructor cannot be abstract, static, final,
and synchronized
There cannot be any return type in constructor
definition.
There cannot be any return statement in constructor.
Constructors can be overloaded by different
arguments.
Example for constructor
class Car
{
String name ;
String model;
Car( )
//Constructor
{
name =""; model="";
}
}
Default Constructor
A constructor which takes no arguments
is known as default constructor
Alternatively, it is also referred as no-
arg constructor as it don’t takes any
arguments.
Program
class AddDemo1
{
AddDemo1()
{
int a=10;
int b=5;
int c;
c=a+b;
System.out.println("*****Default Constructor*****");
System.out.println("Total of 10 + 5 = "+c);
}
public static void main(String args[])
{
AddDemo1 obj=new AddDemo1();
}
}
Output
Parameterized constructor
A constructor can have parameters
Program
class Employee
{
int Id;
String Name;
int Age;
long Salary;
Employee(int i,String nm,int a,long s) // Parameterize
Constructor
{
Id = i;
Name = nm;
Age = a;
Salary = s;
}
Continue..
void PutData()
{
System.out.print("\n\tEmployee Id : "+Id);
System.out.print("\n\tEmployee Name : "+Name);
System.out.print("\n\tEmployee Age : "+Age);
System.out.print("\n\tEmployee Salary : "+Salary);
}
public static void main(String args[])
{
Employee E = new Employee(2,"Sumit",31,35000);
// Creating object and passing values to constructor.
E.PutData(); // Calling PutData()
}
}
Output
public class Rectangle
{
int length;
int breadth;
String color;
//constructor 1
Rectangle( int l , int b){
length = l;
breadth = b;
color = "Green";
}
//constructor 2
Rectangle(int l, int b, String c){
length = l;
breadth = b;
color = c;
}
void display(){
System.out.println("Length-" + length + "Breadth-" + breadth+ "Color" + color);
}
public static void main(String args[]){
Rectangle obj1 = new Rectangle(2,4);
Rectangle obj2 = new Rectangle(2,4,"Green");
obj1.display();
obj2.display();
}
}