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

Java Polymorphism: Overloading vs Overriding

Polymorphism in Java allows objects to take many forms and is categorized into compile-time (method overloading) and runtime (method overriding) polymorphism. Method overloading involves multiple methods with the same name but different parameters, while method overriding occurs when a subclass provides a specific implementation of a superclass method. Key differences include that overloading does not require inheritance and uses static binding, whereas overriding requires inheritance and uses dynamic binding.
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)
6 views3 pages

Java Polymorphism: Overloading vs Overriding

Polymorphism in Java allows objects to take many forms and is categorized into compile-time (method overloading) and runtime (method overriding) polymorphism. Method overloading involves multiple methods with the same name but different parameters, while method overriding occurs when a subclass provides a specific implementation of a superclass method. Key differences include that overloading does not require inheritance and uses static binding, whereas overriding requires inheritance and uses dynamic binding.
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

Object-Oriented Programming (OOP) - Java Concepts

1. Define polymorphism and explain its two types in Java.

Polymorphism in Java is the ability of an object to take many forms. It allows one interface to be

used for a general class of actions. The most common use of polymorphism in OOP occurs when a

parent class reference is used to refer to a child class object.

There are two types of polymorphism in Java:

1. **Compile-Time Polymorphism (Method Overloading)**:

- Achieved by defining multiple methods with the same name but different parameter lists (type,

number, or order).

- Decided at compile time.

- Example:

```java

class Demo {

void show(int a) { }

void show(String b) { }

```

2. **Runtime Polymorphism (Method Overriding)**:

- Achieved when a subclass provides a specific implementation of a method that is already defined

in its superclass.
Object-Oriented Programming (OOP) - Java Concepts

- Decided at runtime using dynamic method dispatch.

- Example:

```java

class Animal {

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

class Dog extends Animal {

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

```

2. What are the key differences between method overloading and method overriding?

- **Definition**:

- Overloading: Multiple methods with the same name but different parameters.

- Overriding: A subclass provides a specific implementation of a method already defined in the

superclass.

- **Polymorphism Type**:

- Overloading: Compile-time polymorphism.

- Overriding: Runtime polymorphism.

- **Inheritance Requirement**:
Object-Oriented Programming (OOP) - Java Concepts

- Overloading: Not required.

- Overriding: Requires inheritance.

- **Binding**:

- Overloading: Static binding.

- Overriding: Dynamic binding.

3. Can method overloading be achieved in different classes (i.e., parent and child classes)? Why or

Yes, method overloading can be achieved in parent and child classes. If a child class defines a

method with the same name as in the parent class but with different parameters (signature), it is

considered overloading, not overriding.

This is because method overloading is based on method signatures, not class hierarchy. However,

true polymorphism requires the methods to be part of the same class or inherited class hierarchy.

You might also like