[go: up one dir, main page]

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

NIMIEXP4JAVA

The document explains method overriding as a means to achieve runtime polymorphism in Java, contrasting it with method overloading, which is a form of compile-time polymorphism. It discusses the importance of inheritance in polymorphism and provides several programming examples demonstrating method overloading and overriding in various contexts, such as area calculation and payroll systems. Additionally, it highlights the roles of compile-time and runtime mechanisms in ensuring code correctness and behavior during execution.

Uploaded by

knb.23.09.2008
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)
11 views7 pages

NIMIEXP4JAVA

The document explains method overriding as a means to achieve runtime polymorphism in Java, contrasting it with method overloading, which is a form of compile-time polymorphism. It discusses the importance of inheritance in polymorphism and provides several programming examples demonstrating method overloading and overriding in various contexts, such as area calculation and payroll systems. Additionally, it highlights the roles of compile-time and runtime mechanisms in ensuring code correctness and behavior during execution.

Uploaded by

knb.23.09.2008
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
You are on page 1/ 7

Aim : Implement method overriding to achieve runtime polymorphism

Theory :
Polymorphism:

❖ Method Overloading (compile-time) and Method Overriding (runtime).


❖ Advanced Features: Use of super, final for class/method restriction,
Object Cloning, and Reusability Best Practices.

Compile time and run time mechanisms :


➢ Compile-time checks ensure that your code adheres to Java's syntax and type
system,
➢ while run-time mechanisms oversee the actual execution of your
program, including memory management, security, and dynamic behavior.
➢ Both mechanisms work together to produce reliable and efficient Java
applications.

Polymorphism:
Polymorphism means "many forms." In Java, it allows one action to be
performed in dif erent ways.
Inheritance plays a key role in achieving polymorphism, where objects of different
classes can be treated as objects of a common superclass.
This allows for more flexible and extensible code. i.e Polymorphism is extensively
used in implementing inheritance.
Implementation of inheritance is performed at compile time. It is at compile time
that the child class inherits the public data members and methods of the base class.

Compile time and run time mechanisms:


These mechanisms ensure that your Java code is both syntactically correct and
behaves as expected when it runs.

Method Overloading
When there are multiple methods with the same name but different parameters then
these functions/methods are said to be overloaded.
Methods can be overloaded by changes in the number of arguments or/and a
change in the type of arguments.

Compile-Time Polymorphism
It is also known as static polymorphism. This type of polymorphism is achieved by
function overloading or operator overloading.

Runtime Polymorphism:
➢ It is also known as Dynamic Method Dispatch. It is a process in which a function
call to the overridden method is resolved at Runtime.
➢ This type of polymorphism is achieved by Method Overriding.

Method overriding, on the other hand, occurs when a derived class has a definition for
one of the member functions of the base class. That base function is said to be
overridden.
PROGRAM 1:Overloading by Different Number of Arguments
class SimpleMultiplication
{
int multiply(int a, int b)
{
return a*b;
}
int multiply(int a, int b, int c)
{
return a*b*c;
}
}
class Demo
{
public static void main(String args[])
{
SimpleMultiplication num = new
SimpleMultiplication();
System.out.println(num.multiply(10, 20));
System.out.println(num.multiply(10, 20, 30));
}
}
OUTPUT:

Program 2: Overloading by Changing Sequence of Arguments


class OverloadExample2 {
void show(int a, double b) {
System.out.println("Integer and Double: " + a + ", " + b);
}
void show(double a, int b) {
System.out.println("Double and Integer: " + a + ", " + b);
}

public static void main(String[] args) {


OverloadExample2 obj = new OverloadExample2();
obj.show(10, 20.5);
obj.show(30.5, 40);
}}
OUTPUT:

Program 3: Overloading by Changing Type of Arguments


class Multi {
static int Multiply(int a, int b)
{
return a * b;
}
static double Multiply(double a, double b)
{
return a * b;
}}
class Main {
public static void main(String[] args)
{
System.out.println(Multi.Multiply(2, 4));
System.out.println(Multi.Multiply(5.5, 6.3));
}}
OUTPUT:

Problem Statement 1: Shape Area Calculator


class AreaCalculator{
double calculateArea(double radius)
{return Math.PI*radius*radius;}
double calculateArea(double length,double breadth)
{ return length*breadth;}
double calculateArea(double base, double height, boolean isTriangle)
{return (base*height)/2;}
double calculateArea(double a, double b, double height)
{return ((a+b)*height)/2;}
}
class Demo{
public static void main(String[] args){
AreaCalculator c=new AreaCalculator();
System.out.println("CIRCLE AREA:"+c.calculateArea(4));
System.out.println("RECTANGLE AREA:"+c.calculateArea(4,6));
System.out.println("TRIANGLE AREA:"+c.calculateArea(6,8,true));
System.out.println("TRAPEZIUM AREA:"+c.calculateArea(4,6,9));
}}
OUTPUT:

PROGRAM 4:( BANKING SYSTEM)


class Account {
double balance;
Account(double balance) {
this.balance = balance;
}
void calculateInterest() {
System.out.println("Calculating interest for a generic account.");
}
}
class SavingsAccount extends Account {
double interestRate;
SavingsAccount(double balance, double rate) {
super(balance);
interestRate = rate;
}
void calculateInterest() {
double interest = balance * (interestRate / 100);
System.out.println("Savings account interest: " + interest);
}
}
class CheckingAccount extends Account {
CheckingAccount(double balance) {
super(balance);
}
void calculateInterest() {
System.out.println("Checking account doesn't earn interest.");
}
}
public class BankingSystemDemo {
public static void main(String[] args) {
SavingsAccount savingsAccount = new SavingsAccount(5000.0, 4.5);
CheckingAccount checkingAccount = new CheckingAccount(3000.0);
savingsAccount.calculateInterest();
checkingAccount.calculateInterest(); }}
OUTPUT:

PROGRAM : ( EMPLOYEE PAYROLL)


class Employee {
protected String name;
protected int employeeID;
protected double basicSalary;

public Employee(String name, int employeeID, double basicSalary) {


this.name = name;
this.employeeID = employeeID;
this.basicSalary = basicSalary;
}

public double calculateSalary() {


return basicSalary;
}

public void displaySalary() {


System.out.println("Employee: " + name + " | ID: " + employeeID + " | Salary: " +
calculateSalary());
}
}

class Manager extends Employee {


private static final double ALLOWANCE = 5000.0;

public Manager(String name, int employeeID, double basicSalary) {


super(name, employeeID, basicSalary);
}
//Override
public double calculateSalary() {
return basicSalary + ALLOWANCE;
}
}

class Clerk extends Employee {


private static final double BONUS = 2000.0;
public Clerk(String name, int employeeID, double basicSalary) {
super(name, employeeID, basicSalary);
}

//Override
public double calculateSalary() {
return basicSalary + BONUS;
}
}

public class PayrollSystem {


public static void main(String[] args) {
Employee manager = new Manager("Nimisha", 101, 40000);
Employee clerk = new Clerk("Naksh", 102, 20000);

manager.displaySalary();
clerk.displaySalary();
}
}
OUTPUT:

You might also like