Very cool! +rep
Thread: Polymorphism |
Hello today, Codecall !
I will present today my part of lesson with a last tutorial for a loooong while.
Polymorphism.
So let's go on...
Packages
This package will be our own, which means your folder you save the files have to be called Polymorphism. Polymorphism provides the Java coder the wondrous ability to have fun with overloading things.Code:package Polymorphism;
Main class and constructor
Nothing much, just giving the ability to have a main frame for your code. You could call it a template for future work...Code:public class Forum { Forum() { }
main
The main, nothing much to point out or tell about.Code:public static void main(String[] arg) {
Instances
This part is where the things are happening. While referring to the human class we will have to give some commands. By giving it a so called "String" name. It's really easy then to see what is happening if you want to have nothing or with a string name... So by creating handles we can get different things for instance. We can use whenever we like.Code:Human[] H = new Human[4]; H[0] = new Human("I ist BOT"); H[1] = new English("Jake"); H[2] = new Swedish("Bosse"); H[3] = new Spanish("Joseu"); System.out.println("Welcoming from around the world"+"\n"); for(int i=0; i<H.length; i++) { H[i].talk(); } } }
Super class.(This is not to be confused with superclass)
As human will represent the rest classes we have, such as sweden/spanish/english. We will have to make methods that will be simple to follow. Making it easy for us to close and open various functions, such as closed instance methods inside packages or open instance methods from other packages or available for other...Code:package Polymorphism; public class Human { String Name; Human() { } public Human(String name) { this.Name = name; } public void talk() { System.out.println("Teh bot: "+Name); } }
English class
Now we are inheriting ever thing from the so while referring to the same class instance method we also overload the old one. So we are not replacing it but we are overloading it with another one. Most likely to do like this. Put a plate with food on another plate with food.Code:package Polymorphism; public class English extends Human { English() { } public English(String in) { super.Name = in; } public void talk() { System.out.println("Hey my name is "+Name); } }
Spanish class
Same as the english part, we are inheriting from Human, and overloading the talk method.Code:package Polymorphism; public class Spanish extends Human { Spanish() { } public Spanish(String in) { super.Name = in; } public void talk() { System.out.println("Hola jouso "+Name); } }
Swedish class
So here comes swedish partCode:package Polymorfism; public class Swedish extends Human { Swedish() { } public Swedish(String in) { super.Name = in; } public void talk() { System.out.println("Hej jag heter "+Name); } }, well same as the rest inheriting the instances and reference we overload for fun.
Output
Here is our output of the thing...Code:Welcoming from around the world Teh bot: I ist BOT Hey my name is Jake Hej jag heter Bosse Hola jouso Joseu
To summarize everything from now. Polymorphism allows user to override and overload simultaneously as well objecting instance methods and instance variables. Overriding a method allows users to change the whole concept of the originate method while overloading one is to apply a self made one identical from the original to the current one you making. So thus you can add different methods while using the same concept. Our human can talk, however our swedish person can sing out from the talk method. So generally these types of learning are needed when working with multiply classes and you want to have some control of them but still want them to be unique. Like a food store or corner menu list. You choices overrides every time you change your mind...
Cheers !
![]()
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Very cool! +rep
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Awesome!
+rep
Nice.
Inheritance can make things really easy but I don't quite see the point of overloading methods. The example above inherits from variables and then redefines the talk method.
So then why not just define the talk method in each class. What is the point of overriding methods?
@chili5: He's showing that they can be overridden using method overloading. He needs to use overloaded methods because if he didn't, the "Human" object wouldn't have a "talk" method, in which case he couldn't call the talk method when referring to human objects, even if they are derived forms of human objects. The for loop is why he used overloading, however he did it in a similar way one would use interfaces, he didn't really need to use that, but he wasn't teaching interfaces.
@Turk4n: Excellent work here. This leads me, naturally, to a question on the subject of method overloading: It would seem that methods are always able to be overloaded, similarly to how C++ would use the "virtual" keyword. In that case, is it possible to prevent a client programmer from using a polymorphed version of a method in your classes? For example, in C++ if you do not use the "virtual" keyword, when the object is referred to by it's base class, the function will not polymorph even if you DID overload it (see my C++ tut for clarification).
Oh, and I give you my measely +rep!![]()
Should I get a userbar here?
Very ******* cool.
Being Poor Sucks
Turk4n Rocks
Thank you
The choice from using same method but you can add in some extra things.Extra instances or variables. Such as when talking, Human will be talking and English can talk to, however when talking they will ask you. Which isn't inside with human.
Thank you, I will view your good read
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FANKKKKKKKKKKKKKKKKS![]()
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Wait, I wanted to know...In that case, is it possible to prevent a client programmer from using a polymorphed version of a method in your classes?
Should I get a userbar here?
Hatsune Miku ~❤❤❤
初音ミク。~❤❤❤
Yes, you must declare a method final, so i can't be overwritten.
By declaring the whole class final, it can't be extended at all.
My teacher teached "the security hole" by programming a Door, that could be opened by certain Key, and then extended a Superkey from it, which could open any Door.
Code:final public class NoExtendingAtAll { public void method(){ } } public class Someclass { final public void noOverwritingMethod(){ } }
There are currently 1 users browsing this thread. (0 members and 1 guests)