Results 1 to 15 of 15
  1. #1
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default incompatible types

    Hallo, alltogether,

    I think this is a real newbie-question and I searched for "incompatible types" and didn't find anything.
    I wrote this code:

    Java Code:
    //import java.awt.Point;
    class Klassenuebernahme{
    	public static void main(String[] args){
    		Stone s=new Point(5,3);
    		//Point p=new Point(5,3);
    		s.hallo();
    	}
    }
    class Stone extends Point{
    	void hallo(){
    		System.out.println("Hallo!");
    	}
    }
    class Point{
    	public int x;
    	public int y;
    	public Point(int x,int y){
    		this.x=x;
    		this.y=y;
    	}
    	public double getX(){
    		return x;
    	}
    	public double getY(){
    		return y;
    	}
    	public Point(){
    		this.x=0;
    		this.y=0;
    	}
    }
    When I compile that, I get the following error:

    Klassenuebernahme.java:4: incompatible types
    found : Point
    required: Stone
    Stone s=new Point(5,3);
    ^
    1 error

    I don't understand that: I thought it was possible to generate an instance of a subclass by generating an instance of the superclass. I say:
    Stones are special kinds of Points (with additional behaviors). So every Stone is a Point. If I create a Stone by saying that he should have the behaviors every Point has, then (perhaps it is not clear, what the special behavior of that object is)

    But I have another example where it worked:
    Java Code:
    class ServerTest{
    	public static void main(String[] args){
    		Computer computer;
    		computer=new Server();
    	}
    }
    class Computer{
    	private String typbezeichnung;
    	private String standort;
    	private boolean betriebsstatus;
    	Computer(String typbezeichnung,String standort,boolean betriebsstatus){
    		this.typbezeichnung=typbezeichnung;
    		this.standort=standort;
    		this.betriebsstatus=betriebsstatus;
    	}
    	Computer(){
    		this("unbekannt","Lager",false);
    	}
    }
    class Server extends Computer{
    	/*private boolean netzverbindung;
    	void netzEin(){
    		netzverbindung=true;
    	}*/
    }
    A little remark: I want to use the class Point from java.awt in the end; because my code didn't work I took some kind of the java.awt.Point-code and filled it in here.

    Thank you for every help.
    Mephistochen

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    Your first example is attempting to assign a Point to a Stone variable, but though every Stone is a Point, not ever Point is a Stone...so the compiler flags the error.
    In the second example you assign a Server to a Computer variable, which is fine because every Server is a Computer.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    Thank you for your answer.
    You are right; I made a mistake. Unfortunately the constructor is not hereditary. I would wish to construct a special Point-object (which is in fact a Stone-object and while creating the object tell the compiler what the additional behaviors of that object should be). Do you know how to do that?

  4. #4
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: incompatible types

    You are right this is a complete newbie's question
    To simplify what Tolls has explained to you
    consider this

    public class Shape{}
    public class Circle extends Shape{}

    A Circle is-a Shape
    but not every shape is a circle, it could be anything from square, rectangle triangle
    Meaning you can't say
    Circle c = new Shape();
    in java we can say

    Shape s; // a variable named s can point at any Shape
    s = new Circle(); our variable s now pionts to this particular Circle

    use the shape and cirlcle example wherever you see polymorphism( It seriously helped me alot)
    Last edited by Kagiso; 05-17-2013 at 11:58 AM. Reason: rephrasing some of the words

  5. #5
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    I think I have got it. Inside my Stone-class I can call the constructor of the Point-class. It works like this:

    Java Code:
    //import java.awt.Point;
    class Klassenuebernahme{
    	public static void main(String[] args){
    		Stone s=new Stone(5,3);
    		//Point p=new Point(5,3);
    		s.hallo();
    	}
    }
    class Stone extends Point{
    	private Point p;
    	void hallo(){
    		System.out.println("Hallo!");
    	}
    	Stone(int x,int y){
    		this.p=new Point(x,y);
    	}
    }
    class Point{
    	public int x;
    	public int y;
    	public Point(int x,int y){
    		this.x=x;
    		this.y=y;
    	}
    	public double getX(){
    		return x;
    	}
    	public double getY(){
    		return y;
    	}
    	public Point(){
    		this.x=0;
    		this.y=0;
    	}
    }
    Thanks again for your help!
    Mephistochen

  6. #6
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    Thank you for your analogy, too, Kagiso!
    I think here is no possibility to mark this thread as solved, is it?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    No, that's not it at all.
    Stone extends Point, so adding a Point attribute makes little sense (in this context).
    You want to call the constructor:
    Java Code:
    Stone(int x,int y){
        super(x,y); <-- this calls the parent constructor.
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    This works also fine. But isn't that the same as with
    Java Code:
    class Stone extends Point{
      Private Point p;
      Stone(int x,int y){
        this.p=Point(x,y);
      }
    }
    I think my example showed that my program accepted s to be a real Stone (not only a Point), otherwise it wouldn't have accepted that s can call the method hallo():

    Java Code:
    Stone s=new Stone(5,3);
    		s.hallo();

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    OK, so what happens with your version of Stone if you call getX or getY?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    This is my example at the moment:

    Java Code:
    import java.awt.Point;
    class Klassenuebernahme{
        public static void main(String[] args){
            Stone s=new Stone(5,3);
            s.hallo();
    	s.rightpush();
    	System.out.println(s.getX()+" "+s.getY());
        }
    }
    class Stone extends Point{
        private Point p;
        void hallo(){
            System.out.println("Hallo!");
        	}
        Stone(int x,int y){
         	this.p=new Point(x,y);
        }
        void rightpush(){
    	int k=(int) this.getX();
    	int r=(int) this.getY();
    	this.move(k+1,r);
        }
    }
    The output is:
    Hallo!
    1.0 0.0

    So it is possible to call s.getX()
    But: What I don't understand is, why new Stone(5,3) constructs the ordered pair (0,0) [instead of the ordered pair (5,3)].
    Last edited by mephistochen; 05-17-2013 at 01:09 PM. Reason: shiftings better

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    Because you have simply created a Point object as an attribute of Shape, rather than calling the Point constructor as I said in my earlier post.
    x and y never get set.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    Ok. I see it: It works better with super(x,y):

    Java Code:
    import java.awt.Point;
    class Klassenuebernahme{
        public static void main(String[] args){
            Stone s=new Stone(5,3);
            s.hallo();
    	s.rightpush();
    	System.out.println(s.getX()+" "+s.getY());
        }
    }
    class Stone extends Point{
        private Point p;
        void hallo(){
            System.out.println("Hallo!");
        	}
        	Stone(int x,int y){
         		super(x,y);
        	}
        	void rightpush(){
    		int k=(int) this.getX();
    		int r=(int) this.getY();
    		this.move(k+1,r);
    	}
    }
    I think in the old code, the compiler did the following:
    Because of the line
    Java Code:
    Stone s=new Stone(5,3)
    it constructed a new Object Stone(5,3). This Object has a variable Point which is set to (5,3). But when I asked for s.getX() it was not clear which value to take because in my class I had only this Point variable set to (5,3) and not the object itself. This was the reason why the interpreter took the standard value for Point-objects (0,0).

    This is how I explain the behavior of my previous code.

    But now it works with the new code (above).

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    When you called s.getX() it called the getX() that is part of the Shape class.
    That getX is inherited from the Point class, so that was the code that was run.

    x and y were set to 0 in that case because a Java constructor always starts by calling its parent constructor first.
    If you don't specifically put in super() then the compiler will add it.
    If the parent class does not have a no parameter constructor in this case then it will flag an error.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    mephistochen is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: incompatible types

    Thanks for the explanation, Toll!

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default Re: incompatible types

    Good-oh.
    At least I didn't confuse matters by talking about "Shape" when I should have been talking about "Stone" in that previous couple of posts...:)
    (Thanks to jim for pointing that out)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Incompatible types?
    By jacjacjac in forum New To Java
    Replies: 1
    Last Post: 10-13-2012, 09:01 PM
  2. incompatible types?
    By slider57 in forum New To Java
    Replies: 3
    Last Post: 09-26-2012, 12:55 PM
  3. Incompatible types
    By Yokomoko in forum New To Java
    Replies: 10
    Last Post: 07-10-2012, 03:49 AM
  4. incompatible types
    By angelicatomnob in forum New To Java
    Replies: 3
    Last Post: 01-17-2012, 03:25 PM
  5. incompatible types!!
    By sneha343 in forum New To Java
    Replies: 7
    Last Post: 11-30-2011, 11:49 PM

Tags for this Thread

Posting Permissions

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