Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8327017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:57:37+00:00 2026-06-09T00:57:37+00:00

In an interview I was asked if polymorphism can be achieved without inheritance. Is

  • 0

In an interview I was asked if polymorphism can be achieved without inheritance. Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:57:39+00:00Added an answer on June 9, 2026 at 12:57 am

    The best explanation on the subject that I’ve ever read is an article by Luca Cardelli, a renown type theorist. The article is named On Understanding Types, Data Abstraction, and Polymorphism.

    Types of Polymorphism

    Cardelli defines several types of polymorphism in this article:

    • Universal
    • parametric
    • inclusion
    • Ad-hoc
    • overloading
    • coercion

    The kind of polymorphism related to inheritance is classified as inclusion polymorphism or subtype polymorphism.

    Wikipedia provides a good definition:

    In object-oriented programming, subtype polymorphism or inclusion
    polymorphism is a concept in type theory wherein a name may denote
    instances of many different classes as long as they are related by
    some common super class. Inclusion polymorphism is generally
    supported through subtyping, i.e., objects of different types are
    entirely substitutable for objects of another type (their base
    type(s)) and thus can be handled via a common interface.
    Alternatively, inclusion polymorphism may be achieved through type
    coercion, also known as type casting.

    Another Wikipedia article called Polymorphism in object-oriented programming seems to answer your questions as well.

    In Java

    This subtyping feature in Java is achieved, among other means, through the inheritance of classes and interfaces. Although the subtyping features of Java may not be evident in terms of inheritance all the time. Take for example the cases of covariance and contravariance with generics. Also, arrays are Serializable and Cloneable although this is not evident anywhere in the type hierarchy. It can also be said that through primitive widening conversion the numeric operators in Java are polymorphic, in certain cases even accepting totally unrelated operands (i.e. concatenation of strings and numbers or of a string plus some other object). Consider also the cases of boxing and unboxing of primitives. These latter cases of polymorphism (coercion and overloading) are not at all related to inheritance.

    Examples

    Inclusion

    List<Integer> myInts = new ArrayList<Integer>();
    

    This is the case to which your question seems to refer i.e. when there is an inheritance or implementation relationship between the types, as in this case where ArrayList implements List.

    As I mentioned, though, when you introduce Java generics, some time the rules of subtyping get fuzzy:

    List<? super Number> myObjs = new ArrayList<Object>();
    List<? extends Number> myNumbers = new LinkedList<Integer>();
    

    And in other cases, the relationships are not even evident in the API

    Cloneable clone = new int[10];
    Serializable obj = new Object[10]
    

    Even so, all these, according to Cardelli, are forms of universal polymorphism.

    Parametric

    public <T> List<T> filter(Predicate<T> predicate, List<T> source) {
      List<T> result = new ArrayList<>();
      for(T item : source) {
        if(predicate.evaluate(item)){
             result.add(item);
        }
       return result;
      }
    }
    

    The same algorithm can be used to filter all kinds of lists with all kinds of predicates without having to repeat a single line of code for every possible type of list. The type of the actual list and the type of predicate are parametric. Like in the following examples with Java lambda expressions.

    filter(x -> x % 2 == 0, asList(1,2,3,4,5,6)); //filters even integers
    filter(x -> x % 2 != 0, asList(1L,2L,3L,4L,5L,6L)); //filters odd longs
    filter(x -> x >= 0.0, asList(-1.0, 1.0)); //filters positive doubles
    

    According to Cardelli, this is a form of universal polymorphism.

    Coercion

    double sum = 1 + 2.0;
    

    Integer and floating-point arithmetic are totally different. Applying the plus operator to two operands of different types here is impossible without some form of coercion.

    In this example, the types integer and double, are automatically coerced (converted) to type double without an explicit cast. The entire expression is promoted to double. This is so because in Java we have primitive widening conversions.

    According to Cardelli, this form of automatic coercion is a form of ad-hoc polymorphism provided for the plus operator.

    There are languages in which you could not even sum an integer and a floating-point number without an explicit cast (i.e. AFAIK, SML, in which, by the way, parametric polymorphism is key to overcome this kind of problems).

    Overloading

    double sum = 2.0 + 3.0;
    String text = "The sum is" + sum;
    

    The plus operator here means two different things depending on the arguments used. Evidently, the operator has been overloaded. This implies it has different implementations depending on the types of operands. According to Cardelli, this is a form of ad-hoc polymorphism provided for the plus operator.

    This, of course, also applies to forms of method overloading in classes (i.e java.lang.Math methods min and max are overloaded to support different primitive types).

    In Other Languages

    Even when inheritance plays an important role in the implementation of some of these forms of polymorphism, certainly it is not the only way. Other languages that are not object-oriented provide other forms of polymorphism. Take, for example, the cases of duck typing in dynamic languages like Python or even in statically-typed languages like Go, or algebraic data types in languages like SML, Ocaml and Scala, or type classes in languages like Haskell, multi methods in Clojure, prototypal inheritance in JavaScript, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was the question asked in interview. Can we call one constructor from another
I asked this question in an interview. In asp.net how can we check the
I was asked this in an interview today and I just can't figure it
This is an interview question asked a month ago.... Do session use cookies? If
Yesterday in my interview I was asked this question. (At that time I was
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
I was asked in an interview to enlighten the ways one can use to
I was asked this in my interview today, I applied for a graduate developer
This was one of the interview questions asked. How to find the length of
Interview Question I have been asked this question in an interview, and the answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.