Details

May 22
Link

Statistics

Replies: 34

programming concepts

:iconilusienns:
ilusienns Featured By Owner May 22, 2020
can someone explain to me simply what are programming concepts like polymorphism and inheritance and how do they work or differ compared to simple if else or for loop?

tx
Reply

Devious Comments (Add yours)

:iconuwumoeo-ouwu:
UwUMoeO-OUwU Featured By Owner 2 days ago  Hobbyist Digital Artist
So, I've just started Devianart and I was wondering if any of you could check out my gallery. I mostly draw in an anime art style and draw characters and real people alike. Here is the link to my page: www.deviantart.com/uwumoeo-ouw… I will really appreciate if you added some of my art to favorites or started watching me too.
Reply
:iconilusienns:
ilusienns Featured By Owner May 26, 2020
whats this
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner May 23, 2020  Hobbyist General Artist
Polymorphism isn't really one specific thing but rather a recurring theme that takes different forms.  The theme is that it's convenient when different things can be treated the same and it just works without having to worry about the difference.

When you call print and pass it a string or an integer or whatever without having to call a dedicated "print_string" or "print_integer" method, that's a kind of polymorphism.  The specific kind of polymorhphism depends. 

Sometimes it's ad hoc polymorphism where there are different functions all named print but with different signatures.  Overloading is an example of this.

Sometimes it's parametric polymorhphism where a component is able to pass the difference through to somewhere else to handle it using some other mechanism without having to deal with the difference itself.  Generics, templates, and type unions are examples of this.

Sometimes its subtype polymorphism where the different behaviour needed to handle the situation is bundled with the things that need that handling in a way that it can be invoked without knowing which of them it is.  Interfaces, subclassing, and duck typing are examples of this.
Reply
:iconilusienns:
ilusienns Featured By Owner May 23, 2020
uh sorry whats example of generic
,templates and interface again?
is this common in game programming or...?
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner Edited May 24, 2020  Hobbyist General Artist
Parametric polymorphism depends on particular features of the language and some languages don't have any of those mechanisms, particularly those that don't use strong type checking.

C++ has templates.  std:vector<int> is a vector of integers.  Without parametric polymorphism, this would either need to define a separate vector class for each type it can hold, or vectors would be able to hold anything but there would be no information about what they are holding to ensure type safety.

Java has generics, which look similar but work differently under the hood.  Collection<Integer>  is a Collection of Integer objects.  

The difference between templates and generics is that templates create entirely new entities (classes, functions, etc).  std:vector<int> is a different class from a std:vector<float>.  Generics only apply at the level of type checking though and then are thrown away.  ArrayList<Integer> and a ArrayList<Float> are the same ArrayList class but the compiler can look at the type parameter to make sure everything lines up and from then on they are just ArrayList objects without any type parameter.

A C++ vector can only hold the kind of thing its type parameter specifies.  A Java ArrayList can hold anything, but the compiler will try to prevent you from putting in anything that doesn't match its type parameter.

In a language like Ruby or Python that doesn't do this kind of type checking, the entire concept of parametric polymorphism becomes meaningless.
Reply
:iconilusienns:
ilusienns Featured By Owner May 24, 2020
type checking?
what do u mean by type checking?
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner May 24, 2020  Hobbyist General Artist
If you have a method 

doStuffToAnInteger(int x)

and you try to call it with a string

doStuffToAnInteger("not an integer")

The compiler will not let you. That's type checking.  Some languages do it, some don't.  Some languages even make it optional.  It helps you detect mistakes sooner, but it can also get in the way at times.
Reply
:iconilusienns:
ilusienns Featured By Owner May 24, 2020
Wow, that's so high.... like i guess the program can then call the notinteger as integer???
wont that like, crash or error? or can you still process it?
python and ruby dont do that? thats weird
i thought even basic detect something as minor as that...
do you have example of this?
but then, using polymorphism, how do you process an non integer as integer? makes no sense...
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner Edited May 25, 2020  Hobbyist General Artist
No, type checking means that the problem is noticed before the program even gets to run.  It never gets the opportunity to crash.  It's usually much easier to deal with compiler errors than with runtime errors so if you can turn runtime errors into compile time errors, that's a good thing.  Type checking is one way to do this.

That's my point, doStuffToAnInteger("not an integer") will cause a compile time failure.

As for polymorphism.

Ad hoc polymorphism with overloading would be like this (example is Java)

Imaging you have a class with these methods.
void doStuff(int x) {/* do something with an integer */}
void doStuff(String x) {/* do something with a string */}


Then on an instance o of that class you could call

o.doStuff(1);
o.doStuff("a string");


But the following would be a failure

o.doStuff(1.5); // Compiler error

Using the declared types in the signatures of the two methods, the correct one can be determined and used.  In most languages this is done at compile time not runtime so it cares about the declared type of the value being passed in.  So if you passed a String as an Object, it would not be recognized.

o.doStuff((Object)"A string cast as an object"); // Compiler error


You can make dynamic languages do something similar this way (Example is Ruby):

def do_stuff(x)
  case x
  when String: 
    # do something with a string
  when Integer
    # do something with an integer
  else
    raise "Unexpected value, x should be a String or an Integer"
  end
end


But that happens at runtime.  It wan't fail until the program is running and do_stuff is called with a value that is not a String or Integer

You could also use subtype polymorphism

void doStuff(Object x) {
  String s = x.toString();
  /* do stuff with string s */
}


This accepts the Object class which defines the toString method which then gets used to do whatever it does.

o.doStuff(1); // the int 1 gets "boxed" as an Integer which is a subclass of Object and has a toString which turns it into a decimal representation of the number.
o.doStuff("A string"); // String is a subclass of Object and its toString method just returns itself because it's already a string.
o.doStuff(new MyClass()); // MyClass is a subclass of Object and either defines its own toString which gets used, or the default is used which provides some information useful for debugging


Object oriented dynamic languages can do this as well.

def do_stuff(x)
  s=x.to_s // all Ruby objects define to_s
  # do something with s
end


It doesn't have to use the Object class and it doesn't have to use converting them to strings.   You could define your own class or interface, or for a duck typed language like Ruby it could just be a set of well known methods you expect. You can then then write against that common expectation and anything that implements it can be fed in regardless of the internal details of how it implements it.  That's subtype polymorphism.

For an example of parametric polymorphism using Java generics here's how a list works.

List<Integer> theList = new LinkedList<Integer>();

The LinkedList class can store a list of any sort of object but because the type parameter is set to Integer, it will be a compile time error if you try to add anything but an Integer to it.

theList.add(1); // Just fine
theList.add("a string"); // a compiler error

And when you try to get something from the list, it will be typed as an Integer.  No need to know that and cast it to Integer before using it.

Integer x = theList.get(0);

But you can also make lists of any other type by specifying a different type parameter.  That's parametric polymorphism.

List<String> anotherList = new LinkedList<String>(); // a list of Strings
List<List<List<MyClass>>> yetAnotherList = new LinkedList<List<List<MyClass>>>(); // A List of Lists of Lists of MyClass objects
Reply
:iconilusienns:
ilusienns Featured By Owner May 25, 2020
wow it seems more of programming language design than...designing/constructing/putting together a program itself
ah yeah, very cool so you can write system error/runtime error/unexpected runtime yadayada thingy wow...
is this usually planned ahead in language programming?
because i recall there's alot of this happening. and it seems endless.
so am i correct to say that polymorphism then is not very common since each type is, not interchangeable in a sense?
Reply
(1 Reply)
:icon2357111317192329:
2357111317192329 Featured By Owner May 22, 2020  Hobbyist General Artist
Those concepts are on different levels of abstraction. If else and loops are low level concepts and are about what the program does. Inheritance and polymorphism are higher level concepts and their purpose is to add structure to the code so it's easier to read by a human, so it doesn't make sense to try to understand them in terms of what the program does. Hope this helps.
Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
how is it easier to read by human then?
im utterly confused by the existence of it...
Reply
:icon2357111317192329:
2357111317192329 Featured By Owner May 22, 2020  Hobbyist General Artist
Yeah, the ideas really don't make much sense until you've worked on a very large scale project, ideally with a team. It might make sense to just ignore them for the moment if they seem pointless, because they probably are.

To give you an idea though, I'm sure you've seen functions/methods, where the idea is that if you have the same piece of code you want to execute at multiple places, you would call the function instead of writing the code over and over again. This would make your code shorter, and you could give the function a meaningful name, like "SortListAlphabetically" which would be easier to understand than the actual code that does it.

Analogously, there are often cases where you might want to run "similar" pieces of code, which aren't exactly the same. This is where these higher level concepts come in. You can think of them as ways of grouping "similar" pieces of code together so you can immediately understand what that piece of code is supposed to be doing.
Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
...it's basically writing a new code? like a new class?
or referring back to a variable?
Reply
:icon2357111317192329:
2357111317192329 Featured By Owner May 22, 2020  Hobbyist General Artist
I'm not sure what you mean, could you specify? I was referring to "high level concepts" in general, but I'd be happy to answer any specific questions.
Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
thats whatyou said?
like abrevation or mathemathically assign a letter or name to a variable to recall the particular funxtion again?
isnt that what you said?
so polymorphism and inheritance is that?
naming a set of code or function you made to call it again?
thats like defining a new class isnt it? like def class? thats it?
Reply
:icon2357111317192329:
2357111317192329 Featured By Owner May 22, 2020  Hobbyist General Artist
I was trying to give a general idea of what these things are meant to do, and I think you got the general idea. I can get into a bit more detail though.
Polymorphism: you can think of these as annotations you put on classes that guarantees that your objects will be able to do certain things. One way this is useful is that you won't have to look at the code of that class to understand how to use it.
Inheritance: you can think of this as a glorified way of writing a new class that contains an object of the old class as a class variable. On top of the obvious use, you get a lot of the same benefits of polymorphism.

Again, I'm not sure of these concepts are going to be of any use, so I wouldn't worry too much about them. I'm sure everything will click when you come up against the problem that they are intended to solve.
Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
well the explanation you gave kinda eases me... if it s correct.
and if its correct then i guess the entire programming idea revolves in the same way how math works that is variable and functions and referring back to older functions....
not sure how polymorphism work still but inheritance seems to suggest so....
guess im the kind who thinks before they click supposedly.
thank you.
Reply
(1 Reply)
:iconpyrohmstr:
pyrohmstr Featured By Owner May 22, 2020  Professional Artist
The best skill you can learn while trying to learn programming is using Google - specifically to use Google to find Stack Overflow answers. For instance: stackoverflow.com/questions/63…

Polymorphism and inheritance have nothing to do with a for loop. Totally different things.

Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
Well in my language there's an old saying that
"those who's shy to ask will lose their way"
but i guess that existed before google lol
just wondering if there's an easier explanation than that(i've seen it) since english is not my first and im not academically trained to comprehend that so i'll try not to make assumptions orz
Reply
:iconpyrohmstr:
pyrohmstr Featured By Owner May 22, 2020  Professional Artist
That stack overflow link is actually the best I've ever seen it explained. I can't think of a simpler way. I would suggest maybe trying to find a book or class in your native language.

Programming is not simple to understand. Most concepts really can't be distilled down and the only way to learn them is to actually program things and work hard to build understanding. Run code. If you want to see polymorphism vs inheritance you need to make some classes, some methods, and see what happens when you do stuff. This is where an organized class like codecademy is useful because they'll walk you through it and give you examples you can run and tinker with. 
Reply
:iconilusienns:
ilusienns Featured By Owner May 22, 2020
Hmmm, i wonder...
i thought it would be like game programming and designing ...where
game objects are causing things towards each other as the user plays it, creating gameplay and experience...
Something like 1 sentence short like that, of "What" doing what and so on...
Reply
Add a Comment: