inheritance in java
Inheritance is one of the features of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class. The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class.
Inheritance is a compile-time mechanism. A super-class can have any number of subclasses. But a subclass can have only one superclass. This is because Java does not support multiple inheritances.
Types of Inheritance
*Multilevel Inheritance
*Multiple Inheritances
*Hybrid Inheritance
*Hierarchical Inheritance
Class Shape {
private int length;
private int
breadth;
public int
getBreadth() {
return breadth;
}
public int
getLength() {
return length;
}
public void
setBreadth(int i) {
breadth = i;
}
public void
setLength(int i) {
length = i;
}
// default Constructor
Shape() {
length = 0;
breadth = 0;
System.out.println("Inside default constructor of Shape ");
}
// Parameterized
Constructor
Shape(int len, int
bdth) {
length = len;
breadth = bdth;
System.out.println("Inside
constructor of Shape ");
System.out.println("length : " + length);
System.out.println("breadth : " + breadth);
}
}
// A subclass which extends for shape
class Rectangle extends Shape {
private String
type;
// default
Constructor
Rectangle() {
super();
type = null;
System.out.println("Inside default constructor of rectangle
");
}
// Parameterized
Constructor
Rectangle(String
ty, int len, int bdth) {
super (len,
bdth);
System.out.println("Inside constructor of rectangle ");
System.out.println("length : " + len);
System.out.println("breadth : " + bdth);
System.out.println("type : " + type);
}
public String
getType() {
return type;
}
public void
setType(String string) {
type = string;
}
}
// A subclass which extends for rectangle
class ColoredRectangle extends Rectangle {
private String
color;
/* default
Constructor*/
ColoredRectangle()
{
super();
color = null;
System.out.println("Inside default constructor of
coloredRectangle");
}
// Parameterized
Constructor
ColoredRectangle(String c, String ty, int len, int bdth) {
super (ty, len,
bdth);
System.out.println("Inside
constructor of coloredRectangle ");
System.out.println("length : " + len);
System.out.println("breadth : " + bdth);
System.out.println("type : " + ty);
}
public String
getColor() {
return color;
}
public void
setColor(String string) {
color =
string;
}
}
public class Test {
public static void
main(String args[]) {
ColoredRectangle CR = new ColoredRectangle();
ColoredRectangle CR2 = new ColoredRectangle("Red","Big",
5, 2 );
}
}
The output is:
Inside default constructor of Shape
Inside default constructor of rectangle
Inside default constructor of coloredRectangle
Inside constructor of Shape
Length: 5
Breadth: 2
Inside constructor of rectangle
Length: 5
Breadth: 2
Type: null
Inside constructor of coloredRectangle
Length: 5
Breadth: 2
Type: Big
Comments
Post a Comment