0% found this document useful (0 votes)
3 views5 pages

Java Inheritance and Overloading Examples

The document contains Java programs demonstrating Single Inheritance, Multilevel Inheritance, and Constructor Overloading. It includes examples of class structures and methods for each inheritance type, as well as a comparison between Constructor Overloading and Method Overloading. Additionally, it provides a Rectangle class with both default and parameterized constructors to illustrate constructor overloading.

Uploaded by

virajupalanchi6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java Inheritance and Overloading Examples

The document contains Java programs demonstrating Single Inheritance, Multilevel Inheritance, and Constructor Overloading. It includes examples of class structures and methods for each inheritance type, as well as a comparison between Constructor Overloading and Method Overloading. Additionally, it provides a Rectangle class with both default and parameterized constructors to illustrate constructor overloading.

Uploaded by

virajupalanchi6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1: Write a Java program to demonstrate Single Inheritance

class Animal {

void eat() {

[Link]("Animals eat food");

class Dog extends Animal {

void bark() {

[Link]("Dog barks");

public class SingleInheritance {

public static void main(String[] args) {

Dog d = new Dog();

[Link](); // from parent

[Link](); // from child

Q2: Write a Java program to demonstrate Multilevel Inheritance

class Animal {

void eat() {

[Link]("Animals eat");
}

class Mammal extends Animal {

void walk() {

[Link]("Mammals walk");

class Dog extends Mammal {

void bark() {

[Link]("Dog barks");

public class MultilevelInheritance {

public static void main(String[] args) {

Dog d = new Dog();

[Link]();

[Link]();

[Link]();

Q3: Explain with examples how Constructor Overloading differs from Method Overloading
• Constructor Overloading: Multiple constructors in the same class with different parameter lists.

• Method Overloading: Multiple methods in the same class with the same name but different
parameters.

class Example {

int a, b;

// Constructor Overloading

Example() {

a = 0; b = 0;

Example(int x, int y) {

a = x; b = y;

// Method Overloading

void show() {

[Link]("a=" + a + ", b=" + b);

void show(int x) {

[Link]("Value: " + x);

public class OverloadingDemo {

public static void main(String[] args) {

Example e1 = new Example();

Example e2 = new Example(5, 10);


[Link]();

[Link]();

[Link](20); // method overloading

Q4: Create a Rectangle class with Constructor Overloading

class Rectangle {

int length, breadth;

// Default constructor

Rectangle() {

length = 5;

breadth = 3;

// Parameterized constructor

Rectangle(int l, int b) {

length = l;

breadth = b;

int area() {
return length * breadth;

public class RectangleDemo {

public static void main(String[] args) {

Rectangle r1 = new Rectangle(); // default

Rectangle r2 = new Rectangle(10, 4); // parameterized

[Link]("Area (default): " + [Link]());

[Link]("Area (parameterized): " + [Link]());

You might also like