How To Use Java Class or Object With Examples |
How To Use Java Class or Object With Examples
Class: A Java object Oriented Programming Language tries to map code instructions with real words making the code short and easier to understand.
Class: A class is a blueprint from which objects are created. an object is the instance of a class. class is a collection of data members and member functions.
Data members are the variable used inside the class. Member functions are the function used inside the class. It is also called a User-defined data type.
What is an object-oriented Programming Language.?
Object: A object is an instruction of the class. when a class is defined as a template. Memory is allocated only after object instructions.
Java is an Object-oriented Programming Language is solving a problem by creating an object is one of the popular approaches is called the object-oriented Programming Language.
What is the Dry Concept?
Dry: A Dry Concept is called the Do Not Repeat Yourself.Same Other Topic Post Learn.
How To Model Problem Object-Oriented Programming?
Class: Student
Attribute:id,name,age,Mark's,
Method:get.id(),getname()
Object: A object is having states and behaviors in which state means what does it has and behavior means what does it do.
Syntax of class
class class name
{
Access specifier Data member 1;
Data member 2;
Member function 1;
Member function 2;
}
class class name
{
Access specifier Data member 1;
Data member 2;
Member function 1;
Member function 2;
}
How To Create Java Class
class Rectangle
{
//Data Member
int h;
int w;
int area;
//Member function
void findArea()
{
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
//creating object
Rectangle ob=new Rectangle();
//accessing data member
ob.h=10;
ob.w=15;
//accessing member function
ob.findArea();
}
}
*****Output*****
Area of Rectangle:150
class Rectangle
{
//Data Member
int h;
int w;
int area;
//Member function
void findArea()
{
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
//creating object
Rectangle ob=new Rectangle();
//accessing data member
ob.h=10;
ob.w=15;
//accessing member function
ob.findArea();
}
}
*****Output*****
Area of Rectangle:150
Access Modifiers in java
An Access Specifier where property and method are accessible.Access Modifiers are Four Types.
1. Public
2. Private
3. Protected
4. Default
Contractors in Java: A Java contractor is a special member function of a class that executes.
when we create the object of that class in other words we can say that there is no need to call a constructor. L
Its name is the same as the class name. It has no return type. It may be parameterized or non-parameterized. It is used to initialize the class level variable.
How To Create Constructor in Java?
class Rectangle
{ int h;
int w;
int area;
Rectangle()//Constructor
{
h=10;
w=15;
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle();
}
}
*****Output*****
Area of Rectangle:150
class Rectangle
{
int h;
int w;
int area;
Rectangle()//Constructor
{
h=10;
w=15;
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle();
}
}
*****Output*****
Area of Rectangle:150
Three Types of constructors in Java
1. Default Constructor.
2. Parameterized Constructor.
3. Copy Constructor.
Default Constructor: A Default constructor is a no parameter is called default constructor.
How To Create Default Constructor in Java?
class Rectangle
{
int h;
int w;
int area;
Rectangle()// Default Constructor
{
h=10;
w=15;
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle();
}
}
*****Output*****
Area of Rectangle:150
class Rectangle
{
int h;
int w;
int area;
Rectangle()// Default Constructor
{
h=10;
w=15;
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle();
}
}
*****Output*****
Area of Rectangle:150
Parameterized Constructor: A parameters constructor with parameters is called the parameterized constructor.
How To Create Parameter Constructor in Java?
class Rectangle
{
int area;
Rectangle(int h,int w)// Parameter Constructor
{
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle(10,15);
}
}
*****Output*****
Area of Rectangle:150
class Rectangle
{
int area;
Rectangle(int h,int w)// Parameter Constructor
{
area=h*w;
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle(10,15);
}
}
*****Output*****
Area of Rectangle:150
How To Create Copy Constructor in Java?
class Rectangle
{ int area;
Rectangle(int h,int w)// Copy Constructor
{
area=h*w;
}
void show()
{
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle(10,15);
ob.show();
Rectangle ob1=ob;
ob1.show();
}
}
*****Output*****
Area of Rectangle:150
Area of Rectangle:150
class Rectangle
{
int area;
Rectangle(int h,int w)// Copy Constructor
{
area=h*w;
}
void show()
{
System.out.println("Area of Rectangle:"+area);
}
public static void main(String[] args)
{
Rectangle ob=new Rectangle(10,15);
ob.show();
Rectangle ob1=ob;
ob1.show();
}
}
*****Output*****
Area of Rectangle:150
Area of Rectangle:150