Beginner quizzes – Java casting object

The following Java exercise(s) are designed for beginner level programmers. If the level is too hard, then I recommend reading my article about this topic, which offers a theoretical explanation including more exercises. Read More: Java Object Casting

casting-objects-green

Quiz 1: Upcasting an object

What happens when the following program is compiled and run?

public class Super {

	protected int i = 4;

	public int method(){
		return i * i;
	}
}

public class Sub extends Super {

	int i = 3;

	public int method(){
		return i * i;
	}
	public static void main(String[] args){
		Super s = new Sub();
		System.out.print("-x" + s.method());
		System.out.print("-x" + s.i);
	}
}

Select the correct answer.


Quiz 2: Downcasting and upcasting objects in Java

What happens when the following program is compiled and run?

public class MySuper {

	int z = 3;

	int myMethod(){
		return z;
	}
}

public class MySub extends MySuper {

	int z = 8;

	int myMethod(){
		return z;
	}
	public static void main(String[] args){
		MySuper mySuper = new MySub();
		MySub mySub = (MySub) mySuper;
		System.out.print("-x" + mySuper.myMethod() + "-x" + mySuper.z);
		System.out.print("-x" + mySub.myMethod() + "-x" + mySub.z);
	}
}

Select the correct answer.




Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

27 Comments

Add a Comment
  1. hello sir !

    why 4 in the first output ?

    1. By casting objects
      s.method() would invoke the overridden method in the subclass, while the attribute is of the type of the superclass. So, s.i refers to the i in the superclass.

  2. Hello world !
    i did not understand why in the first code 4 was in the output?

  3. I like what you are up too. This kind of clever work!

  4. Wow, marvelous blog structure! How long have you been running a blog?

  5. i love your blog and its my favorite java website!!

  6. Love the quizzes! I am the beginner and spent over an hour thinking about few questions. Not easy but covers a lot!

  7. The paragon of undrestanding these issues is right here!

  8. First of all, I want to say that your website is awesome! I love all your quizzes and thanks for your great effort.

    1. You are always welcome!

      1. That’s a genuinely impressive answer.

    2. The quality of your posting is there for all to see

  9. I am a beginner Java programmer. Can you tell me please, how long it takes to start with the intermediate level quizzes?

    1. I think you need almost one year experience, but that is not a rule:)

      1. Thanks! Always good to find a real expert.

    2. If inoamrftion were soccer, this would be a goooooal!

  10. I learn everyday from your quizzes!

    1. I am glad to read that!

      1. I told my kids we’d play after I found what I neddee. Damnit.

    2. Big help, big help. And sutrleapive news of course.

  11. Thanks for posting this nice article!

    1. If your aritelcs are always this helpful, “I’ll be back.”

  12. Your articles and quizzes are just great. Keep up the good work!

    1. The answer of an expert. Good to hear from you.

  13. Please, more quizzes!

    1. I update and add content to this website regularly. New quizzes are on the way:)

      1. Just what I’ve been looking for. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *