Tuesday, June 20, 2017

Code to show the use and benefits of very usefull instanceof operartor in java

This program is to show the application of instanceof operator

import java.util.Scanner;

class Car {
void info() {
}
}

/*
 * the info() method of Car has been overridden by its all the subclasses
 */
class Maruti extends Car {
void info() {
}
}

class Alto extends Maruti {
void info() {
System.out.println("alto");
}
}

class Swift extends Maruti {
void info() {
System.out.println("swift");
}
}

class Ciaz extends Maruti {
void info() {
System.out.println("ciaz");
}
}

class WagonR extends Maruti {
void info() {
System.out.println("wagonR");
}
}

class Hundai extends Car {
void info() {
}
}

class I10 extends Hundai {
void info() {
System.out.println("i10");
}
}

class Creta extends Hundai {
void info() {
System.out.println("creta");
}
}

class Verna extends Hundai {
void info() {
System.out.println("verna");
}
}

class Tata extends Car {
void info() {
}
}

class Nano extends Tata {
void info() {
System.out.println("nano");
}
}

class Tiago extends Tata {
void info() {
System.out.println("tiago");
}
}

public class TheInstanceOfOperator {
public static void main(String[] args) {

/*
* here we are creating an array of Car class 
* which can store object of any subclass of Car
*/
Car[] cars=new Car[12];
/*
* lets store some objects of subclasses inside this array
* one by one
*/
cars[0]=new Maruti();
cars[1]=new Hundai();
cars[2]=new Tata();
cars[3]=new Alto();
cars[4]=new Swift();
cars[5]=new WagonR();
cars[6]=new I10();
cars[7]=new Verna();
cars[8]=new Nano();
cars[9]=new Tiago();
cars[10]=new Creta();
cars[11]=new Ciaz();

/*
* scanner to take input
*/
Scanner sc=new Scanner(System.in);
System.out.println("please type....\n1=to show all the cars");
System.out.println("2=to show all maruti cars");
System.out.println("3=to show all hundai cars");
System.out.print("4=to show all tata cars: ");
int choice=sc.nextInt();

String message="";

if(choice==1) {
message="showing all the cars";
}
else if(choice==2) {
message="showing all the maruti cars";
}
else if(choice==3) {
message="showing all the hundai cars";
}
else if(choice==4) {
message="showing all the tata cars";
}
else {
message="invalid choice";
}
System.out.println("####"+message+"####");

/*
* fetch the objects from the array of Car class using for loop
*/
for (int i = 0; i < cars.length; i++) {
if(choice==1) {
cars[i].info();
}
else if(choice==2) {
/*
* as we know the instanceof is an relational operator that checks whether 
* an object belongs to a class or not
* we know that an object belongs to itself and to its superclasses
*/
if(cars[i] instanceof Maruti) { // test for objects those are Maruti type
cars[i].info();
}
}
else if(choice==3) {
if(cars[i] instanceof Hundai) { // test for objects those are Hundai type
cars[i].info();
}
}
else if(choice==4) {
if(cars[i] instanceof Tata) { // test for objects those are Tata type
cars[i].info();
}
}
}
System.out.println("####thank you####");


}
}

4 comments: