I will present today my part of lesson with a last tutorial for a loooong while.
Polymorphism.
So let's go on...
Packages
package Polymorphism;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. :P
Main class and constructor
public class Forum {
Forum() {
}
Nothing much, just giving the ability to have a main frame for your code. You could call it a template for future work...
main
public static void main(String[] arg) {
The main, nothing much to point out or tell about.Instances
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();
}
}
}
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.Super class.(This is not to be confused with superclass)
package Polymorphism;
public class Human {
String Name;
Human() {
}
public Human(String name) {
this.Name = name;
}
public void talk() {
System.out.println("Teh bot: "+Name);
}
}
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...English class
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);
}
}
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.Spanish class
package Polymorphism;
public class Spanish extends Human {
Spanish() {
}
public Spanish(String in) {
super.Name = in;
}
public void talk() {
System.out.println("Hola jouso "+Name);
}
}
Same as the english part, we are inheriting from Human, and overloading the talk method.Swedish class
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);
}
}
So here comes swedish part :D, well same as the rest inheriting the instances and reference we overload for fun.Output
Welcoming from around the world Teh bot: I ist BOT Hey my name is Jake Hej jag heter Bosse Hola jouso JoseuHere is our output of the thing...
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 !
:amr:


Sign In
Create Account



Back to top









