0% found this document useful (0 votes)
5 views3 pages

Java Interface Polymorphism Example

The document presents a Java program demonstrating key object-oriented concepts such as method overriding, abstract classes, the super keyword, final methods, polymorphism, inheritance, and interfaces. It defines an interface 'Animal' and an abstract class 'LivingBeing', with a concrete implementation in the 'Dog' class. The main class showcases polymorphism by using parent references to call overridden methods and final methods from the parent class.

Uploaded by

howareyu494
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)
5 views3 pages

Java Interface Polymorphism Example

The document presents a Java program demonstrating key object-oriented concepts such as method overriding, abstract classes, the super keyword, final methods, polymorphism, inheritance, and interfaces. It defines an interface 'Animal' and an abstract class 'LivingBeing', with a concrete implementation in the 'Dog' class. The main class showcases polymorphism by using parent references to call overridden methods and final methods from the parent class.

Uploaded by

howareyu494
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

Common java program which contains following concepts:

• Method Overriding (@Override)


• Abstract Class
• super Keyword (calling parent constructor and methods)
• final Keyword (prevents method overriding)
• Polymorphism (Dynamic Method Dispatch)
• Inheritance (extends) and Interface (implements)

You can write this program in all concepts of unit 3.1 and 3.2

// Define an interface

interface Animal {

void makeSound(); // Abstract method to be implemented

// Abstract class

abstract class LivingBeing {

// Constructor

LivingBeing() {

[Link]("A living being is created.");

// Concrete method (can be overridden)

void breathe() {

[Link]("Living being is breathing...");

// Final method (CANNOT be overridden)

final void grow() {

[Link]("Living being is growing...");

// Abstract method (MUST be implemented by subclasses)

abstract void eat();


}

// Child class Dog extends LivingBeing and implements Animal

class Dog extends LivingBeing implements Animal {

// Constructor using super()

Dog() {

super(); // Calls parent constructor

[Link]("A dog is created.");

// Overriding makeSound() from Animal interface

public void makeSound() {

[Link]("Dog barks: Woof Woof!");

// Overriding breathe() from LivingBeing

@Override

void breathe() {

[Link](); // Calls parent method

[Link]("Dog is breathing heavily...");

// Implementing abstract method eat()

@Override

void eat() {

[Link]("Dog is eating dog food...");

// Main class demonstrating all concepts

public class AbstractExtendImplement {

public static void main(String[] args) {


// Polymorphism: Parent reference holding a child object

Animal myDog = new Dog();

[Link](); // Calls Dog's version of makeSound()

// Using LivingBeing reference

LivingBeing lb = new Dog();

[Link](); // Calls overridden breathe() method from Dog

[Link](); // Calls final method from LivingBeing (cannot be overridden)

[Link](); // Calls Dog's implementation of eat()

Common questions

Powered by AI

In the provided Java code, the 'super' keyword is used to refer to the parent class of an object. It is applied to invoke the parent class's constructor and methods. In the 'Dog' class, 'super()' is called in its constructor to execute the constructor of its parent class 'LivingBeing', which outputs the message 'A living being is created.' Additionally, within the 'breathe()' method, 'super.breathe()' is called to use the implementation from 'LivingBeing', ensuring that both the original and the overridden behavior are executed when 'breathe()' is called on a 'Dog' object .

The 'implements' keyword in Java indicates that a class is providing implementations for methods declared in an interface. In the provided program, the 'Dog' class uses 'implements' to commit to the provisions of the 'Animal' interface, meaning 'Dog' must provide concrete implementations for all abstract methods declared, such as 'makeSound()'. This establishes a contract where 'Dog' can be referenced and used interchangeably with 'Animal', supporting polymorphism and interface-based programming .

Dynamic method dispatch in Java refers to the process where a call to an overridden method is resolved at runtime, rather than compile time. This is exemplified in the provided code through the references 'Animal myDog' and 'LivingBeing lb', which point to 'Dog' objects. When 'makeSound()' or 'breathe()' is called on these references, Java uses dynamic dispatch to determine the method implementation in the actual object's class ('Dog'), thus calling 'Dog's' version of these methods during runtime. This enables runtime polymorphism by allowing method calls to be determined by the object's actual type rather than the reference type .

The 'final' keyword in Java, when applied to methods, indicates that such a method cannot be overridden by subclasses. This is essential for preserving the intended behavior of methods that should remain constant across all derived classes, ensuring consistent behavior. In the document, the method 'grow()' in the 'LivingBeing' class is marked as 'final', preventing any subclass (like 'Dog') from altering its implementation. This ensures that the growing process behaves uniformly regardless of subclass modifications .

Inheritance in the provided Java code is shown through the 'Dog' class, which extends the abstract class 'LivingBeing'. This demonstrates single inheritance where 'Dog' inherits properties and behaviors (such as the 'breathe()' method and 'grow()' method) from 'LivingBeing' and is required to implement any abstract methods declared in it. The advantage of inheritance is that it promotes code reuse and establishes a hierarchical classification, allowing derived classes to inherit common functionality from base classes while enabling extensions or modifications specific to derived classes .

In the provided Java program, constructors play a vital role in inheritance as they are automatically invoked in order from superclass to subclass during object creation. When a 'Dog' object is instantiated, its constructor first calls the constructor of its superclass 'LivingBeing' using 'super()', ensuring that any initial setup defined in 'LivingBeing' takes place before completing the 'Dog' specific setup. This order guarantees that parent class constructors are executed first, setting up the necessary object state for inherited properties, thus ensuring a consistent initialization sequence .

Interfaces in Java facilitate a form of multiple inheritance by allowing a class to implement multiple interfaces, effectively inheriting the abstract behavior declared in each. In the provided program, the class 'Dog' implements the 'Animal' interface in addition to extending 'LivingBeing'. This allows 'Dog' to adhere to the contract specified by 'Animal', particularly the 'makeSound()' method, while also inheriting from 'LivingBeing'. Interfaces provide a way to achieve design flexibility by allowing disparate classes to implement shared behaviors, unlike class inheritance which restricts a class to a single path of inheritance .

Method overriding in Java is a mechanism where a subclass provides a specific implementation for a method already defined in its superclass. This allows for dynamic method dispatch, meaning a call to an overridden method is resolved at runtime. Polymorphism is achieved as different classes can behave differently when the same method call is made on objects of different types. The '@Override' annotation is used to inform the compiler that a method is intended to override a method in a superclass. This helps to prevent errors, such as mistyping the method name or changing the method signature unintentionally .

Abstract classes in Java provide a base for subclasses to build upon and can include both abstract methods (methods without a body) and concrete methods (fully implemented). In contrast, interfaces can only declare methods (abstract by default in Java until version 8, which allows default and static methods). The code demonstrates an abstract class 'LivingBeing', which has an abstract method 'eat()' and a concrete method 'breathe()'. The class 'Dog' implements both this abstract method and overrides the concrete one. The interface 'Animal' declares the method 'makeSound()', implemented by 'Dog'. Interfaces support multiple inheritance, while a class can only extend one abstract class .

Polymorphism is illustrated in the Java program through the ability of different classes to define methods that are called through the reference of a parent class type. In the main method, the reference 'Animal myDog' points to an object of the 'Dog' class. Despite being an 'Animal' type reference, it calls the 'makeSound()' method overridden in 'Dog', demonstrating polymorphism. This feature allows for flexible and reusable code by enabling methods to act differently depending on the object type they are invoked on, allowing one interface to be used for different data types .

You might also like