Class and Object in Java

Introductions of Class and Object:

Class:
  • Class is used to hold data members and member function i.e. variables and functions.
  • It can be easily accessed anywhere and used to reduce the reusability of the code.
For example,
  • Let’s take student,
  • Student properties are all called variables and student performance or student-related calculations(i.e. student result based on total and it may be pass or fail) is called as a function.
Where,
The student is a class.
Student properties (variables) : Roll_num,name,age,ect.
Student behaviours(function)  : total,average,result,fees details ect.
Note:
  • If one or more variables declared within the class, called data members.
  • If one or more functions declared within the class, called member function.
Object:
  • An object is used to access the members of the class.
  • It simply said as the short name of the class.
Syntax:
  1. <import packages>
    Access_specifiers class class_name //class declaration
    {
              Variable_declaration;
              Method declaration or function_declaration()
              {
              //Statements
              }
    public static void main(String[] args)
    {
    //object declaration
    class_name obj_name new=class_name();
    //accessing member function using object
    obj_name.function_name();
    }
  2. }
Note:
The class name and object name is a user-defined word.
Access specifiers:
  • Java access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
Types of access specifiers:
  • Private – It is used to access the data member and member function within the classes.
  • Public – It is used to access the data member and member function out of the class. That means any class can be accessible.
  • Protected – It members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

Example:
  1.  package oops_examples;

    import java.util.Scanner;

    public class Class_and_obj_Example_program {

        int sno, m1, m2, m3, m4, m5, tot;

        String name;

        float avg;

        void get()//Method for value get from user

        {

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter student number : ");

            sno = sc.nextInt();

            System.out.println("Enter student name : ");

            name = sc.next();

            System.out.println("Enter Five subject mark : ");

            m1 = sc.nextInt();

            m2 = sc.nextInt();

            m3 = sc.nextInt();

            m4 = sc.nextInt();

            m5 = sc.nextInt();

        }

        void result()//Method for calculating total and average

        {

            tot = m1 + m2 + m3 + m4 + m5;

            avg = tot / 5;

        }

        void display()//Method for displaying full details

        {

            System.out.println("Student details............");

            System.out.println("Student id is : " + sno);

            System.out.println("Student name is : " + name);

            System.out.println("Five subject marks : \t" + m1 + "\t" + m2 + "\t" + m3 + "\t" + m4 + "\t" + m5);

            System.out.println("Total mark for five subject :" + tot);

            System.out.println("Average mark for five subject :" + avg);

        }

        public static void main(String[] args) {

            Class_and_obj_Example_program c = new Class_and_obj_Example_program();//object declaration

    //accessing member function using object

            c.get();

            c.result();

            c.display();

        }

    }

Output:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me