I can't seem to get my access to the private xCoor and yCoor. I try couple of things i just learn in school but none of em make sense. Can someone explain and tell me a way to get access to those xCoor, yCoor, width, and height while still leaving them private in class snowperson?

import java.awt.*;

class snowperson
{
	private int xCoor;
	private int yCoor;
	private int width;
	private int height;
	
	public snowperson()
	{
		xCoor = 0;
		yCoor = 0;
		width = 0;
		height = 0;
	}//end default
	public snowperson(int x, int y, int wid, int hght)
	{
		xCoor = x;
		yCoor = y;
		width = wid;
		height = hght;
		
	}//end default	
	public int getX()
	{
	   return xCoor;
	}
	public int getY()
	{
	   return yCoor;
	}
	public int getH()
	{
	   return height;
	}
	public int getW()
	{
	   return width;
	}
	public void drawSnowBottom(Graphics g)
	{
		g.fillOval(xCoor,yCoor,width, height);
	}//end draw snow bottom
	public void drawSnowMiddle(Graphics g)
	{
	 g.fillOval(xCoor+19, yCoor-128,width-40, height-40);
	}//end draw snow middle
	public void drawSnowTop(Graphics g)
	{
	  g.fillOval(xCoor+50, yCoor-211, width/2, height/2);
	}//end draw snow top
	
	public void drawSnowperson(Graphics g)
	{
		g.setColor(Color.white);
		drawSnowBottom(g);
		drawSnowMiddle(g);
		drawSnowTop(g);
	}//end draw snow person	
}//end class SnowpersonAG

class snowSanta extends snowperson
{	
	public void drawHat(Graphics g)
	{
		g.setColor(Color.red);
		int xHat[] = {xCoor+50, xCoor+150, xCoor+160, xCoor+170, xCoor+160, xCoor+150, xCoor+125, xCoor+100, xCoor+75};
    	int yHat[] = {yCoor-200, yCoor-200, yCoor-180, yCoor-225, yCoor-250, yCoor-260, yCoor-250, yCoor-225, yCoor-215};
    	int nHat = 9;
    	g.fillPolygon(xHat, yHat, nHat);
	}
}//end classs snowSanta

Here are three solutions:
1. Make your instance variables (xCoor, yCoor etc.) public that way the subclass can see them and use them however it wants.

2. Use the getters and setters methods, you have them there for a reason, ;) though you are going to need an instance of you parent class in the subclass in order to use them in the subclass.

3. This one I would avoid if you have never used them, but make your second class an inner class of your first class, that way you can keep your original instance variables private yet you'll still be able to access them in your second class and you won't have any need for the setter and getter methods.

Honestly if I were you I would go with the first option its the simplest solution though you should learn to use the setter and getter methods since you have them in your first class. Hope that helps let me know if you don't understand anything. :)

Option 2 would be the standard "good O.O." way of doing it. Zetlin is not quite right - you define those methods in the parent class and the subclass will inherit them, and can call them directly. You do NOT need an instance of the parent class to do this.

Option 1 in zetlin's post isn't quite correct either. Default (also known as package-protected) or protected access would allow the subclass to use the variable as well. It isn't necessary to make them public.

import java.awt.*;

class Snowperson
{
	private int xCoor;
	private int yCoor;
	private int width;
	private int height;
	
	public Snowperson()
	{
		xCoor = 0;
		yCoor = 0;
		width = 0;
		height = 0;
	}//end default
	public Snowperson(int x, int y, int wid, int hght)
	{
		xCoor = x;
		yCoor = y;
		width = wid;
		height = hght;
		
	}//end default	
	public int getX()
	{
	   return xCoor;
	}
	public int getY()
	{
	   return yCoor;
	}
	public int getH()
	{
	   return height;
	}
	public int getW()
	{
	   return width;
	}
	public void drawSnowBottom(Graphics g)
	{
		g.fillOval(xCoor,yCoor,width, height);
	}//end draw snow bottom
	public void drawSnowMiddle(Graphics g)
	{
	 g.fillOval(getX()+19, getY()-128,width-40, height-40);
	}//end draw snow middle
	public void drawSnowTop(Graphics g)
	{
	  g.fillOval(getX()+50, getY()-211, width/2, height/2);
	}//end draw snow top
	
	public void drawSnowperson(Graphics g)
	{
		g.setColor(Color.white);
		drawSnowBottom(g);
		drawSnowMiddle(g);
		drawSnowTop(g);
	}//end draw snow person	
}//end class SnowpersonAG

class snowSanta extends Snowperson
{
	public snowSanta(int x, int y, int wid, int hght)
	{
		super (x,y,wid,hght);
	}//pass into to Snowperson
	
	public void drawHat(Graphics g)
	{
		g.setColor(Color.red);
		g.setColor(Color.red);
		int xHat[] = {getX()+50, getX()+150, getX()+160, getX()+170, getX()+160, getX()+150, getX()+125, getX()+100, getX()+75};
    	int yHat[] = {getY()-200, getY()-200, getY()-180, getY()-225, getY()-250, getY()-260, getY()-250, getY()-225, getY()-215};
    	int nHat = 9;
    	g.fillPolygon(xHat, yHat, nHat);
	}//end draw snow person	
}//end class snowSanta

this build, but when i use it in my driver, the drawHat wont work :( can any1 help

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class snowDriverATT
{
	public static void main(String args[])
	{
		GfxApp gfx = new GfxApp();
		gfx.setSize(1024,768);
		gfx.addWindowListener(new WindowAdapter()
		 {public void windowClosing(WindowEvent e)
		  {System.exit(0);}});
		gfx.show();
		
	}// main
}//snowDriver

class GfxApp extends Frame
{
	Color myBackground = new Color(231,225,235);
	public void paint(Graphics g)
	{
		g.setColor(myBackground);
		g.fillRect(0,0,1024,768);
        // basic SnowPerson
		Snowperson santa = new snowSanta(0,568,200,200);
		santa.drawSnowperson(g);
		santa.drawHat(g);
		
	
	}//end paint
}//end class GfxApp

btw any1 have a tutorial for making snow fall?

You declared "santa" to be a Snowperson, which does not have a drawHat() method. You need to declare him as a snowSanta if you wish to use drawHat().

(Also, please note the rules on "chat speak". Make the effort to type full-sentence English if you would like others to take their time to help you.)

This article has been dead for over six months. Start a new discussion instead.