• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Basic Java doubts

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a couple of doubts regarding Java concepts.

1. In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value?

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?

Plz help me getting these concepts clear. Thanks in advance.

navEEn
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by naveen chinthakunta:
... In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value? ...


Java always passes by value. In the case of objects, it's a copy of the reference (a value) that is passed. Note however, that the original reference and the copy of that reference both point to the same object. So even though the reference copy is local to a method, the object itself can still be modified via that reference (assuming the object is mutable).

So, if I'm understanding your question, you may want to clone your object in order to get a "local" copy that will not affect the original object. For details on this, see Appendix A: Passing & Returning Objects from Bruce Eckel's Thinking in Java. (Interestingly, this appendix frames Java's "pass by value" vs. "pass by reference" confusion as a debate in which both sides have merit.)
[ November 02, 2005: Message edited by: marc weber ]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by naveen chinthakunta:
on, how do I achieve that. Basically how do I pass objects by value?

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?



Java doesn't do away with multiple inheritance of type, only multiple inheritance of implementation. You can extend only one class, but you can implement as many interfaces as you'd like. Therefore, Java class hierarchies should be designed around interfaces, not around classes.

If you'd like to inherit implementation from multiple classes for convenience sake, though, you can use delegation. Extend one class, then for the others, hold a member of that other class, and forward methods to it. This works great as long as you don't try to treat classes as types; only treat interfaces as types.
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. In Java methods, primitive types are passed by value and objects are passed by reference. So if I dont want my object value to change in the function, how do I achieve that. Basically how do I pass objects by value?



I could go into the whole 'pass by copy of a reference' argument, but I always found it to be too technical, whereas the concept is, yes you are conceptually passing object by reference. If you don't want an object to change you have to deep clone it. If you alternatively want a primitive to change you have to put it in an object like an array.

There are alternate strategies for duplication such as using good encapsulation. For example, if all class members must only be accessed via getters and there are no setters (or limited access to them) you could prevent someone from modifying the object. You have to watch out for complex data types, though, such as an array where the person is free to modify elements of it the array using a getArray() method. In that example, you would need a getter for the primitive elements such as getArrayElement(int).

2. Java completely does away with multiple inheritance. If my requirement demands me to inherit from two classes how do I achieve this? Whats the work around?



Interfaces and helper classes. Multiple Inheritence (via implementation) can be deadly to large-scale applications. Classes can use multiple interfaces and in if organized well, this strategy often leads to cleaner code management.
[ November 02, 2005: Message edited by: Scott Selikoff ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic