Access attributes of type Object

1

I'm having difficulty accessing values of an attribute of type Object. I have a class in which has as attributes in prox, and Object Item.

Each item is an object of the Subject class.

asked by anonymous 24.06.2018 / 22:53

1 answer

1

If items are always Subjects, why do not you declare them as Subject and not as Object?

Anyway, if you do not want / can change this, to access the Attributes of Subject, you need to cast, so java will try to convert the object to the type you want, you do this by informing the type in parentheses.

System.out.println("Desc:"+ ((Assunto)head.item).propriedadeDeAssunto);

Another tip is not good practice to access properties directly, instead make private properties and create get and set methods to access the values, so you maintain the encapsulation. If you are still studying and have not seen it, you will see in the future.

    
24.06.2018 / 23:23