Class and Object
Original Scanned PDF – View Notes
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);
}
}
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
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();
}
}
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.
Defining Sub Class
- A class that is derived from another class is called a sub class.
- Sub class is created using
extendskeyword. - General rule for defining sub class:
class SubClass extends SuperClass
{
Variable declaration;
method declaration;
}
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.
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.
3. Hierarchical Inheritance
If a no of class are derived from a single base class it is called Hierarchical inheritance.
4. Multiple Inheritance
A class is derived from multiple parent classes.
5. Hybrid Inheritance
It is the combination of two or more type of inheritance.
Program to Illustrate Single Inheritance
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);
}
}
The area of Room = 400 The Volume of Room = 8000
Discussion
Share a helpful question, idea, or explanation with other students.