Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Polymorphism

polymorphism

  • Please log in to reply
20 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 23 August 2009 - 11:50 AM

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
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 Joseu
Here 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:
  • 3

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 23 August 2009 - 02:09 PM

Very cool! +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 23 August 2009 - 03:02 PM

Awesome! :)
+rep
  • 0

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 23 August 2009 - 04:47 PM

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?
  • 0

#5 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 23 August 2009 - 05:08 PM

@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! :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 relapse

relapse

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 323 posts

Posted 23 August 2009 - 05:38 PM

Very ******* cool.
  • 0

#7 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 23 August 2009 - 08:48 PM

Very cool! +rep

Thank you :D

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?

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.

@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! :)

Thank you, I will view your good read :)

Very ******* cool.


FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFANKKKKKKKKKKKKKKKKS :amr:
  • 0

#8 ZekeDragon

ZekeDragon

    CC Leader

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1263 posts

Posted 23 August 2009 - 09:15 PM

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?


  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 23 August 2009 - 10:08 PM

Wait, I wanted to know...


Yes, if call the class.
MyClass MyC = new MyClass();
By this you will only use the method and it's own content meaning you can create a new method but have no connections to the method that might resident in MyClass...
Ok, or was I unclear :X?
  • 0

#10 Sinipull

Sinipull

    CC Addict

  • Validating
  • PipPipPipPipPip
  • 384 posts

Posted 25 August 2009 - 02:27 PM

Wait, I wanted to know...


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. :P

final public class NoExtendingAtAll {       
    public void method(){
    	
    }   
}

public class Someclass {       
    final public void noOverwritingMethod(){
    	
    }   
}

  • 0

#11 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 25 August 2009 - 08:53 PM

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. :P

final public class NoExtendingAtAll {       
    public void method(){
    	
    }   
}

public class Someclass {       
    final public void noOverwritingMethod(){
    	
    }   
}


Great to hear.
  • 0

#12 unscripted_1

unscripted_1

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 23 April 2010 - 01:09 AM

Very helpful tut!

I would like to ask, do I have the correct understanding of polymorphism? Here's what I understand:

first, I create a class called Human, which has a generic talk() method:

public class Human {

   public void talk() {

      System.out.println("I'm a generic Human!");

   }
}
I can then create EXTENSIONS of the Human class, which override the talk() method in Human.java... This Child class does this:

public class Child extends Human {

   public void talk() {

      System.out.println("I'm a Child Human!");

   }
}
& just for good measure here's an Oldie class:

public class Oldie extends Human {

   public void talk() {

      System.out.println("I'm an ancient Human!");

   }
}
OK! Now we're ready to use our polymorphed Human classes in our program... Here's the example:

public class Tester {

   public static void main(String[] args) {

      Human[] myPeople = new Human[3];

      myPeople[0] = new Human();
      myPeople[1] = new Child();
      myPeople[2] = new Oldie();

      // lets test their overloaded methods

      for (int i = 0; i < myPeople.length; i++) {

         myPeople[i].talk();

      }

   } // end main

} 
Again, thanks for the tutorial, it was very instructive!
  • 0





Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download