Significance of Classes and Object

Java is a pure object oriented language, so all the functionality revolves around classes and objects. Without classes, there can be no objects and without objects, no computation takes place. Thus, classes form the basis of all computation in Java. When a class is declared, no memory space is allotted to it. This is because a class is simply a blueprint containing objects and methods. Memory space is allocated when objects of a class type are created.

The declarations of member variables take place in the body of the class. Member variables can be of two types: class/static variable and instance variable. Class variables are the data members that are declared once for a class. All objects of the class type share these data members as their single copy is available in memory. Class variables are made by declaring variable with static keyword. Instance variable is a data member that is created for every object of the class. All objects of a class have their own copies of instance variable. The declarations of member methods take place in the body of the class after member variables. Member methods can be of two types: class method and instance method. Class methods operate only on class variables and do not have access to instance variable. The static keyword is used in declaration. Instance method operates on instance variables of current object but also have access to the class variable. Java provides four types of access specifiers: public, private, default and protected.

To Access the full content, Please Purchase

  • Q1

    List the parts of Java programs.

    Marks:1
    Answer:

    All Java programs consist of:

    (1) Objects (Data and behaviour)

    (2) Calling methods

    View Answer
  • Q2

    Define class.

    Marks:1
    Answer:

    A class is often described as a blueprint for an object.

    View Answer
  • Q3

    Name few characteristics of method?

    Marks:2
    Answer:

    The characteristics of a method are:

    1. Methods break down large problems into smaller parts.

    2. Same method can be called many times.

    3. Methods can return a value.

    4. Methods take parameters.

    View Answer
  • Q4

    What does the Java object contain?

    Marks:2
    Answer:

    Java class objects exhibit the attributes and behaviors defined by its class. Attributes represent the data that is unique to an instance of a class, while behaviors are methods that operate on the data to perform useful tasks.

    Explanation:
    null
    View Answer
  • Q5

    Write a program to calculate average of five inputs?

    Marks:4
    Answer:

    class Method_Average
    {

    float calc_avg(int num1, int num2, int num3, int num4, int num5)

    {

    float local_avg;

    local_avg = (float)(num1+num2+num3+num4+num5)/5;
    return local_avg;

    }

    }

    class Average
    {

    public static void main(String args[ ])
    {

    Method_Average AVG = new Method_Average();

    int num1=20;

    int num2=40;

    int num3=50;

    int num4=60;

    int num5=80;

    float avg;

    avg=AVG.calc_avg(num1, num2, num3, num4, num5);

    System.out.println(“The average is” + avg);

    }

    }

    View Answer