1

I was reading a program and saw the following syntax:

Pet pet1 = (Pet) objIn.readObject();

Pet is a abstract class, objIn.readObjet() means read the objects from the file, but what does the syntax (Pet) mean?

Does it set the every object read from file as a new Pet object? But Pet is an abstract class, how can set a object in it?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
xiaopassion
  • 73
  • 1
  • 9
  • 1
    To be precise: `(` and `)` are called parentheses. Brackets are `[` and `]` –  Apr 23 '14 at 12:31

3 Answers3

4
objIn.readObject();

Returns a reference of type Object. You can not assign a reference of type Object, to a reference declared as type Pet. So you use this syntax to cast it. Casting is changing the type of something. In this case, the pointer to the existing Object.

Example

Object obj = objIn.readObject();

if(obj instanceof Pet) {
    // Make sure it is of type Pet
    Pet pet1 = (Pet)obj;
}

The (Pet) tells the compiler to now refer to this instance as a type of Pet.

Edit

I couldn't put it better myself, so Andrzej's comment can go here:

Casting only tells the type-checker that you (the programmer) have more information about the real type of the object, than can be inferred from static typing. In this example for instance, readObject is only guaranteed to return an Object - but we can tell from the context that the returned value will actually, in reality, be an instance of Pet.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • I would clarify that *casting doesn't actually change the type of any objects*. You cannot use it to *convert* an instance of one class to another, e.g. to turn a `String` (such as `"4.2"`) into a `BigDecimal`. Casting only tells the type-checker that you (the programmer) have more information about the real type of the object, than can be inferred from static typing. In this example for instance, `readObject` is only guaranteed to return an `Object` - but we can tell from the context that the returned value will actually, in reality, be an instance of `Pet`. – Andrzej Doyle Apr 23 '14 at 09:08
  • I thought I'd covered that by saying it is changing the pointer to the existing `Object`. I agree though, this might come off a little vague. – christopher Apr 23 '14 at 12:28
0

It is a type cast. Read this or this.

It means you can treat objects as (one of) their superclass(es). In Java, every class derrives from the class Object (except for the primitive types, that all have a corresponding reference type too, anyway). This means that I can cast String, ArrayList, Calendar, SimpleDateFormat or just any other class you can think of to Object.

Smokez
  • 382
  • 1
  • 5
0

But Pet is an abstract class, how can set a object in it?

This relates to polymorphism. In object-oriented languages, such as Java, it is possible to make variable declarations like:

class Dog extends Pet {...}
Dog someDog = new Dog(...);

Pet pet = someDog; // <--- polymorphism in action

It doesn't actually matter if Pet is abstract or not. We are simply storing a reference to someDog in a variable "shaped" like a Pet.

We can call all the methods defined in Pet on our pet variable, but any additional methods defined in the Dog class won't be available.

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254