enthuware.ocajp.i.v7.2.1117
Moderator: admin
- 
				jojo314519
 - Posts: 1
 - Joined: Tue Sep 30, 2014 1:27 pm
 - Contact:
 
enthuware.ocajp.i.v7.2.1117
I ran it.  It only prints:
Automobile: drive
Why does test say answer is:
It will print:
Automobile: drive
Truck: drive
Truck: drive
in that order.
Thanks.
			
			
									
									
						Automobile: drive
Why does test say answer is:
It will print:
Automobile: drive
Truck: drive
Truck: drive
in that order.
Thanks.
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
Please make sure you are running the code exactly as given.
			
			
									
									
						- 
				lostintesting
 - Posts: 1
 - Joined: Fri Nov 14, 2014 3:44 pm
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
no, he's right.  the code is:  
class Automobile{
public void drive() { System.out.println("Automobile: drive"); }
}
public class Truck extends Automobile{
public void drive() { System.out.println("Truck: drive"); }
public static void main (String args [ ]){
Automobile a = new Automobile();
Truck t = new Truck();
a.drive(); //1
there is only one line that calls the drive statement. a.drive(); there is not way to get three lines of output.
			
			
									
									
						class Automobile{
public void drive() { System.out.println("Automobile: drive"); }
}
public class Truck extends Automobile{
public void drive() { System.out.println("Truck: drive"); }
public static void main (String args [ ]){
Automobile a = new Automobile();
Truck t = new Truck();
a.drive(); //1
there is only one line that calls the drive statement. a.drive(); there is not way to get three lines of output.
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
Please observe lines marked //1 , //2, and //4.
			
			
									
									
						- 
				baichen7788
 - Posts: 23
 - Joined: Fri Mar 26, 2021 7:25 am
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
I don't understand why t=(Truck)a will result in ClassCastException.
			
			
									
									
						- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
Does the variable a refer to an object of type Truck at run time?
			
			
									
									
						- 
				baichen7788
 - Posts: 23
 - Joined: Fri Mar 26, 2021 7:25 am
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
class Automobile{
public void drive(){}
}
public class Truck extends Automobile {
public void drive(){}
public static void main(String[] args) {
Automobile a = new Automobile();
Truck t = new Truck();
t=(Truck) a;
}
}
			
			
									
									
						public void drive(){}
}
public class Truck extends Automobile {
public void drive(){}
public static void main(String[] args) {
Automobile a = new Automobile();
Truck t = new Truck();
t=(Truck) a;
}
}
- 
				admin
 - Site Admin
 - Posts: 10443
 - Joined: Fri Sep 10, 2010 9:26 pm
 - Contact:
 
Re: enthuware.ocajp.i.v7.2.1117
So, what type of object does the variable a refer to??
a refers to an object of type Automobile. An Automobile is not a Truck. That is why CCE is thrown.
Please go through Java book to understand how casting works. Or you may also go through this tutorial: https://www.baeldung.com/java-type-casting
			
			
									
									
						a refers to an object of type Automobile. An Automobile is not a Truck. That is why CCE is thrown.
Please go through Java book to understand how casting works. Or you may also go through this tutorial: https://www.baeldung.com/java-type-casting
Who is online
Users browsing this forum: No registered users and 10 guests