0% found this document useful (0 votes)
5 views7 pages

Program 4

The document outlines a Java programming assignment focused on inheritance and polymorphism. It includes tasks to demonstrate multiple inheritance using interfaces, implement single and multilevel inheritance, create a Person class with private variables and public methods, and develop a Bank class with overridden interest calculation methods in derived classes. Source code examples for each task are provided to illustrate the concepts.

Uploaded by

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

Program 4

The document outlines a Java programming assignment focused on inheritance and polymorphism. It includes tasks to demonstrate multiple inheritance using interfaces, implement single and multilevel inheritance, create a Person class with private variables and public methods, and develop a Bank class with overridden interest calculation methods in derived classes. Source code examples for each task are provided to illustrate the concepts.

Uploaded by

dawon14123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM: 4

OBJECTIVE: Create Java programs using inheritance and polymorphism

(a) “Java does not support multiple inheritance but we can achieve it by interface”. Write a
program to justify the above statement.

(b) Write a program in java to implement the following types of inheritance:

• Single Inheritance

• Multilevel Inheritance

(c) Write a Java program to create a class called Person with private instance variables name,
age and country. Provide public getter and setter methods to access and modify these variables.

(d) Write a Java program to create Bank class with method name interestCalculation with one
argument (rate variable with double data type). Inherit this class in two classes IDBI and PNB
and override interestCalculation method.

SOURCE CODE:
(a)

interface add {

default int add(int a, int b) {

return a + b;

interface subtract {

default int subtract(int a, int b) {

return a - b;

default void display() {


System.out.println("I am in display method")

class abc implements add, subtract {

public void show() {

System.out.println("I am in show method");

public class MultipleInheritanceFinal {

public static void main(String[] args) {

abc obj = new abc();

obj.display();

obj.show();

System.out.println("Sum is " + obj.add(10, 12));

System.out.println("Difference is " + obj.subtract(10, 38));

(b)

class Animal {

void eat() {

System.out.println("animal is eating");

class Dog extends Animal {

void bark() {

System.out.println("dog is barking");
}

public class SingleInheritanceDemo {

public static void main(String[] args) {

Dog d = new Dog();

d.eat();

d.bark();

class grandparent {

void speak() {

System.out.println("Grandparent is eating");

class parent extends grandparent {

void walk() {

System.out.println("Parent is running");

class child extends parent {

void play() {

System.out.println("Children is playing");

public class MultilevelInheritance {

public static void main(String[] args) {


child mul = new Child();

mul.speak();

mul.walk();

mul.play();

(c) public class Person {

private String name;

private int age;

private String country;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public int getAge() {

return age;

public void setAge(int age) {

this.age = age;

public String getCountry() {

return country;

}
public void setCountry(String country) {

this.country = country;

public static void main(String[] args) {

Person p = new Person();

p.setName("Akshi");

p.setAge(20);

p.setCountry("India");

System.out.println("Name: " + p.getName());

System.out.println("Age: " + p.getAge());

System.out.println("Country: " + p.getCountry());

(d)

class Bank {

void interestCalculation(double rate) {

System.out.println("Bank interest rate: " + rate + "%");

class IDBI extends Bank {

@Override

void interestCalculation(double rate) {

System.out.println("IDBI Bank interest rate: " + (rate + 0.5) + "%");

}
class PNB extends Bank {

@Override

void interestCalculation(double rate) {

System.out.println("PNB Bank interest rate: " + (rate + 1.0) + "%");

public class BankInterestDemo {

public static void main(String[] args) {

Bank bank;

bank = new IDBI();

bank.interestCalculation(5.0);

bank = new PNB();

bank.interestCalculation(5.0);

}
Output:
(a)

(b)

(c)

(d)

You might also like