Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

core java programming doubts

 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please clarify some doubts:

1. When I execute this class




I get the output : In add in class A
In add in class A

Please explain what is happening here.Is it overloading or overriding.


2.I have an ArrayList



Can I make the list readonly ,no other values must be added to the List.

 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags, without code tags it is difficult to read the program.
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it overloading or overriding


Because overloaded methods must differ in the type and/or number of their parameters. Therefore it is method overloading.
 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I set the code tags
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I make the list readonly ,no other values must be added to the List.

The Collections class has some utility methods to wrap collections and make them unmodifiable. For example, Collections#unmodifiableList.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p hasini wrote:How do I set the code tags

UseCodeTags
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This time someone (bartender) set it for you. But please use code tags in future. using code button before posting your post, or click on edit button after posting your post and set them
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. The add() method in class B does not override the add() method in class A, because the argument types are not the same. X and Y are subclasses of E and S, but they are not the same. Because of this, the method to call is determined at compile time, by looking at the type of the variable a, which is A (there is no dynamic dispatch, as you would have when there was overriding).

2. You can effectively make the ArrayList read-only by wrapping it like this:

 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 50245
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it says unmodifiableList, that means the List returned cannot be modified, but it remains a copy of the original List. Any changes to the original List are reflected in the unmodifiable copy, so I think it might have been better to call the method readOnlyList.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic