CodeGym/Java Blog/Inheritance in Java/Instanceof and Inheritance in Java
Author
Aditi Nawghare
Software Engineer at Siemens

Instanceof and Inheritance in Java

Published in the Inheritance in Java group
members
Hi! In previous lessons, we've already briefly become acquainted with the concept of inheritance. Today, we'll touch on this topic again, but again not too deeply. We are still going to have a more detailed lesson on this in the future. Today we'll just take a quick look at a few practical examples and get acquainted with an interesting operator in Java.

Inheritance

So, what is inheritance? instanceof and Inheritance 101  - 1 Inheritance is a programming mechanism (including in Java) that lets you declare a new class based on an existing one. The derived class then gains access to the fields and methods of the parent class. Why would we need this? Well, imagine that you need to create several car classes in a program: Truck, RaceCar, Sedan, Pickup, etc. Even before writing any code, you know for sure that all these classes have a lot in common: all cars have a model name, year of manufacture, engine size, maximum speed, etc. (not to mention the fact that they all have wheels and other parts in common). In this situation, you can:
  • Create these fields in each class (adding them to each new car class as you create it)
  • Bring the fields common to all cars into a Car parent class, and then use the keyword extends to derive all classes for specific types of cars from the Car class.
Naturally, the second option is much more convenient:
public class Car {

   private String model;
   private int maxSpeed;
   private int yearOfManufacture;

   public Car(String model, int maxSpeed, int yearOfManufacture) {
       this.model = model;
       this.maxSpeed = maxSpeed;
       this.yearOfManufacture = yearOfManufacture;
   }
}

public class Truck extends Car {

   public Truck(String model, int maxSpeed, int yearOfManufacture) {
       super(model, maxSpeed, yearOfManufacture);
   }
}

public class Sedan extends Car {
   public Sedan(String model, int maxSpeed, int yearOfManufacture) {
       super(model, maxSpeed, yearOfManufacture);
   }
}
At a minimum, we avoid the unnecessary duplication of code (and we should always strive for that when writing programs). Plus, we have a simple and understandable class structure, with all fields common to all cars consolidated into one class. If trucks have any special fields that other cars do not, they can be declared in the Truck class. The same goes for methods. All cars have certain common behavior that can be described with methods, e.g. start the car, accelerate/brake, etc. These common methods can be consolidated into the Car parent class, and each specific type of car can define its unique actions in their derived classes.
public class Car {

   public void gas() {
       // Accelerate
   }

   public void brake() {
       // Brake
   }
}


public class F1Car extends Car {

   public void pitStop() {

       // Only race cars make pit stops
   }

   public static void main(String[] args) {

       F1Car formula1Car = new F1Car();
       formula1Car.gas();
       formula1Car.pitStop();
       formula1Car.brake();
   }
}
We added the methods common to all cars to the Car class. But, look at the F1Car class, which represents "Formula 1" race cars. Pit stops (stops for urgent car maintenance) are only done in races, so we added this specific functionality to the relevant derived class. instanceof and Inheritance 101  - 2

instanceof operator

In Java, there is a special operator, instanceof, for checking to see if an object was created based on a particular class. It returns true or false depending on the result of the check. Let's see how it works using the classes in our car example:
public class Truck extends Car {

   public static void main(String[] args) {

       Truck truck = new Truck();
       System.out.println(truck instanceof Car);
   }
}
Output: true The instanceof operator returns true, since we have a Truck object, and all trucks are cars. The Truck class is derived from the Car class. All trucks are created based on the common parent, the Car class. Look closely at how the instanceof operator is used. You write it without a period, since it's an operator, not a method ("object instanceof Class”). Let's try another way:
public static void main(String[] args) {

   Car car = new Car();
   System.out.println(car instanceof Truck);
}
Output: false The Car class (and car objects) does not derive from the Truck class. All trucks are cars, but not all cars are trucks. Car objects are not based on the Truck class. One more example:
public static void main(String[] args) {

   Car car = new Car();
   Truck truck = new Truck();
   System.out.println(car instanceof Object && truck instanceof Object);
}
Output: True Here the logic is simple too: all classes in Java, including classes you create, descend from the Object class (even though you don't write "extends Object"—it's already implied). How and when would this be useful? The instanceof operator is most commonly used when overriding the equals() method. For example, here's how the equals method is implemented in the String class:
public boolean equals(Object anObject) {
   if (this == anObject) {
       return true;
   }
   if (anObject instanceof String) {
       String anotherString = (String) anObject;
       int n = value.length;
       if (n == anotherString.value.length) {
           char v1[] = value;
           char v2[] = anotherString.value;
           int i = 0;
           while (n-- != 0) {
               if (v1[i] != v2[i])
                       return false;
               i++;
           }
           return true;
       }
   }
   return false;
}
Before comparing a String to the passed object, the method tests to see whether the object is even a string? Only then does it start comparing the two objects' properties. If this test didn't exist, any object with value and length fields could be passed to the method and compared with a String, which would be wrong, of course.
Comments (21)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Anonymous #10428383
Level 12 , Seattle, United States
19 January, 12:05
it's compare the 2 strings characters by index, after check as String
Dejejen
Level 22 , Poland, Poland
19 November 2021, 08:01
What is going on with "value"? Where is from this one?
Anonymous #10428383
Level 12 , Seattle, United States
20 January, 08:14
from 'this' this == anObject
Piotr
Level 20 , Poland
31 January 2021, 20:12
Shouldn't the lines 9-10 looks like this: char[] v1 = value; char[] v2 = anotherString.value; Correct me if I'm wrong please.
Dmitri
Level 22 , Seversk, Russia
17 November 2020, 12:31
From the above example - int n = value.length; I do not see the initialization of the variable value.
Anonymous #10428383
Level 12 , Seattle, United States
20 January, 08:15
from 'this' this == anObject
Lena Kopito
Level 11 , Haifa, Israel
7 July 2020, 11:46
Can someone explain what lines 2-4 mean? What does "this" in
if (this == anObject) {
    return true;
}
represent?
Gellert Varga
Level 23 , Szekesfehervar, Hungary
7 July 2020, 22:24
If you want to check if two strings (str1 and str2) are equal, you have to call this described equals() method, this way:
str1.equals(str2);
Inside the method described above, "this" means str1. ("this" always means the object on which we invoke the method.) The passed/received object is the str2. But the str2 is named anObject within the method. So this code:
this == anObject
examines the identity of the references of the str1 and str2 objects. The == operator between two objects examines the references. If they are the same, str1 and str2 point to the same string object. So a thousand percent that they are 'equals': so the method returns 'true'.
Adrie Taniwidjaja
Level 11 , Bandung, Indonesia
3 September 2019, 03:17
regarding line 8 to 18: - does line 17 an "else" phrase of "if" on line 8? I never know about this syntax, can someone give any link explain about that? - the condition syntax on "while" loop is very interesting, it looks like a shortcut for decreasing the value of n and then directly compare the value. We need this kind of shortcut if there is any. Does somebody know it?
Angel Li
Level 18 , Fremont, United States
26 June 2020, 23:04
1. The if statements are nested, so basically:
if (anObject instanceof String) {
       String anotherString = (String) anObject;
       int n = value.length;
       if (n == anotherString.value.length) {
           char v1[] = value;
           char v2[] = anotherString.value;
           int i = 0;
           while (n-- != 0) {
               if (v1[i] != v2[i])
                       return false;
               i++;
           }
           return true;
       }
   }
return false;
In line 1 the first if starts. The steps are as follows: (according to the code above, I know it's not line 1 in the image in the post) 1. If the object is an instance of the class String, then it executes line 2 and 3. 1.1 Since the if statement in line 1 doesn't end here, it executes the next if statement. If the lengths are equal, then it executes line 5-7. 1.2 Moves on to the while loop. The n-- != 0 means the while loop will be executed as long as n decremented by 1 is not 0. Continues on to if statement. 1.3 If the condition in the if statement is true, next two lines are executed. 1.4 While loop ends, then returns true. Then the second if statement ends. The if statement on line 1 ends. 2. If the first if statement on line 1 is false, then it goes to line 16 and returns false.
Angel Li
Level 18 , Fremont, United States
26 June 2020, 23:08
In regard to the second question you had look at this example:
int i = 2;
i++;
System.out.println(i); // result will be 3 because the ++ means increase by 1 (2+1)
i--;
System.out.println(i); // result will be 2 because -- means decrease by 1 (3-1)
SirSkii
Level 12 , Chicago, United States
7 October 2020, 03:59
Thanks for this! Line 5 & 6: because Strings Classes are IMMUATBLE we have to store both char values as an array so we can access & modify them. Side Note: Technically, we do not need store the char values as an array to ACCESS the individual char values; we could alternatively use this cool thingy here: charAt(int index). Immutable means it can't be changed, doesn't me it can't be accessed :). But that comes with it's own nuances, hence why it probably wasn't mentioned. line 8 - 10: iterates same number of times as the length of calling this.value.length. "This" value is the invoking objects value length. Line 9 specifically: compares corresponding char array indices. If not equivalent, end method by returning 'false'. This basically does a 1 to 1 check with each char by way of array index.
Guanting Liu
Level 0 , Springfield, United States
Expert
13 December 2020, 09:04
if without else is only allowed when the method is void type because else might not be required if u only want something instead of whole thing,if it needs to return something,that's very silly to write if without else,that's the difference.
Jay
Level 17 , Washington, United States
5 August 2019, 16:03
I'm having trouble understanding the example.
4 May 2020, 15:36
Mee too
Fadi AlSaidi
Level 13 , Carrollton, United States
9 February 2019, 13:32
can someone explains to me lines 7-14? where did value come from. shouldn't be " this.lengths; ", from the look of it, value looks like a array Thank you in advance :)
David Lavigne
Level 19 , Lynnwood, United States
23 March 2019, 19:22
This equals method is part of the string class. I assume, in the string class, there is an instance variable called value, which represents the string value on which the equals method is being called. For example: String s1 = "foo"; String s2 = "bar" S1.equals(s2) In S1 object, it has a value of "foo" and the anObject parameter references s2.
Stanislav Mayer
Level 15 , Czech Republic
10 February 2022, 08:08
Happy to confirm that your assumption is correct: Link
Hashirama
Level 26 , Port-Harcourt, Nigeria
30 January 2019, 03:25
For the last example, at line 6, is it necessary to cast anObject as a String, since it is already an instanceof a String?
fran
Level 14 , Cordoba, Argentina
31 January 2019, 20:25
I think this cast is necesary because anObject could be a Object type. In this case the operator instanceof evalue to true, and you must cast the Object type into the String type.
Hashirama
Level 26 , Port-Harcourt, Nigeria
1 February 2019, 12:20
On the matter that, though it may be referencing a String, it could as well be an Object type while doing this, hence, the cast. This is true. Thanks...!!!