[Go to site: main page, start]

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

Java Inheritance and Polymorphism Examples

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)
6 views4 pages

Java Inheritance and Polymorphism Examples

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

Java Programs - Inheritance and

Polymorphism
1. Method Overriding in Java

AIM
To implement method overriding in Java.

PROGRAM

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

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() {
[Link]("Parent class constructor called");
}
}
class Child extends Parent {
Child() {
super(); // Call to parent constructor
[Link]("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() {
[Link]("Parent class display method");
}
}
class Child extends Parent {
void display() {
[Link](); // Call to parent method
[Link]("Child class display method");
}
}
public class SuperMethod {
public static void main(String[] args) {
Child obj = new Child();
[Link]();
}
}

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() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
void sound() {
[Link]("Cat meows");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Animal a;
a = new Dog();
[Link](); // Dog's sound method is called

a = new Cat();
[Link](); // Cat's sound method is called
}
}

OUTPUT
Dog barks
Cat meows

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

Common questions

Powered by AI

Polymorphism in software development, particularly in Java, facilitates flexibility and reusability of code. It allows methods to work with objects of different classes as long as they derive from a common superclass, enabling a single function to handle different types of operations at runtime. This makes code easier to extend and maintain. For example, in runtime polymorphism demonstrated by dynamic method dispatch, new subclasses can be introduced without altering existing code, as the base class interface remains consistent yet allows class-specific functionality, which is crucial for large-scale, maintainable applications .

In Java, when creating an object of a subclass, the constructor of the base class is called automatically before the subclass's constructor, ensuring that the base class part of the subclass object is initialized. This default behavior can be explicitly controlled using the 'super()' keyword in a subclass's constructor, which calls a specific constructor of the superclass, as shown in the program where the Child class calls the Parent class constructor using 'super()', ensuring the proper order of initialization .

The 'super()' constructor call in Java is significant as it ensures that the superclass is initialized before the subclass. This is crucial for proper chaining of constructors and for the integrity of data members inherited from the superclass. It allows for controlled setup of the state and behavior of objects, particularly when subclasses extend the functionality of a base class. By ensuring that the base class constructor is invoked, 'super()' also maintains the proper lifecycle of inheritance hierarchies, as demonstrated in the program where the Child's constructor begins by calling the Parent constructor with 'super()', illustrating the ordered initialization process .

Method overriding is essential for achieving runtime polymorphism in Java, as it allows a subclass to provide a specific implementation of a method already declared in its superclass. This mechanism enables dynamic method dispatch, where a superclass reference variable decides at runtime which overridden method is executed depending on the actual object's type. In the PolymorphismDemo program, overriding the sound method in the Dog and Cat classes allows different behaviors when the method is invoked on an Animal reference, illustrating how method overriding facilitates runtime polymorphism .

Java handles method calls using dynamic method dispatch by resolving the method call at runtime rather than compile time. This allows a base class reference to invoke methods that are actually defined in any subclass. Java determines which version of the method to execute based on the actual object type that the reference points to during execution. This capability allows for runtime flexibility, as seen in the example where an Animal reference is used to call the sound method, and it resolves to the sound method of the actual object type (Dog or Cat) at runtime, demonstrating dynamic dispatch in action .

You can call a superclass method within a subclass by using the 'super' keyword followed by the method name. This explicitly invokes the method defined in the superclass, even if a similar method is overridden in the subclass. For instance, in the given program, the display method in the Child class calls its superclass Parent's display method with 'super.display()', before executing its own display method, demonstrating how superclass methods can be accessed from within a subclass .

The `super.display()` call in method overriding serves the purpose of invoking a method from the superclass from within an overriding method in the subclass. This allows both the superclass and subclass instances to execute their versions of the method, facilitating code reuse and logic extension. For instance, in the program, the Child class overrides the display() method but calls `super.display()` within it to execute the Parent class's display method first. This demonstrates how the subclass can extend or build upon superclass logic, effectively merging behaviors of both class levels .

The 'super' keyword in Java is used to refer to the immediate superclass of the current object. It is utilized in two primary ways: to call a superclass constructor and to access superclass methods. When used in a constructor, as in the Child class extending Parent, 'super()' calls the constructor of the Parent class, ensuring that the base class is initialized before the child. Additionally, 'super' can be used within a method in the subclass to invoke a version of a method defined in the superclass, which is demonstrated when the display method in the Child class calls the Parent class's display method using 'super.display()' .

Dynamic method dispatch is a process in Java where the method call is resolved at runtime rather than at compile time. It is significant as it provides the backbone for achieving runtime polymorphism. Java utilizes a base class reference variable to reference a subclass object, and when a method is called on the reference, the version of the method in the actual object's class is executed. This allows for flexibility and scalability in applications, enabling method calls to exhibit different behaviors depending on the object's class type at runtime. The example with Animal, Dog, and Cat classes shows dynamic method dispatch, where calling sound on an Animal reference actually executes the Dog's or Cat's sound method based on the object it refers to, demonstrating polymorphism .

Method overriding in Java is a key aspect of run-time polymorphism. It allows a subclass to provide a specific implementation of a method that is already defined in its superclass. When a base class reference points to a subclass object, the overridden method in the subclass is invoked at runtime, demonstrating polymorphism. This is seen in the program where a Dog and a Cat class both override the sound method of the Animal class. When the sound method is called on Animal references that point to Dog or Cat objects, the output differs based on the actual object, illustrating polymorphism .

You might also like