0% found this document useful (0 votes)
21 views

polymorphism

Uploaded by

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

polymorphism

Uploaded by

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

Experiment 13

OBJECT : To demonstrate the concept of Polymorphism and implementing method


overriding.
Exercise 1:

class BaseClass {
void Display() {
System.out.println("\nThis is Display() method of BaseClass");
}

void Show() {
System.out.println("\nThis is Show() method of BaseClass");
}
}

class DerivedClass extends BaseClass {


// Overriding method - new working of base class display method
@Override
void Display() {
System.out.println("\nThis is Display() method of DerivedClass");
}
}

public class Main {


public static void main(String[] args) {
DerivedClass dr = new DerivedClass();
BaseClass bs = dr;
bs.Display();
dr.Show();
}
}
Output:

Exercise 2: Dynamic Method Look-Up


OBJECT ORIENTED PROGRAMMING

// A Java program to illustrate Dynamic Method


// Dispatch using hierarchical inheritance
class A
{
void m1()
{
System.out.println("Inside A's m1 method");
}
}

class B extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside B's m1 method");
}
}

class C extends A
{
// overriding m1()
void m1()
{
System.out.println("Inside C's m1 method");
}
}

// Driver class
class Dispatch
{
public static void main(String args[])
{
// object of type A
A a = new A();

// object of type B
B b = new B();

// object of type C
C c = new C();

// obtain a reference of type A


A ref;

// ref refers to an A object

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

ref = a;

// calling A's version of m1()


ref.m1();

// now ref refers to a B object


ref = b;

// calling B's version of m1()


ref.m1();

// now ref refers to a C object


ref = c;

// calling C's version of m1()


ref.m1();
}
}

Output:

Exercise 3: Early Binding Vs Late Binding

class Shape {
protected int width, height;

public Shape(int a, int b) {


width = a;
height = b;
}

// Member function in the base class


int area() {
System.out.println("Parent class area: " + (width * height));
return width * height;

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

}
}

// Derived class Rectangle


class Rectangle extends Shape {
public Rectangle(int a, int b) {
super(a, b);
}

// Override area function in Rectangle class


int area() {
System.out.println("Rectangle class area: " + (width * height));
return width * height;
}
}

// Derived class Triangle


class Triangle extends Shape {
public Triangle(int a, int b) {
super(a, b);
}

// Override area function in Triangle class


int area() {
System.out.println("Triangle class area: " + ((width * height) /
2));
return (width * height) / 2;
}
}

public class Main {


public static void main(String[] args) {
// Create objects of the derived classes
Rectangle rec = new Rectangle(10, 7);
Triangle tri = new Triangle(10, 5);

// Classify Category
Shape shape = rec;
shape.area(); // Calls Rectangle's area() function

shape = tri;
shape.area(); // Calls Triangle's area() function
}
}

Output: (Also Analyze the Code and Explain in which category it falls Late or Early Binding)

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

Task 1: Create a Java program that demonstrates function overriding. Define a base class called
"Animal" with a virtual function makeSound(). Create two derived classes from "Animal"
called "Dog" and "Cat".In each derived class, override the makeSound() function to provide a
different sound. For example, the Dog class may make a barking sound, and the Cat class may
make a meowing sound. In the main function, create objects of both the "Dog" and "Cat"
classes and call the makeSound() function for each object.
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog barks");
}
}

class Cat extends Animal {


@Override
public void makeSound() {
System.out.println("Cat meows");
}
}

public class Task1 {


public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound();
Cat cat = new Cat();
cat.makeSound();
}
}

Output:

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

Task 2: Write a Java program that demonstrates dynamic method dispatch using hierarchical
inheritance in a simple banking system.

a. Create a superclass Account with properties accountNumber and balance, and methods
deposit(double amount), withdraw(double amount), and displayAccountDetails().
b. The deposit method should add the specified amount to the balance.
c. The withdraw method should deduct the specified amount from the balance if sufficient
funds are available.
d. The displayAccountDetails method should display the account type, account number, and
current balance.
e. Implement two subclasses: SavingsAccount and CheckingAccount, which inherit from
Account.
f. SavingsAccount should have an additional property interestRate and override the
withdraw method to apply a penalty fee if the withdrawal amount exceeds the balance.
g. CheckingAccount should have an additional property overdraftLimit and override the
withdraw method to allow overdrafts up to a certain limit.
h. In the Dispatch class (the driver class): Create objects of type Account, SavingsAccount,
and CheckingAccount. Demonstrate dynamic method dispatch by calling the
displayAccountDetails() method through a reference of type Account.
class Account {
protected int accountNumber;
protected double balance;
public void deposit(double amount){
balance=balance + amount;
}
public Account(int accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public void withdraw(double amount){

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

if (balance>=amount){
balance=balance-amount;
System.out.println("Withdraw successful!!");
}
else{
System.out.println("Insufficient funds");
}
}

public void displayAccountDetails(){


System.out.println("Account Type: General account");
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: " + balance);
}

class SavingsAccount extends Account{


private double interestRate;

public SavingsAccount(int accountNumber, double balance, double interestRate){


super(accountNumber, balance);
this.interestRate=interestRate;
}
@Override
public void withdraw(double amount) {
if (balance >= amount) {
balance=balance- amount;
System.out.println("Withdraw successful");
} else {
System.out.println("Insufficient funds");
}
}

@Override
public void displayAccountDetails() {
System.out.println("Account Type: Savings Account");
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: " + balance);
System.out.println("Interest Rate: " + interestRate);
}
}
class CheckingAccount extends Account {
private double overdraftLimit;

public CheckingAccount(int accountNumber, double balance, double overdraftLimit) {


super(accountNumber, balance);
this.overdraftLimit = overdraftLimit;
}

@Override
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance = balance + amount;
System.out.println("Withdraw successful");
} else {

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

System.out.println("Exceeds overdraft limit");


}
}

@Override
public void displayAccountDetails() {
System.out.println("Account Type: Checking Account");
System.out.println("Account Number: " + accountNumber);
System.out.println("Balance: " + balance);
System.out.println("Overdraft Limit: " + overdraftLimit);
}
}

public class Dispatch1 {


public static void main(String[] args) {
Account A = new Account(4560, 50000);
SavingsAccount SA = new SavingsAccount(2071, 10000, 0.05);
CheckingAccount CA = new CheckingAccount(3901, 7000, 2000);

A.withdraw(4000);
A.deposit(2000);
A.displayAccountDetails();
System.out.println();
SA.withdraw(4000);
SA.deposit(2000);
SA.displayAccountDetails();
System.out.println();
CA.withdraw(4000);
CA.deposit(2000);
CA.displayAccountDetails();
}
}

Output:

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

Task 3: Design a superclass Device with a non-static method turnOn() that prints "Device
turned on" and a static method getManufacturer() that returns "Unknown Manufacturer".
Implement a subclass Television that extends Device. Override the turnOn() and
getManufacturer() method in Television. In the Main class, demonstrate the usage of both
methods by creating instances of Device and Television and invoking the appropriate methods.
Discuss the concept of method hiding and observe its effect in this scenario
class Device{
public void turnOn(){
System.out.println("Device turned on");
}
public static String getManufacturer(){
return "Unknown Manufacturer";
}
}
class Television extends Device{
@Override
public void turnOn(){
System.out.println("Television turned on");
}

public static String getManufacturer(){


return "Unknown Manufacturer";
}
}
public class Main2 {
public static void main(String[] args) {
Device d1 = new Device();
Television t1 = new Television();

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

d1.turnOn();
t1.turnOn();

System.out.println(Device.getManufacturer());
System.out.println(Television.getManufacturer());
Device dt1 = new Television();
dt1.turnOn();
}
}

Output:

Task 4: Consider a superclass Vehicle with a method drive() that prints "Driving a vehicle".
Implement two subclasses: Car and Motorcycle, both of which extend Vehicle. Override the
drive() method in both subclasses to print "Driving a car" and "Driving a motorcycle"
respectively. In the Main class, create objects of type Car and Motorcycle, assign them to
Vehicle references, and call the drive() method. Discuss whether early binding or late binding
occurs in this scenario, and why.
class Vehicle{
public void drive(){
System.out.println("Driving a vehicle");
}
}

class Car extends Vehicle {


@Override
public void drive() {
System.out.println("Driving a car");
}
}

class Motorcycle extends Vehicle {


@Override
public void drive() {
System.out.println("Driving a motorcycle");
}
}
public class Main3 {
public static void main(String[] args) {
Vehicle car = new Car();
Vehicle motorcycle = new Motorcycle();

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

car.drive();
motorcycle.drive();
}
}

Output:

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS

You might also like