Faisal Ahmad

Ranch Hand
+ Follow
since Aug 31, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Faisal Ahmad

Paul, thank you for your reply. When I said "what exactly are polymorphic in Java", I actually meant - "what exactly are polymorphic in Java - references or instances?". I understand what benefits polymorphism brings in Java, and how it works.

I became curious after I came across different descriptions of polymorphism - and that too from Sun/Oracle. Then I started to dig deeper to understand exactly how polymorphism works in Java.

So far it seems - both reference variables and objects are polymorphic.
6 months ago
I am studying various resources to understand how Java implements polymorphism, and what exactly are polymorphic in Java. It is surprising to see that there exist quite opposite views on what are polymorphic in Java.

In programming language theory and type theory, polymorphism is the use of a single symbol to represent multiple different types. -- Luca Cardelli et. al.

In object-oriented programming, polymorphism is the provision of a single interface to entities of different types. -- Bjarne Stroustrup




The reference variable Animal a is polymorphic because it can refer to a Cat instance and a Dog instance, not just an Animal object. This is one aspect of polymorphism. A reference variable can refer to an instance of its own type, and its subtypes. The ability of the reference variable Animal a to refer to an instance of its own type and also its subtypes is polymorphic.



In the above code block, the instruction was to  a.makeNoise(), but a different form of the  makeNoise() method has been invoked at runtime. At compile-time it was a call to  Animal class's  makeNoise(). But at runtime, it was a call to  Dog class's  makeNoise(). This is another aspect of polymorphism - virtual method invocation.

Somewhere on the web:

So polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).



Thus, I have concluded that - the reference variable is polymorphic, and not the instance/object. Object/instance has only one form.  Based on all the quotes discussed - it is the super-type reference variable that is polymorphic, not the objects of sub-types. Animal is the interface for underlying forms - Dog and Cat.

Update:
An instance can be referred to using references of different types, including its own type:

So, that makes an instance polymorphic?!
6 months ago
I know how reference variable works. And the relation between an object and a reference variable. Also, I understand inheritance - super-type and sub-types.

My question is - what should be considered as polymorphic - reference variable or object?

A book says, an object has only one form - thus, it is not polymorphic. It is the reference variable that is polymorphic.

Other books say that object is polymorphic, not the reference variables.

Or, are they both polymorphic?
6 months ago
Yes. IMHO, architecture is about designing the components of the software. Designing classes, interfaces etc. An architect, instead of coding, would spend most of the time using UML or other modeling tools.
6 months ago
I am confused after having read different explanations of polymorphism in Java.

1. The Java Programming Language book:

polymorphism, meaning that an object of a given class can have multiple forms, either as its own class or as any class it extends. The new class is a subclass or extended class of the class it extends; the class that is extended is its superclass.



2. Oracle university course book:

  • Polymorphism is the ability to have many different forms; for example, the Manager class has access to methods from Employee class.
  • An object has only one form.
  • A reference variable can refer to objects of different forms.



  • 3. OCP Java SE 17 Developer Study Guide book:

    The property of an object to take on many different forms. To put this more precisely, a Java object may be accessed using:

  • A reference with the same type as the object
  • A reference that is a superclass of the object
  • A reference that defines an interface the object implements or inherits



  • What is polymorphic in Java - the reference variable or the object?
    6 months ago
    Hi!

    When can we expect K&B book on OCP Java 17 to be available?
    Are you new to servlets & jsps? If yes, go and grab Murach's Servlet & JSP book. It explains you how to build professional web application using servlets & jsps. They also have code available online.

    Best,
    Faisal
    10 years ago
    Though Spring and Struts are widely used, everything depends on your requirements.
    10 years ago
    I believe, it lives until response is committed. I think, once the response is committed it can't be accessed.

    Have I got that right?

    Ulf Dittmer wrote:A response is generated once it's been determined how a request should be handled.



    You mean - ServletResponse reference that is passed to a servlet doesn't refer to an existing object?


    Ulf Dittmer wrote:So from that I find it more natural to attach attributes to requests, because the request will exist even when the response does not yet.

    On a more practical level, I think it would be confusing to have attributes for both. By choosing one, it's clear where to look for attributes.



    Consider the below flow:

    Container passes client's request as an input to Servlet -> Servlet processes the input and adds its output/response to the ServletResponse object and then passes on these objects to next in action (view for e.g.) -> View checks the ServletResponse and performs some action based on the what it found.

    It's Servlet's response. So, isn't it more intuitive/logical to find it in a 'ServletResponse' object?

    Maneesh Godbole wrote:'Have' is correct English. Why do you want to change it to 'has'



    I have some confusion regarding when to use 'doesn't have' and 'doesn't has'. I searched for some guidance on the web but I'm not fully convinced with the info that I found so far.
    Instead of adding attribute to the request object that's being passed to any 'view' from a servlet for e.g., it is better to add it to the response object - that sounds intuitive. It's like - servlet does something with the request (input) and passes on its results (output) through response object to the next item in action.

    Looks weird? Please share your thoughts.

    Under what circumstances do you expect to see it?



    Good question!
    10 years ago
    Thanks a lot! Great book to have.
    10 years ago