What are Custom/reference Data Types in java

Custom/reference Data Types in java >

  • Custom data types are created by defining and calling constructors of the class.
Example - We may create employee class and define its constructor.

class Employee {
   private Integer id;
  
   /** Employee constructor*/
   public Employee(Integer id) {
          this.id = id;
   }
}
/** Main class*/
public class MyClass {
    public static void main(String...a) {
           Employee emp; //emp is pointing to null
           emp=new Employee(1); //emp is pointing to object
    }
}
emp is reference variable of Employee type.


> emp can refer to any subClass of Employee as well
emp=new SubEmployee();
  Where SubEmployee is subClass of Employee
class SubEmployee extends Employee{
   SubEmployee(){
          super(1);
   }
}

No comments:

Post a Comment