Class 11 | Class and Object | Java Programming Notes

UNIT 3
Class 11 Java Programming

Class and Object

Original Scanned PDF – View Notes

01

Constructor

A constructor is a special type of method that enables an object to initialized itself when it is created.

  • It has same name as the class itself.
  • They do not specify a return type, not even void because they return instance of the class itself.

Program to Illustrate Constructor in Java

class Rectangle
{
    int length;   // instance variables
    int breadth;

    Rectangle(int x, int y)   // constructor definition
    {
        length = x;
        breadth = y;
    }

    int RectArea()
    {
        return (length * breadth);
    }
}

class RectangleArea
{
    public static void main(String []args)
    {
        Rectangle rect = new Rectangle(10,5);   // constructor calling
        int area = rect.RectArea();
        System.out.println("The area of Rectangle = " + area);
    }
}
02

Types of Constructor

1. Default Constructor

The constructor automatically initialize the object variable with some default values at the time of object instantiation. It does not take any parametric values.

2. Parameterized Constructor

The constructor is invoked by passing certain argument at the time of object instantiation. It takes parametric values.

Program Illustrating Default and Parameterized Constructor

Source transcription note: The program below follows the scanned note as written. Apparent syntax choices in the scan are preserved rather than silently rewritten.
class Perimeter
{
    int length;
    int width;

    Perimeter()   // default constructor
    {
        length = 0;
        width = 0;
    }

    Perimeter(int x, int y)   // parameterized constructor
    {
        length = x;
        width = y;
    }

    void CalPerimeter()
    {
        int peri;
        peri = 2 (length + width);
        System.out.println("The perimeter of Rectangle = " + peri);
    }
}

class ConstructorTest
{
    public static void main(String args [])
    {
        Perimeter obj1 = new Perimeter();       // default constructor calling
        Perimeter obj2 = new Perimeter(10,5);   // parameterized constructor calling

        obj1.CalPerimeter();
        obj2.CalPerimeter();
    }
}
03

Inheritance

Inheritance is mechanism of deriving new class from an existing class.

  • The existing (old) class is known as base class or super class or parent class.
  • The new class is known as derived class or sub class or child class.
  • Inheritance allows to use the properties and behaviour of one class (parent) in another class (child).
  • A class whose properties are inherited is known as parent class.
  • A class that inherits the properties of the parent class is known as child class.
04

Defining Sub Class

  • A class that is derived from another class is called a sub class.
  • Sub class is created using extends keyword.
  • General rule for defining sub class:
class SubClass extends SuperClass
{
    Variable declaration;
    method declaration;
}
05

Type of Inheritance

1. Single Inheritance

  • In single inheritance, a sub class is derived from only one super class.
  • It inherits the properties and behaviour of a single parent class.
Single inheritance showing parent class A and child class B A Parent class B Child class
fig:- Single inheritance

2. Multilevel Inheritance

A class is derived from a class which is also derived from another class is called multilevel inheritance.

In simple word, a class that has more than one parent class is called Multilevel inheritance.

Multilevel inheritance showing A parent class, B child class and C grand child class A Parent class B Child class C Grand child class
fig:- Multilevel Inheritance

3. Hierarchical Inheritance

If a no of class are derived from a single base class it is called Hierarchical inheritance.

Hierarchical inheritance showing parent class A with child classes B, C and D A Parent class B C D Child class
fig:- Hierarchical Inheritance

4. Multiple Inheritance

A class is derived from multiple parent classes.

Multiple inheritance showing parent classes A and B leading to child class C A B Parent class C Child class
fig:- Multiple Inheritance

5. Hybrid Inheritance

It is the combination of two or more type of inheritance.

Hybrid inheritance diagram showing A parent class, B below it and child classes C, D and E A Parent class B C D E Child class
fig:- Hybrid Inheritance
06

Program to Illustrate Single Inheritance

Source transcription note: The scan is reproduced closely below, including the variable name used in the final println statement and the written output.
class Room
{
    int length;
    int breadth;

    Room(int x, int y)
    {
        length = x;
        breadth = y;
    }

    int area()
    {
        return (length * breadth);
    }
}

class BedRoom extends Room
{
    int height;

    BedRoom(int x, int y, int z)
    {
        super(x,y);   // passes values to super class
        height = z;
    }

    int Volume()
    {
        return (length * breadth * height);
    }
}

class InheritanceTest
{
    public static void main(String []args)
    {
        BedRoom obj1 = new BedRoom(20,20,30);
        int area1 = obj1.area();
        int volume1 = obj1.Volume();
        System.out.println("The area of Room :" + area1);
        System.out.println("The volume of Room :" + volume2);
    }
}
Output:
The area of Room = 400
The Volume of Room = 8000

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play