KnowledgeBoat Logo
OPEN IN APP

Chapter 4

Objects and Classes

Class 12 - APC Understanding ISC Computer Science with BlueJ



Fill in the blanks with an appropriate word/words

Question 1

In an Object Oriented Programming, the stress is laid on data.

Question 2

An Object is an unique entity that contains characteristics and behaviours.

Question 3

You can create a number of objects from the same class.

Question 4

The process of combining data and methods that enables them to be used as a single unit is called Encapsulation.

Question 5

The process by which an object of a class acquires the properties from the object of another class is called Inheritance.

Question 6

In an Object Oriented Programming, Class is a blue print that describes attributes and behaviours.

Question 7

Creating objects of a class is also called instantiating a class.

Question 8

The process of restricting the free flow of data from the outside world, is known as Encapsulation.

State True or False

Question 1

Object is the fundamental concept of Object Oriented Programming.
True

Question 2

Objects can communicate with each other through functions.
True

Question 3

Encapsulated class can have the feature of re-usability
False

Question 4

Inter-class sharing of data can be enabled through inheritance.
True

Question 5

Encapsulation promotes data sharing among the objects of different classes.
False

Question 6

Class is also called an instance of an Object.
False

Question 7

The class variables are also called as static variables.
True

Write short answers

Question 1

What is meant by Object Oriented Programming? Name all the principles of Object Oriented Programming.

Object Oriented Programming is a modular approach of programming that gives stress to data over functions. It decomposes the program into a number of entities called Objects. Each Object contains data and functions to operate on that data. Objects cannot access each other's data, they only communicate through functions.
The principles of Object Oriented Programming are:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Question 2

What are the features of Object Oriented Programming? Explain.

Features of Object Oriented Programming are:

  1. It gives stress on the data items rather than functions.
  2. It makes the complete program/problem simpler by dividing into number of objects.
  3. The objects share information through functions.
  4. Data remain secure from outer interference.

Question 3

Explain the terms Class and Object with the help of real world examples.

An object is an entity having a specific identity, specific characteristics and specific behavior. A class is a blue print that represents a set of objects that share common characteristics and behaviour. Take the example of a house. An architect will have the blueprints for a house. Those blueprints will be plans that explain exactly what properties the house will have and how they are all laid out. However, it is just the blueprint, we can't live in it. Builders will look at the blueprints and use those blueprints to make a physical house. They can use the same blueprint to make as many houses as they want. Each house will have the same layout and properties. Each house can accommodate its own families. So, in this example, the blueprint is the class, the house is the object and the people living in the house are data stored in the object's properties.

Question 4

What is meant by 'Abstract Class'?

A class declared using the abstract keyword is called an Abstract Class. We cannot instantiate objects of Abstract Class. An Abstract Class can contain abstract methods (methods without body) and concrete methods (normal methods with body). The sub-class inheriting the abstract class needs to provide the definition of abstract methods of the super-class otherwise sub-class also needs to be abstract.

Question 5

What is the need of an Abstract Class? Explain.

Let's say I have a class called Shape that I want to use as the base class for different shapes like Circle, Rectangle, etc. I have a method in Shape class named computeArea that calculates the area of its corresponding shape. I cannot give any definition of computeArea method in Shape class as each shape computes its area in a different way but I want to enforce that any class deriving from Shape class implements computeArea method to calculate its area. To do this, I will make Shape class as abstract class and computeArea method as abstract method. Here is the complete program to show this usage of abstract Shape class.

/*
 * Abstract Shape Class Definition
 */
public abstract class Shape
{
    public abstract double computeArea();
}

/*
 * Circle derives from Shape and 
 * implements computeArea method
 */
public class Circle extends Shape
{ 
    private double radius;
    
    public Circle(double r) {
        radius = r;
    }
    
    public double computeArea() {
        double area = 3.14159 * radius * radius;
        return area;
    }
}

/*
 * Rectangle derives from Shape and 
 * implements computeArea method
 */
public class Rectangle extends Shape
{
    private double length;
    private double width;
    
    public Rectangle(double l, double w) {
        length = l;
        width = w;
    }
    
    public double computeArea() {
        double area = length * width;
        return area;
    }
}

/*
 * Test Class with main method to
 * instantiate objects of Rectangle
 * and Circle and display their area
 */
public class TestShape
{
    public static void printArea(Shape s) {
        double area = s.computeArea();
        System.out.println("Area of shape = " + area);
    }
    
    public static void main(String args[]) {
        Rectangle r = new Rectangle(6.0, 3.0);
        Circle c = new Circle(5.0);
        printArea(r);
        printArea(c);
    }
}

Question 6

Define the following terms with real world examples:

(a) Inheritance

Inheritance enables new classes to receive or inherit the properties and methods of existing classes. Below is an example of inheritance showing the hierarchy of vehicles:

Vehicle class hierarchy as an example of inheritance to explain ISC Computer Science

(b) Polymorphism

In object-oriented programming, Polymorphism provides the means to perform a single action in multiple different ways. Taking the real world example of animals, if we ask different animals to speak, they respond in their own way. Dog barks, duck quacks, cat says meow and so on. So the same action of speaking is performed in different ways by different animals.

(c) Encapsulation

Wrapping of data and functions that operate on that data into a single unit is called Encapsulation. A medical capsule is a real world example of Encapsulation as it keeps the drug safe inside it.

(d) Abstraction

Abstraction is the act of representing the essential features without including the background details. An electrical switchboard is one of the real world examples of Abstraction. A switchboard provides us with a simple way of switching ON or OFF electrical equipments while hiding all the details of the electrical circuitry, current, etc.

Question 7

Television set is a real world object. Explain.

A real world object is an entity having a specific identity, specific characteristics and specific behavior. A television can be considered as an object that posses the following characteristics and behaviours:

CharacteristicsBehaviours
It is rectangular in shape.It is used to view news.
It has a flat screen.It is also used to watch games.
It contains a number of channels.It is used to view different entertainment channels.

Moreover a television set is owned by someone giving it an identity. Hence, television set is a real world object.

Question 8

Give two differences between Polymorphism and Inheritance.

PolymorphismInheritance
Polymorphism provides the means to perform a single action in multiple different ways.Inheritance enables new classes to receive or inherit the properties and methods of existing classes.
Polymorphism is implemented through Method Overloading and Method Overriding in Java.A class can inherit from another class using the 'extends' keyword in Java.

Question 9

An object is referred to as an instance of a class. Explain.

A class can create objects of itself with different characteristics and common behaviour. So, we can say that an Object represents a specific state of the class. For these reasons, an Object is called an Instance of a Class.

Question 10

Class and Object are inter-related. Justify this statement.

A Class is used to create various Objects that have different characteristics and common behaviours. Each object follows all the features defined within a class. Hence, class is also referred to as a blue print or prototype of an object.

Question 11a

Design a class 'Add_Dist' with the following specifications:
class Add_Dist
Data members/Instance variables
int km, mts, cm
Member methods:
void get_dist( ) : to accept a distance in km, mts and cm.
void show_dist( ) : to display the distance in km, mts and cm.
Write a main function to create an object of class 'Add_Dist' and call the member methods.

import java.util.Scanner;

class Add_Dist {
    int km;
    int mts;
    int cm;

    void get_dist() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a distance in km, mts and cm");
        System.out.print("Enter km: ");
        km = in.nextInt();
        System.out.print("Enter mts: ");
        mts = in.nextInt();
        System.out.print("Enter cm: ");
        cm = in.nextInt();
    }

    void show_dist() {
        System.out.println("Distance in km = " + km);
        System.out.println("Distance in mts = " + mts);
        System.out.println("Distance in cm = " + cm);
    }

    public static void main(String args[]) {
        Add_Dist obj = new Add_Dist();
        obj.get_dist();
        obj.show_dist();
    }
}

Question 11b

Design a class 'Cuboid' with the following specifications:
class Cuboid
Data members/Instance variables
int len, br, ht, vol;
Member methods:
void input( ) : to accept length, breadth and height of a cuboid.
void calculate( ) : to calculate volume of a cuboid.
void display( ) : to print volume of cuboid.
Write a main function to create an object of class 'Volume' and call the member methods.

import java.util.Scanner;

class Cuboid {
    int len;
    int br;
    int ht;
    int vol;

    void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter length of Cuboid: ");
        len = in.nextInt();
        System.out.print("Enter breadth of Cuboid: ");
        br = in.nextInt();
        System.out.print("Enter height of Cuboid: ");
        ht = in.nextInt(); 
    }

    void calculate() {
        vol = len * br * ht;
    }

    void display() {
        System.out.print("Volume of Cuboid = " + vol);
    }

    public static void main(String args[]) {
        Cuboid obj = new Cuboid();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

Question 11c

Write a class template 'Calculator' with the following specifications:
Class : Display
Data members/Instance variables
int a,b,c;
Member Methods:

NamePurpose
void Accept()to accept the values of a and b.
void Max()to find greater of the two number 'a' and 'b' and store the result in c. Display the result.
void Min()to find smaller of the two number 'a' and 'b' and store the result in c. Display the result.
void Diff()to store the difference between 'a' and 'b' and store the result in c. Display the result.

Write a main class to create an object of class 'Display' and call the member methods.

import java.util.Scanner;

class Display {
    int a;
    int b;
    int c;

    void Accept() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter value of a: ");
        a = in.nextInt();
        System.out.print("Enter value of b: ");
        b = in.nextInt();
    }

    void Max() {
        c = a > b ? a : b;
        System.out.println("Greater Number = " + c);
    }

    void Min() {
        c = a < b ? a : b;
        System.out.println("Smaller Number = " + c);
    }

    void Diff() {
        c = a - b;
        System.out.println("Difference = " + c);
    }

    public static void main(String args[]) {
        Display obj = new Display();
        obj.Accept();
        obj.Max();
        obj.Min();
        obj.Diff();
    }
}
PrevNext