Can anyone help me with this java problem? Here's the problem:

"Write some code to demonstrate inheritance polymorphism. Create a superclass class with 3 subclasses. The superclass should have a method that prints out a line identifying the current class (something like "I am a Superhero"). Two of the subclasses should override this method to print out a different message (like "I am Wolverine"). Give the superclass a main() method with an array of size 4, typed as the superclass (for example, Monster[] monsters = new Monster[4];). Your main() should populate the array with references to 4 objects, each with a different class, and then traverse the array, calling your method on each array component."

I need some idea... dont know where to start though..

By writing a class with a main method and another method, and three other classes that extend that class and override that "other" method. In the main you create four references to the original class by creating an instance of each of the four classes you've created than call the "other" method on all four instances.

Just the instructions in other words. The instructions are pretty clear. Make an attempt and post that code and we will help you to correct it.

By writing a class with a main method and another method, and three other classes that extend that class and override that "other" method. In the main you create four references to the original class by creating an instance of each of the four classes you've created than call the "other" method on all four instances.

Just the instructions in other words. The instructions are pretty clear. Make an attempt and post that code and we will help you to correct it.

Is this correct?

public class InheritanceTry //super class
{
	public void Superhero()
	{
		System.out.println("I am a BATMAN");
	}
	
}
public class Superhero2 extends InheritanceTry { //SUBCLASS 1

	public void Superhero() {
	 System.out.println("I am WOLVERINE");	
	}

}
public class Superhero3 extends InheritanceTry //subclass 2
    { 
	
	public void Superhero() 
	{
		 System.out.println("I am SPIDERMAN"); //overrides the Superhero//	
	}
	
}
public class Superhero4 extends InheritanceTry { //subclass 2
	//statement Superhero() is inherited..//
}
public class QuizFeb1_JonathanBesinan  //main
{
	
	public static void main(String[]args)
	{
		InheritanceTry [] hero =new InheritanceTry[4]; 
		 												   												    
		hero[0]=new InheritanceTry();  
		hero[1]=new Superhero2();	   	
		hero[2]=new Superhero3();	   	
		hero[3]=new Superhero4();	   
		
		
		System.out.println("~,.,~SUPERHEROES~,.~");
		for(int x=0; x<4; x++)
		{
			hero[x].Superhero();
		}
		
	}	
	
		
}

Output:
~,.,~SUPERHEROES~,.~
I am a BATMAN
I am WOLVERINE
I am SPIDERMAN
I am a BATMAN

Well, it's doing what you want, right? Except for the last of course where the method has not been overriden.

Well, it's doing what you want, right? Except for the last of course where the method has not been overriden.

oh no.. what should i do? help me

Uhm, override the method?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.