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

Java Inheritance Polymorphism

The document presents Java programs demonstrating key concepts of inheritance and polymorphism, including method overriding, calling base class constructors and methods using the super keyword, and implementing run-time polymorphism through dynamic method dispatch. Each section includes an aim, program code, output, and conclusion summarizing the results. The examples illustrate how derived classes can override methods and utilize base class functionalities effectively.

Uploaded by

Divyanshi
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)
4 views

Java Inheritance Polymorphism

The document presents Java programs demonstrating key concepts of inheritance and polymorphism, including method overriding, calling base class constructors and methods using the super keyword, and implementing run-time polymorphism through dynamic method dispatch. Each section includes an aim, program code, output, and conclusion summarizing the results. The examples illustrate how derived classes can override methods and utilize base class functionalities effectively.

Uploaded by

Divyanshi
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/ 4

Java Programs - Inheritance and

Polymorphism
1. Method Overriding in Java

AIM
To implement method overriding in Java.

PROGRAM

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class MethodOverride {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}

OUTPUT
Dog barks

CONCLUSION
The program successfully overrides the method of the base class in the derived class.

2. Call Base Class Constructor using super()

AIM
To call the base class constructor using super keyword in Java.
PROGRAM

class Parent {
Parent() {
System.out.println("Parent class constructor called");
}
}
class Child extends Parent {
Child() {
super(); // Call to parent constructor
System.out.println("Child class constructor called");
}
}
public class SuperConstructor {
public static void main(String[] args) {
Child obj = new Child();
}
}

OUTPUT
Parent class constructor called
Child class constructor called

CONCLUSION
The program demonstrates how to use super() to call the base class constructor from a
subclass.

3. Call Base Class Method using super keyword

AIM
To call a base class method using the super keyword in Java.

PROGRAM

class Parent {
void display() {
System.out.println("Parent class display method");
}
}
class Child extends Parent {
void display() {
super.display(); // Call to parent method
System.out.println("Child class display method");
}
}
public class SuperMethod {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}

OUTPUT
Parent class display method
Child class display method

CONCLUSION
The program shows how to use super to invoke a method of the base class from the derived
class.

4. Run Time Polymorphism using Dynamic Method Dispatch

AIM
To implement run-time polymorphism using method overriding and dynamic dispatch in
Java.

PROGRAM

class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Dog's sound method is called

a = new Cat();
a.sound(); // Cat's sound method is called
}
}

OUTPUT
Dog barks
Cat meows

CONCLUSION
The program successfully demonstrates run-time polymorphism using dynamic method
dispatch.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy