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 !
![]()


LinkBack URL
About LinkBacks



, well same as the rest inheriting the instances and reference we overload for fun.





Reply With Quote











Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum