windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Object type cast in Java

Asked By: Anonymous

I’m struggling to understand the C() object created in this code.

I understand that when it is created on line 3, it is an object with the reference in the A class. I also understand that you can reference the same object on line 4 from a reference in the B class. However, I don’t understand the casting of reference o1 on line 4. Does this change the object or just how it is referenced?

Would really appreciate an explanation 🙂 thanks

public class TestClass{
   public static void main(String args[ ] ){
      A o1 = new C( );
      B o2 = (B) o1;
      System.out.println(o1.m1( ) );
      System.out.println(o2.i );
class A { int i = 10;  int m1( ) { return i; } }
class B extends A { int i = 20;  int m1() { return i; } }
class C extends B { int i = 30;  int m1() { return i; } }


Answered By: Anonymous


A o1 = new C( );

you assign an object of type C to a variable of type A.

o1 can reference any object of class A or any sub-class of A (such as B and C). However, once you assign an object of class C to a variable of type A, you can only use that variable to execute methods declared in class A (or any super class of A).

If you want to execute methods of class B, you must tell the compiler that this variable actually references an object of class B (which is true, since that variable holds a reference to an object of class C, which is also of class B, since C extends B).

That’s what the cast is for:

B o2 = (B) o1;

Now, using the variable o2, you can call methods declared in class B.

However, since all 3 classes declare a method of the same signature (int m1( )), the cast doesn’t allow you to call a method you couldn’t call via the o1 variable. It does, however, give you access to the instance variable i of class B (by writing o2.i).

Does this change the object or just how it is referenced?

It doesn’t change the object. It just gives the compiler more detailed information about the actual type of the object referenced by that variable.

techinplanet staff

Porno Gratuit Porno Français Adulte XXX Brazzers Porn College Girls Film érotique Hard Porn Inceste Famille Porno Japonais Asiatique Jeunes Filles Porno Latin Brown Femmes Porn Mobile Porn Russe Porn Stars Porno Arabe Turc Porno caché Porno de qualité HD Porno Gratuit Porno Mature de Milf Porno Noir Regarder Porn Relations Lesbiennes Secrétaire de Bureau Porn Sexe en Groupe Sexe Gay Sexe Oral Vidéo Amateur Vidéo Anal

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno