• Post Reply Bookmark Topic Watch Topic
  • New Topic

how is class cast in method paramter  RSS feed

 
hayrullah karaman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi forum;
how is cast a class at a methods parameters? i have a problems in a methods paramter. i draw red line my exception and mymethods is orage color .how the solutions ...?






...............
..................

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should use code tags when posting code.

You get the error because CoffeCup is not a CaffeCup in your code so you can't cast a CoffeCup to a CaffeCup.
If you want this to work then you must declare a CoffeCup to be a CaffeCup by using inheritance to make your CoffeCup class extend from the CaffeCup class.

*goes to make a cup of coffee*
 
hayrullah karaman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hji,
i extend caffe from caffeup but the error is contiınue..


public class Caffe extends CoffeCup{

private static final int tooCold = 10;
private static final int tooHot = 80;

public static int getToocold() {
return tooCold;
}

public static int getToohot() {
return tooHot;
}


public void drinkcaffe(CoffeCup tempeture) throws TooHotException,
TooColdException {

int temputere = tempeture.getTempeture();

if (temputere <= getToocold()) {
throw new TooColdException();
} else if (temputere >= tooHot) {
throw new TooHotException();

}else {
System.out.println("afiyet olsun");
}
}

,,,,,,,,,,,,,,,,,,,,,,
public class CoffeCup {

private int tempeture ;


public CoffeCup() {
// TODO Auto-generated constructor stub
}
public int getTempeture() {
return tempeture;
}

public int setTempeture(int tempeture) {
return this.tempeture = tempeture;
}



}
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I said. Revise your notes on inheritance again. You made Caffe extend CoffeCup which means that you are saying that a Caffe is a CoffeCup. That is not correct and is not what I said you should do.
 
hayrullah karaman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i make but it continue..
public class Caffe extends CoffeCup{}
and i control it
Caffe drink = new Caffe();
Caffe cup = (Caffe) new CoffeCup();

if (cup instanceof CoffeCup) {
CoffeCup cup1 = cup;
System.out.println(cup1.getTempeture());
}

it is exception is
Exception in thread "main" java.lang.ClassCastException: com.hayrullah.home.caffeException.CoffeCup cannot be cast to com.hayrullah.home.caffeException.Caffe
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You need to be precise with the names. CaffeCup is not the same as CoffeCup or caffeup. You also have other spelling mistakes in your code.

The argument to the drinkcaffe method is a CoffeCup, and you call the argument "tempeture", which is a bit strange - a coffee cup is not a temperature, so why are you using that name?

In the line: drink.drinkcaffe((CaffeCup)(cup.getTempeture()));

you are calling the drinkcaffe method, and you pass it the result of cup.getTempeture(). Note that cup.getTempeture() returns an int. An int is not a CoffeCup (or CaffeCup?), so you cannot cast it to a CoffeCup.

Please use code tags when you post code, so that the forum software can format your code properly.
 
hayrullah karaman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jesper de jong ,
i write a calss is CaffeCup and another Caffe class i usea method in CaffeClaas it use Caffe Tempure veriable than it result a temoutere than i use toocold or toohot exception ..




i dont use drinkcaffe.
why?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

I have added code tags, as E Armitage suggested, to your first post, and doesn't it look better. Unfortunately you cannot use coloured text in code tags. Avoid colours because some people cannot read red text.

Don't use class names like CaffeCup Caffe Coffe and CoffeCup together. That is a potent source of confusion. As Jesper says, you are confusing CaffeCup and CoffeCup.
In your first post you have a cast like this (CoffeCup)(cup.getTemperature())
That will not work, because you have to consider the precedences. The JVM always goes from left to right, so first it finds something to case. Then it goes right and finds something in (), in fact cup.getTemperature(). But that is an int. So the compiler can “see” that cast will never work and the program won't compile.
So you try again:- (CoffeCup)cup.getTemperature()
The compiler sees something to cast, then the expression cup.getTemperature(). So the compiler sees that the . operator has a higher precedence than cast, so it recognises the int again and it won't compile.
So you try again, realising what you need to do is increase the precedence of the cast with more ():- ((CoffeCup)cup).getTemperature()
So far, so good and the compiler is happy. But if the cup parameter is the wrong type, then the cast will fail at runtime. As you see, you get a ClassCastException.

Now, what you are doing is passing ((CoffeCup)cup).getTemperature() to the drink method, but the compiler know the whole thing is an int. And the drink method takes a Cup. So the compiler can “see” that you have the wrong type, and the program won't compile.

As Jesper says, you have many other errors. If the drink method takes a Cup(, you must pass a reference to) a Cup.
And there is a simple rule about casting: don't. At least not if you can avoid it.
 
hayrullah karaman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all the people
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!