Skip to main content

Chapter -2 What is Class and Objects in Java

 Class and Objects in Java 

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();
    }
}



I hope this point is helpful 


Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...