Explain narrowing with object in Java.

Java 8Object Oriented ProgrammingProgramming

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).

Type Casting/type conversion −Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.

Narrowing − Converting a higher datatype to a lower datatype is known as narrowing. In this case the casting/conversion is not done automatically, you need to convert explicitly using the cast operator “( )” explicitly. Therefore, it is known as explicit type casting. In this case both datatypes need not be compatible with each other.

Example

import java.util.Scanner;
public class NarrowingExample {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter an integer value: ");
      int i = sc.nextInt();
      char ch = (char) i;
      System.out.println("Character value of the given integer: "+ch);
   }
}

Output

Enter an integer value:
67
Character value of the given integer: C

Narrowing with objects

you can cast the reference(object) of one (class) type to other. But, one of the two classes should inherit the other.

Therefore, if a class inherits the properties of the other conversion of super class object in to sub class type is considered as, narrowing with respect to objects.

But unlike widening in case narrowing you need to use the cast operator.

Example

In the following Java example, we have two classes namely Person and Student. The Person class has two instance variables name and age and one instance method displayPerson() which displays the name and age.

The Student extends the person class and in addition to the inherited name and age it has two more variables branch and student_id. It has a method displayData() which displays all four values.

In the main method, we have created the objects of the both classes separately and we are trying to convert the sub class object into the super class type.

class Person{
   private String name;
   private int age;
   public Person(String name, int age){
      this.name = name;
      this.age = age;
   }
   public void displayPerson() {
      System.out.println("Data of the Person class: ");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
   }
}
public class Student extends Person {
   public String branch;
   public int Student_id;
   public Student(String name, int age, String branch, int Student_id){
      super(name, age);
      this.branch = branch;
      this.Student_id = Student_id;
   }
   public void displayStudent() {
      System.out.println("Data of the Student class: ");
      System.out.println("Name: "+this.name);
      System.out.println("Age: "+this.age);
      System.out.println("Branch: "+this.branch);
      System.out.println("Student ID: "+this.Student_id);
   }
   public static void main(String[] args) {
      //Creating an object of the Student class
      Student student = new Student("Krishna", 20, "IT", 1256);
      //Converting the object of Student to Person
      Person person = new Person("Krishna", 20);
      //Converting the object of person to student
      student = (Student) person;
      student.displayPerson();
      student.displayStudent();
   }
}

Run time error

To this reference, members of both classes are available and the program gets compiled successfully. But, when you try to execute it, an exception will be raised as shown below −

Exception in thread "main" java.lang.ClassCastException: ther.Person cannot be cast to ther.Student
at ther.Student.main(Student.java:41)

To resolve this issue, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.

Example

public static void main(String[] args) {
   //Converting the object of Student to Person
   Person person = new Student("Krishna", 20, "IT", 1256);
   //Converting the object of person to student
   Student student = (Student) person;
   student.displayPerson();
   student.displayStudent();
}

Output

Data of the Person class:
Name: Krishna
Age: 20
Data of the Student class:
Name: Krishna
Age: 20
Branch: IT
Student ID: 1256
raja
Published on 08-Jul-2019 13:40:18
Advertisements