Chapter -2 What is Class and Objects in Java
Class and Objects in Java
Click :---Java Object in Hindi
Click:---Java Class in Hindi
Introduction to java class and Object
Class and Objects important parts of oops.
Look at the following Example represent through diagram.
Class | Objects |
Fruits | Apple |
Banana | |
Mango | |
Papaya |
Same As Another Example :-
Class | Objects |
Students | Shailesh |
Annu | |
Smriti | |
Kajal |
Class
Class :- It is an user defined data type, it is a template that defined properties and behaviours of objects and it is a real-life instance with blue print of any objects.
It is collections of similar type of objects, for Example:- Birds, Students, Fruits, Cars, Peoples, Animals etc..
Summary :-
- It is a collection of data members and member functions.
- Data members are the variable used inside class.
- Member functions are the function used inside class.
- It is also called Userdefined data type.
Object
Object :- Object is a real world entity.
In another world:- Objects is an entity that has state and behaviours. Here Now:- State means:- Data and Behaviors means functionality.
Main Points :- Well Objects is an instance of a class, All the members of the class can be access through objects.
For example :- Parrot, Shailesh, Mango, Maruti etc.
- Object is having states and behaviors in which state means what does it has and behavior means what does it do. for example a pen has
- States:ink,nib,needle,cap etc
- Behaviors:Writing.
Impotent point to remember while making class program
⇒Declare variable with appropriate data type as per your requirement
for example to find the area of recatngle three variable are sufficient(height, width and area)
⇒Declare function as per your requirement
for example to find the area of recatngle single function findArea() is sufficient.
⇒Create instance(object) of class to access the data member and member function of a class.
classname objectname;
Example:- Rectangle rec1;
Here Rectangle is the name of class and rec1 is the name of object.
⇒Accessing data member and member fucntion
Data member and member fucntion of a class can be accessed using Dot(.) operator.
⇒Accessing data member
rec1.height;
⇒Accessing member function
rec1.findArea();
Example :-
// Example 1: Find area of Rectangle but passing value in main fuctions
class Rectangle{
int height;
int width;
int area;
void findarea(){
area = height* width;
System.out.print("Area of rectangle:"+area);
}
public static void main(String args[]){
Rectangle obj =new Rectangle(); // creatig Object
obj.height=15; // Passing value
obj.width=10;
obj.findarea(); // Access member function
}
}
Example 2:-
// Example 2: Find area of Rectangle using double member function
class Rect{
// Passing Data member
int h;
int w;
int area;
// passing member function
void getvalue(){
h=19;
w=15;
}
// member function
void findarea(){
area = h*w;
System.out.print("Area of Rectangle :-"+area);
}
public static void main(String args[]){
Rect ra= new Rect(); // Creating obj
ra.getvalue();
ra.findarea();
}
}
No comments
Post a Comment