why setXXX() method is not overrinding?

You are creating two independent objects.

First you create an object and name it santhosh. This object is referenced by the obj variable.

pojo obj = new pojo();
obj.setName("santhosh");

Then you create a second object, which is referenced by the exObj variable.

ExtendPojo exObj = new ExtendPojo();

It doesn’t have a name yet, since it’s a new object and you haven’t assigned the name. You then give it a name.

exObj.setName("mahesh");//It is not overriding

Now you print the name of the first object, which hasn’t changed.

System.out.println(obj.getName());//it prints santhosh.

The code is doing exactly what you asked it to do.

If you intended the two variables to reference the same object, you’d do this:

ExtendPojo exObj = new ExtendPojo();

pojo obj = exObj ;//Same object, new variable, different type
obj.setName("santhosh");

exObj.setName("mahesh");//It is working now

System.out.println(obj.getName());//it prints mahesh.

Leave a Comment