Polymorphism

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 815 Views - Last Post: 11 June 2020 - 12:58 AM Rate Topic: -----

#1 hellswindstaff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 27-February 12

Polymorphism

Posted 18 May 2020 - 03:43 PM

Okay currently studying polymorphism... not sure of what everything is so I wrote what I thought in the comments. I keep hearing the word 'type' thrown around but I'm still not positive as to what that means. Also, Not sure why the program won't actually compile as it is an example from the book that I am using.

1  import java.util.Scanner;
2
3  //Program Shows a simple quiz with two question types.
4
5   public class QuestionDemo3
6   {
7    public static void main(String[] args)
8    {
9       Question first= new Question(); // What is Question? What is first?
10      first.setText("Who was the inventor of Java?"); // first is passed(right term?) to presentQuestion in line 21
11      first.setAnswer("James Gosling"); // first is an object and setAnswer is a method
12      
13      second.setText("In which country was the inventor of Java born");
14      second.addChoice("Australia, false");
15      second.addChoice("Canada, true");
16      second.addChoice("Denmark, false");
17      second.addChoice("United States, false");
18      
19      presentQuestion(first);
20      presentQuestion(second);
21   }
22   
23   /************************************************************************/
24   public static void presentQuestion(Question q)
25    {
26      q.display();
27      System.out.print("Your answer: ");
28      Scanner in= new Scanner(System.in);
29      String response= in.nextLine();
30      System.out.println(q.checkAnswer(response));
31    }
32  }



How often is polymorphism used? I watched theNewBoston videos on it which seemed relatively easy, but it seems like a short but rather complicated topic in my book.

Is This A Good Question/Topic? 0
  • +

Replies To: Polymorphism

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15743
  • View blog
  • Posts: 63,068
  • Joined: 12-June 08

Re: Polymorphism

Posted 18 May 2020 - 04:13 PM

09	9       Question first= new Question(); // What is Question? What is first?

'Question' is a class.
'first' is a variable of class type 'Question'.

Quote

How often is polymorphism used?

As much as it's needed. I use it in quick proof-of-concepts for games where I have a larger class of 'character' and branch off player, non-player, enemy from that. All share some of the same traits defined in 'character'.

In work it is types of alerts, base API components, and so on.


Quote

Not sure why the program won't actually compile

You would need to copy/paste the error messages here.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11958
  • View blog
  • Posts: 20,289
  • Joined: 19-March 11

Re: Polymorphism

Posted 18 May 2020 - 05:38 PM

It would also be useful to make sure that Question is defined some place where Java can find it
Was This Post Helpful? 0
  • +
  • -

#4 hellswindstaff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 27-February 12

Re: Polymorphism

Posted 18 May 2020 - 05:41 PM

 ----jGRASP exec: javac -g QuestionDemo3.java
QuestionDemo3.java:22: error: cannot find symbol
   public static void presentQuestion(Question q)
                                      ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:7: error: cannot find symbol
      Question first= new Question();
      ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:7: error: cannot find symbol
      Question first= new Question();
                          ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:11: error: cannot find symbol
      second.setText("In which country was the inventor of Java born");
      ^
  symbol:   variable second
  location: class QuestionDemo3
QuestionDemo3.java:12: error: cannot find symbol
      second.addChoice("Australia, false");
      ^
  symbol:   variable second
  location: class QuestionDemo3
QuestionDemo3.java:13: error: cannot find symbol
      second.addChoice("Canada, true");
      ^
  symbol:   variable second
  location: class QuestionDemo3
QuestionDemo3.java:14: error: cannot find symbol
      second.addChoice("Denmark, false");
      ^
  symbol:   variable second
  location: class QuestionDemo3
QuestionDemo3.java:15: error: cannot find symbol
      second.addChoice("United States, false");
      ^
  symbol:   variable second
  location: class QuestionDemo3
QuestionDemo3.java:18: error: cannot find symbol
      presentQuestion(second);
                      ^
  symbol:   variable second
  location: class QuestionDemo3
9 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11958
  • View blog
  • Posts: 20,289
  • Joined: 19-March 11

Re: Polymorphism

Posted 18 May 2020 - 07:24 PM

  location: class QuestionDemo3
QuestionDemo3.java:13: error: cannot find symbol
      second.addChoice("Canada, true");



This error is pretty straightforward: the compiler cannot find a definition for second

That is not surprising, since it doesn't seem to be defined in the code you've shown us.
Was This Post Helpful? 1
  • +
  • -

#6 hellswindstaff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 27-February 12

Re: Polymorphism

Posted 18 May 2020 - 08:06 PM

View Postjon.kiparsky, on 18 May 2020 - 07:24 PM, said:

  location: class QuestionDemo3
QuestionDemo3.java:13: error: cannot find symbol
      second.addChoice("Canada, true");



This error is pretty straightforward: the compiler cannot find a definition for second

That is not surprising, since it doesn't seem to be defined in the code you've shown us.



Okay I should have caught that one, but what about this one?

 ----jGRASP exec: javac -g QuestionDemo3.java
QuestionDemo3.java:25: error: cannot find symbol
   public static void presentQuestion(Question q)
                                      ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:9: error: cannot find symbol
      Question first= new Question(); // What is Question? What is first?
      ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:9: error: cannot find symbol
      Question first= new Question(); // What is Question? What is first?
                          ^
  symbol:   class Question
  location: class QuestionDemo3
QuestionDemo3.java:13: error: cannot find symbol
      ChoiceQuestion second= new ChoiceQuestion();
      ^
  symbol:   class ChoiceQuestion
  location: class QuestionDemo3
QuestionDemo3.java:13: error: cannot find symbol
      ChoiceQuestion second= new ChoiceQuestion();
                                 ^
  symbol:   class ChoiceQuestion
  location: class QuestionDemo3
5 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15743
  • View blog
  • Posts: 63,068
  • Joined: 12-June 08

Re: Polymorphism

Posted 18 May 2020 - 08:32 PM

Look where the caret is pointing in each of those errors: Question and ChoiceQuestion.

Looking at your code there is no reference (import) to those classes.
Was This Post Helpful? 0
  • +
  • -

#8 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: Polymorphism

Posted 18 May 2020 - 10:57 PM

View Posthellswindstaff, on 18 May 2020 - 11:43 PM, said:

How often is polymorphism used?


Really quite a lot. Of course, polymorphism comes from inheritance which means an "is-a" relationship (so if class B inherits from class A, B is an A). What this means is that anywhere I need to use an A (e.g. as a parameter to a function), I can also use a B. Why is that useful? Well, here's one important case: we need to write automated tests for our software* and say a function we want to test uses a class that communicates with some service on the Internet somewhere. That can make our testing harder, because the tests may be fragile, or costly, or whatever else. Instead, what we choose to do is use a fake implementation that is completely under our control - it perhaps doesn't even need to make network calls, but just return some canned data from its methods. The way we do this is by having a common superclass (or interface) with two subclasses (one for the production code that actually talks to that service over the network and one for tests that doesn't) and then having our function taking an instance of that superclass as a parameter.

This whole thing about being able to swap in different implementations of a thing is really quite powerful and useful not just for tests! In production code, one thing it helps is to allow you to extend the software without having to change too much. OK, I'm being quite light on details really, but the point is as before: if I have a common superclass for something, the function or class that uses it will be able to work with any subclasses without knowing. Today, I might need a thing that formats rows of data separated by tabs, but tomorrow I might need to separate things with commas**. This is quite a trivial example, but real world software has to change a lot so we use techniques like this to help make that easy.


* The benefits of automated testing can be read about elsewhere.
** I have a toy example somewhere that might need some cleaning up, but I can share that if it helps.

This post has been edited by ndc85430: 18 May 2020 - 11:26 PM

Was This Post Helpful? 1
  • +
  • -

#9 hellswindstaff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 27-February 12

Re: Polymorphism

Posted 22 May 2020 - 01:59 PM

View Postndc85430, on 18 May 2020 - 10:57 PM, said:

View Posthellswindstaff, on 18 May 2020 - 11:43 PM, said:

How often is polymorphism used?

** I have a toy example somewhere that might need some cleaning up, but I can share that if it helps.


I'd love to hear about this toy example.

Thanks for responding for you and modi! :bananaman:
Was This Post Helpful? 0
  • +
  • -

#10 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: Polymorphism

Posted 22 May 2020 - 11:45 PM

The toy example is here. It was an exercise I'd written for a student, so what you're seeing is my solution for it. The ContactsApplication class takes a RowWriter as a constructor parameter. RowWriter is an interface and in main, a different implementation (i.e. either a CommaRowWriter or a TabRowWriter) is passed in depending on what the user inputs. The ContactsApplication class is completely unaware of what kind of RowWriter it's given.

I also remembered that I'd written a C++ example showing the same idea, but with a function and unit tests as I described above, though instead of doing something over the network, I've chosen to allow the function to write a string somewhere. The "somewhere" is controlled by a parameter to the function: in the production code, that is the standard output stream, but the tests write to an in memory string. That example can be found here.

This post has been edited by ndc85430: 22 May 2020 - 11:46 PM

Was This Post Helpful? 2
  • +
  • -

#11 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 06-June 20

Re: Polymorphism

Posted 10 June 2020 - 12:01 AM

Polymorphism is simply a new instance class with more than one constructer recognisable by each constructer having different arguments to them and a are not a no argument constructer.
One more feature and probably its main clause is the constructors set different or more or less "instance variables" that change the state of or form of the class "type of Object it specifically is" making a subtle variation of its kind either by functionality of parsing different arguments to it or by what extra is assigned or stored available in the instance variables by having a choice or way to create the "variant type" of that class.
Was This Post Helpful? -1
  • +
  • -

#12 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: Polymorphism

Posted 10 June 2020 - 01:24 AM

View Postnicephotog, on 10 June 2020 - 08:01 AM, said:

Polymorphism is simply a new instance class with more than one constructer recognisable by each constructer having different arguments to them and a are not a no argument constructer.


This is just wrong.
Was This Post Helpful? 0
  • +
  • -

#13 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 06-June 20

Re: Polymorphism

Posted 10 June 2020 - 02:38 AM

@#12ndc85430 NO, It's only half right but none the less right.
To creat ONE instance of a class (a complex type) is an INSTANCE.
To create an instance of a class by extending another means there are TWO tyoes so that extension is Polymorphic IF it is not abstract (abstract classes are implementation not instances therefore cannot be constructed).
However , to create an instance, the PARTICULAR instance variables of a class (globals assigned from the constructor and class variables) MUST BE ASSIGNED to define THE SPECIFIC TYPE OF INSTANCE (constructor overloading in Java - some OOP languages Polymorphism).
therefore - ONE CLASS TEMPLATE CONTAINING MORE THAN ONE CONSTRUCTOR changes the the properties of the Object sufficiently AS AN INSTANCE.
I just found that in java it's referred to as overloading constructors WHEREAS in some OOP language and concepts its referred to as polymorphism.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11958
  • View blog
  • Posts: 20,289
  • Joined: 19-March 11

Re: Polymorphism

Posted 10 June 2020 - 07:34 AM

View Postnicephotog, on 10 June 2020 - 02:01 AM, said:

Polymorphism is simply a new instance class with more than one constructer recognisable by each constructer having different arguments to them and a are not a no argument constructer.
One more feature and probably its main clause is the constructors set different or more or less "instance variables" that change the state of or form of the class "type of Object it specifically is" making a subtle variation of its kind either by functionality of parsing different arguments to it or by what extra is assigned or stored available in the instance variables by having a choice or way to create the "variant type" of that class.


If I could make sense of this, I would probably say it's wrong, but I'm not sure I even understand it well enough to say more than "it's nonsense".

As best I can follow, you're talking about overloaded method signatures. Overloaded method signatures (including constructors) can happen without polymorphism, and polymorphism can happen without overloaded method signatures, so the two are not related. However, if you're talking about something else, please try to clarify what you actually mean.

Also, please note that using emphatic styling markup and capitalization does not make your arguments more readable or more clear or more correct.
Was This Post Helpful? 0
  • +
  • -

#15 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 06-June 20

Re: Polymorphism

Posted 10 June 2020 - 06:36 PM

First, to save extracting the point and for everyone elses benefit to prevent erroneous understanding of polymorphism You are right what polymorphism is in basis, an object created from more than one class type such as extending from a class and thus inheritance of the other type properties.
There is some relationship by what a constructor does, it sets the global values that define the properties the class has at instantiation. It also is a clause whether the class can be cast to another class of new instance operation mechanism because the constructors of the two must be identical of arguments and arguments sequence to the constructor. The global fields that are assigned the moment the constructor exits whether assigned or declared are all deemed part of the constructor and are the governing "definition" of the class. Changing , destroying or omitting some of them from use by overloading constructors changes the definition of the class while instantiated though the name remains the same.
You cannot cast (superclassing inheritance - polymorphism) constructed classes that take arguments that are unalike, hence the utility point interfaces and abstract classes to overcome some of this and up casting to Object class so another cast can occur.
Overloaded constructors are important to watch for in casting polymorphism, but also the internal definition of it from its globals.
I'd hate to see or hear someone trying to cast objects from that simplification you have called inheritance.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2