• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with super ...  RSS feed

 
Emilian Chmiel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I wrote simple a program:



when I run i I get:

i 2; super.i: 2

should't I get

i 1; super.i: 2
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 9 is merely two different ways to access the same variable. You don't have two variables -- as there is only one declaration.

Henry
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emilian Chmiel wrote:when I run i I get:

i 2; super.i: 2

should't I get

i 1; super.i: 2
  • No, If you see here
  • The formal parameters int a has value 1 and int b has 2 value passed by this code
  • then in that constructor of second we have
  • Which assigns value of a i.e. 1 to instance variable i of super class first. Now we have i = 1
  • super is the keyword used to access field of super class, here instance variable int i of super class first.


  • But here on the same line
  • We again assigned i = b where value of b is 2. Now value of i is 2.


  • After above assignments, we called to print() method which prints
  • So i and super.i both means same variable of class first which has value 2 so you get output i 2; super.i: 2

  •  
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Name of class always starts with capital letter as you named UseSuper, so becomes easy to identify it. Worth reading Java Programming Style Guide 
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!