-1

Let's say I have a piece of code:

class one 
{
    String one = "First";
}
class sec extends one 
{
    String sec = "Second";
}
class num
 {
   public static void main(String[] args) 
   {
     one o = new one();
     sec s = new sec();
     one oo = new sec();

     // the object will be of that class for which we are providing the reference

    // then what is the use of the constructor 

     sec s = new one(); // this is not working

     System.out.println(o.one);
     System.out.println(s.sec);
     System.out.println(oo.one); # this seems right
     System.out.println(oo.sec); # this is not working

    }
}

I am confused by the reference and the constructor while creating the object. I have extended the sec class to inherit from class one. I got the meaning of first two object creation that we are referencing to the one class and creating the object same for the second object.

But what about the third object? We are using the reference of the class one and providing the constructor of the child class and I can use the third object(oo) just to refer the variable and methods of the one class not second class.

If I have to use the methods and variables of one class then I can use it with the object(o) rather than object(oo). So what is the difference between both the objects when they are behaving like each other?

 one o = new one();
 one oo = new sec(); 

I want to know the difference between these two that what are the changes come when we use the constructor of the child class rather than the base class.

jwpfox
  • 5,124
  • 11
  • 45
  • 42
babygame0ver
  • 447
  • 4
  • 16

1 Answers1

0

This is actually one of the famous topic of discussion.

First of all there is two things known as object & reference.

Object means your actual image or actual data.

Reference means your view of object.

In terms of database, You can compare object as table & reference as view.

Let's say we have three dimensional cylinder type of object when you see it from top it just look like circle & from front it look like square (here I'm talking about identical conditions) but it's neither actually a circle nor square.

Here the actual object is your cylinder & the the images you get from seeing it from different angles is your reference.

Now come back to the actual question.

When you write one o = new one() it's actual object of one which is referenced (viewed) as a one.

Now when you write one po = new two() it's actual object of two which is referenced as two. Here there is all the components of object exist which is defined in class two. although you can not access them because of the type of reference but they are exist.

To know actual differece let discus about what happens when you write code something like this.

two t=(two)o or two tt=(two)oo

In first case Java throws exception ClassCastException. In the second case you are permitted.

In the second case your object oo is of type two, here the class two inherit the property of class one, so you can treat it as object of class one. But in first case you can not because of the same reason.

The main reason of doing this is because we don't care about all the properties of objects when we are dealing with them.

For example when you are supposed to design the code to send the mail to the list of some persons. Now think what you really required to accomplish this task? Only email id.. You actually don't care about other details which is obviously exist in the object of persons. Now you define the the one class called as Email with the method getEmail.(actually it should be interface) & you make constraints that every person should have to extend this class now every object of person contain property email although it can contain other properties but you don't have to care about that.

Neel Patel
  • 358
  • 2
  • 13