0% found this document useful (0 votes)
2 views4 pages

Java Lab-4

The document explains inheritance in Java, detailing single inheritance with examples of Addition and Subtraction classes, as well as multilevel inheritance involving Addition, Subtraction, Multiplication, Division, and Modulus classes. It also covers polymorphism, defining compile-time polymorphism through method overloading and runtime polymorphism through method overriding, with examples using a Calculator class and Animal subclasses. The main classes demonstrate method calls for both inheritance and polymorphism concepts.

Uploaded by

gadane720
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)
2 views4 pages

Java Lab-4

The document explains inheritance in Java, detailing single inheritance with examples of Addition and Subtraction classes, as well as multilevel inheritance involving Addition, Subtraction, Multiplication, Division, and Modulus classes. It also covers polymorphism, defining compile-time polymorphism through method overloading and runtime polymorphism through method overriding, with examples using a Calculator class and Animal subclasses. The main classes demonstrate method calls for both inheritance and polymorphism concepts.

Uploaded by

gadane720
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

1.

Inheritance
2. Single Inheritance
 One class inherits from another.
class Addition {
void add() {
int x = 10, y = 20;
[Link]("Add = " + (x + y));
}
}
class Subtraction extends Addition {
void sub() {
int x = 20, y = 10;
[Link]("Sub = " + (x - y));
}
}
public class Test {
public static void main(String[] args) {
Subtraction obj = new Subtraction();
[Link](); // parent method
[Link](); // child method
}
}
3. Multilevel Inheritance
//Addition (Base Class)
public class Addition {
public void add(){
int a = 20, b = 10;
int sum = a + b;
[Link]("Addition = " + sum);
}
}
//Subtraction (extends Addition)
public class Subtraction extends Addition{
void sub() {
int a = 20, b = 10;
int difference = a - b;
[Link]("Subtraction = " + difference);
}
}
//Multiplication (extends Subtraction)
public class Multiplication extends Subtraction{
void multi() {
int a = 20, b = 10;
int product = a * b;
[Link]("Multiplication = " + product);
}
}
//Division (extends Multiplication)
public class Division extends Multiplication {
void div() {
int a = 20, b = 10;
int quotient = a / b;
[Link]("Division = " + quotient);
}
}
//Modulus (extends Division)
public class Modulus extends Division {
void mod() {
int a = 20; int b = 10;
int result = a % b;
[Link]("Remainder = " + result);
}
}

//Main class (Calling methods)


public class Main {
public static void main(String[] args) {
Modulus obj=new Modulus();
[Link](); // from Addition class
[Link](); // from Subtraction class
[Link](); // from Multiplication class
[Link](); // from Division class
[Link](); // from Modulus class
}
}
2. Polymorphism
 Polymorphism means one name and many forms.
 There are two types:
1. Compile-time Polymorphism (Method Overloading)
2. Runtime Polymorphism (Method Overriding)

1. Compile-time Polymorphism (Method Overloading)

 Same method name, different parameters.


class Calculator {
void add(int a, int b) {
[Link]("Sum = " + (a + b));
}
void add(int a, int b, int c) {
[Link]("Sum = " + (a + b + c));
}
}
public class Test {
public static void main(String[] args) {
Calculator obj = new Calculator();
[Link](10, 20);
[Link](10, 20, 30);
}
}

2. Runtime Polymorphism (Method Overriding)

 Function or method with same name and same parameter.


 Child class redefines a method of the parent class.

class Animal {
void sound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


void sound() {
[Link]("Dog barks");
}
}

class Cat extends Animal {


void sound() {
[Link]("Cat meows");
}
}

public class Test {


public static void main(String[] args) {
Dog obj1 = new Dog();
[Link](); // Dog barks

Cat obj2 = new Cat();


[Link](); // Cat meows
}
}

You might also like