Results 1 to 5 of 5
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default SuperClass methods

    Hello all.. i am working on a project right know and i face up a strange problem with my class...

    to make my question more easy i create a small program if the problem i face up..

    here is the code :
    Java Code:
    import java.util.ArrayList;
    
    class player{
    	protected String name;
    }
    
    class Human extends player{
    	
    	public Human(){
    		name = "Human";
    	}
    
    	public void Print(){
    		System.out.println(name);
    	}
    }
    
    class Computer extends player{
    	
    	public Computer(){
    		name = "computer";
    	}
    
    	public void Print(){
    		System.out.println(name);
    	}
    }
    
    class test {
    	public static void main(String args[]){
    		ArrayList<player> list = new ArrayList<player>();
    		Human humanObject = new Human();
    		Computer computerObject = new Computer();
    		list.add(humanObject);
    		list.add(computerObject);
    		for(int i = 0 ; i < 2 ; i ++){
    			list.get(i).Print();
    		}
    	}
    }
    when i run this i get this error :

    test.java:37: error: cannot find symbol
    list.get(i).Print();
    ^
    symbol: method Print()
    location: class player
    1 error

    this mean that i should declare the Print method inside the Super Class i did this change and the program work fine..

    Java Code:
    import java.util.ArrayList;
    
    class player{
    	protected String name;
    	
    	public void Print(){}
    }
    
    class Human extends player{
    	
    	public Human(){
    		name = "Human";
    	}
    
    	public void Print(){
    		System.out.println(name);
    	}
    }
    
    class Computer extends player{
    	
    	public Computer(){
    		name = "computer";
    	}
    
    	public void Print(){
    		System.out.println(name);
    	}
    }
    
    class test {
    	public static void main(String args[]){
    		ArrayList<player> list = new ArrayList<player>();
    		Human humanObject = new Human();
    		Computer computerObject = new Computer();
    		list.add(humanObject);
    		list.add(computerObject);
    		for(int i = 0 ; i < 2 ; i ++){
    			list.get(i).Print();
    		}
    	}
    }
    so there is any way to do this without declare the Print Method inside the Super class?? i am asking because in my project i have

    one main class Player and two subclasses ComputerPlayer and HumanPlayer and i have different type of methods ...and all the objects are in an ArrayList<Player>

    thx in advance

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: SuperClass methods

    Why not make an interface that has the print()method in it. Then implement the interface in whatever class needs it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: SuperClass methods

    the code above it's now my actual project..that was just an example...

    i have to make one Super class Player and two Subclasses ComputerPlayer and UserPlayer
    now in ComputerPlayer and in UserPlayer i have two differents methods move()

    for example the move in UserPlayer is :

    Java Code:
    public void move(Cell [][] map){
    		int randomChoice = randomGenerator.nextInt(movements.length);
    		String nextMove = movements[randomChoice];
    		boolean flag = false ;
    		while(!flag){
    			if (nextMove.equalsIgnoreCase("Right")){
    				if ( positionY < MAX_POSITION) {
    					map[positionX][positionY].removeElement();
    					positionY ++ ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else if (nextMove.equalsIgnoreCase("Left")){
    				if(positionY > MIN_POSITION){
    					map[positionX][positionY].removeElement();
    					positionY -- ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else if (nextMove.equalsIgnoreCase("Down")){
    				if(positionX < MAX_POSITION ){
    					map[positionX][positionY].removeElement();
    					positionX ++ ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else{
    				if (positionX > MIN_POSITION ){
    					map[positionX][positionY].removeElement();
    					positionX -- ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}
    		}
    	}
    and the code in UserPlayer is :

    Java Code:
    public void move(Cell [][] map){
    		System.out.print("where you want to go (UP-DOWN-RIGHT-LEFT) :");
    		String answer = input.next();
    		boolean flag = false ;
    		while(!flag){
    			if (answer.equalsIgnoreCase("Right")){
    				if ( positionY < MAX_POSITION) {
    					map[positionX][positionY].removeElement();
    					positionY ++ ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else if (answer.equalsIgnoreCase("Left")){
    				if(positionY > MIN_POSITION){
    					map[positionX][positionY].removeElement();
    					positionY -- ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else if (answer.equalsIgnoreCase("Down")){
    				if(positionX < MAX_POSITION ){
    					map[positionX][positionY].removeElement();
    					positionX ++ ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}else{
    				if (positionX > MIN_POSITION ){
    					map[positionX][positionY].removeElement();
    					positionX -- ;
    					map[positionX][positionY].setElement(this);
    					flag = true ;
    				}
    			}
    			if(!flag){
    				System.out.print("You try to move out of the map..Try again :");
    				answer = input.next();
    			}
    		}
    	}
    as you can the methods move in each subclass is different so i can call them inside an ArrayList<Player> list = new ArrayList<Player>();
    i got the error above... so i must declare the move method in SuperClass or there is onother way ???

    thx you for your first reply..

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,777
    Rep Power
    5

    Default Re: SuperClass methods

    Why not make your Player class an abstract class. In your first example it would be like this:

    Java Code:
    abstract class Player {
        protected String name;
        abstract protected void Print();
    }
    Then just keep your Print implementations in the Human and Computer classes. This should do what you want. Then you can adapt the solution to your real implementation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

Similar Threads

  1. Why cant I use methods from the superclass?
    By EscSequenceAlpha in forum New To Java
    Replies: 5
    Last Post: 05-11-2012, 10:02 PM
  2. Replies: 11
    Last Post: 03-05-2012, 11:18 AM
  3. Override a superclass's methods with a subclass
    By zach&kody in forum New To Java
    Replies: 7
    Last Post: 05-24-2011, 02:50 PM
  4. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  5. Invoking superclass methods... how?
    By rhobincu in forum New To Java
    Replies: 7
    Last Post: 08-09-2007, 03:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •