Computer Science, asked by lovelistedadnan5720, 4 months ago

Can we have inheritance
without polymorphism? Justify your views with valid examples.

Answers

Answered by mssst94
1

Answer:

there can not exist a polymorphism without inheritance.

Explanation:

Static and runtime.

Static polymorphism doesn’t require inheritence.

Runtime polymorphism is based on inheritance.

// A Java program to illustrate Dynamic Method

// Dispatch using hierarchical inheritance

class A

{

void m1()

{

System.out.println("Inside A's m1 method");

}

}

class B extends A

{

// overriding m1()

void m1()

{

System.out.println("Inside B's m1 method");

}

}

class C extends A

{

// overriding m1()

void m1()

{

System.out.println("Inside C's m1 method");

}

}

// Driver class

class Dispatch

{

public static void main(String args[])

{

// object of type A

A a = n

Become a successful data analyst in 90 days.

Let us clarify what polymorphism means and then give an example to see if it is possible without inheritance or not.

Polymorphism is the fact of a reference (variable) taking more than one form (type).

Given three types (class/interface) A, B and C where :

B extends A

C extends A

A reference of type A can take the form of an object of type B or C, therefore it can take more than one form (polymorphic).

In the previous example, the reference of type A is polymorphic because of inheritance, therefore there can not exist a polymorphism without inheritance.

So, every reference of a non final type is a possible polymorphic reference (meaning it can take more than one form).

Similar questions