-2

I create the object using two statements , is there any difference between the two ?

public interface vehicle 
{
void accelerate () ;

}


class bmw  implements vehicle
{
void accelerate () 
{
System.out.println (" top speed of 300kmph " ) ; 
}
}

public class driver 
{
public static void main (String qw [] )  
{
vehicle v = new bmw () ; // statement 1 
v.accelerate () ;

bmw b = new bmw()  ; // statement 2 
b.accelerate() ;
} 

}

Both the statements are giving same output but I think there is some difference between the two .

cs-dev
  • 107
  • 10
  • 4
    Possible duplicate of [What is the difference between field, variable, attribute, and property in Java POJOs?](http://stackoverflow.com/questions/10115588/what-is-the-difference-between-field-variable-attribute-and-property-in-java) – default locale Feb 09 '17 at 10:33
  • What makes you think ``y`` belongs to the subclass? The ``show`` method is defined in ``A``. – f1sh Feb 09 '17 at 10:52

2 Answers2

3

Variable 'y' it's private so you can't access it directly from you subclass. But you can organize the access to this variable creating public methods.

So when are you calling:

subclass.show ( 23 , 45 );

you are not accessing directly to 'y' attribute of 'A' class but only to a public method defined in it where you are using 'y'. You can do it because it's a method in 'A' class.

Trying to explain better:

Private modifier let your attribute in this case be not accesible from a subclass. So you can"t do this:

 B b = new B();
 b.y = 10;

Because you don't have direct access to this attribute. So now you can define how subclass can access this private attribute with a public method. The best example will be a getter or setter: (this methods have to be defined in your superclass)

Public int getY(){
  Return this.y;
 }
Public void setY(int y){
  This.y = y;
 }

Now you can access to private attribute 'y' but you need to call this public method so now you can do:

B b = new B();
b.setY(10);

And you will change the value of 'y'.

Now, according to yuor code, you didn't made any setter or getter but you defined a method call 'show(int,int)' where you are changing the value of 'y'. And this is working likely as setter methods. So you can access directly this method like:

B b = new B();
b.show(5,10);

Because it's a public method. And inside of this method you are doing operationts on a private attribute. So, finally, the private attribute 'y' belongs to 'A' superclass and can't be access directly by any subclass but you can manage operations defining public method in superclasw where you specific how other classes can access superclass private attribute.

I have this doubt because since y is a private variable then how can y belong to object subclass ?

'y' doesn't belong to subclass. As i said before you can access 'private' attributes only using public methods defined in superclass. So if i want to change value or show 'y' i can do it only if there is public methods in superclass that change or show 'y' value.

void show (int o ,int e )
{
   x=o ;      // I mean to say ,
   int y ;    // Is this implicitly declared above  ?

   y=e ;
   System.out.println(" x = " + x);
   System.out.println(" y = " + y);
}

Here you don't need to declare 'int y;' because this method is defined in superclass and so you have direct access to 'y'.

If you need more please comment.

Mike
  • 144
  • 2
  • 8
  • So tell me what you want to know more so i can edit and add something more useful. – Mike Feb 09 '17 at 17:04
  • For subclass 'Y' it's an attribute of superclass. It's private so subclass can't access it directly. – Mike Feb 18 '17 at 11:38
  • Not really, becausewhen you do "y=e" you are changing value of "y" that it's an attribute not a local variable. – Mike Feb 18 '17 at 11:56
0

yes, y will b printed because y is local and it has been initialized before printing . so there is no error.

In your second program, Y is private member of class A and it is not directly accesible in its child class. you have made object of sub class but you have not override show method of parent class.So the show method of parent class means A class will be executed, and there you will find Y.

surajy79
  • 21
  • 1
  • 1
  • 4