• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

in what ways can we change this formula..

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i noticed that whenever we want to create some object we need to make

same_class a=new same_class();

i heard that we can put an interface on the left side
what does it meen
what are the laws of this stuff???
[ March 20, 2008: Message edited by: donaldth smithts ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is closely related to this recently discussed topic.

In your example it would mean that if you have

you could write

instead of

[ March 20, 2008: Message edited by: Ulf Dittmer ]
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and what is the differece between these two objects??
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference in the two objects. If an object of type SomeClass is created, its type is fixed and never changes.

However, there could be multiple references to the SomeClass object. Typically, a reference is either of the same type (SomeClass) or something higher up the SomeClass hierarchy, for example SomeInterface.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have trouble understand what you are saying
the only reffernce i know is when
A=B
then A takes the value of B and they both point to the same memory cell
i have trouble in thinking that way in creating an object

if i understood you correctly you sayd that if we have

same_object A=new same_object()
then it cannot be changed
but when we have interface on the left side then its changeable

what do you meen by change??
what changes can we have??
 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

then it cannot be changed


The type Roger refers to is actually the class (type), i.e. same_object in your example. The class type CANNOT be changed.

However, the reference, i.e. A in your example CAN be changed, for example, to point to another object of type same_object.

Whether an interface is declared as the type of reference for A does not come into play here. It simply means that any class (type) that implements the interface may be assigned to the reference.

For example if SameClass and AnotherClass both implement AnInterface, both of the following declarations are valid.

BTW, the class name you used, i.e. same_object, should really be same_class. Objects are instances of classes. You should learn to make the distinction.
[ March 20, 2008: Message edited by: Jay Damon ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two very common examples:

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you sayd that


regarding this line

i understand that its a legal form
but how it differs from

plus i cant understand how did you implement the AnotherClass
in the second example

its not a legat form
you should write something on the left side not just A
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For SameClass and AnotherClass to implement AnInterface, you would need the following class declarations:

Implementing an interface simply means that a class will provide, at a minimum, concrete implementations of all methods declared in the interface. For example, if AnInterface declares a single method named aMethod, then SameClass and AnotherClass must provide an implementation of aMethod. Doing so allows either SameClass or AnotherClass to behave as type AnInterface.

It may be helpful to think of an interface as a common denominator among classes. In this example:

SameClass may be considered of type SameClass or AnInterface.
AnotherClass may be considered of type AnotherClass or AnInterface.

SameClass cannot be assigned to a reference of type AnotherClass or vice versa. However, either may be assigned to a reference of type AnInterface.

you should write something on the left side not just A


I did not do so because doing so would result in a duplicate declaration. The variable A was declared as type AnInterface in the previous line. My example was meant to show you that an instance of either SameClass or AnotherClass could be assigned to the same reference.

I could just as easily have written:
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say we have this.



Now consider these two variables.



The only methods which can be invoked are those known to the variables.

 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so when its interface on the left side then we can
use only the methods which are defined in the interface
unlike the other type where we can use all of them

am i correct
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are correct.

Now here is a little test.



What is the complete list of methods which can be invoked by using the object variable?
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats some how different
because you have interface on the right side
the stucture that i understood is:
that on the right side we have a menu of method
and the left side chooses the methods to use

the problem is in that example we dont have any methods on the menu
only a declarations
so its an illegal line
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is important is to understand that someInterface and object are both references to a SomeClass object. I assure you that the line of code is quite legal because a reference of type Object can be assigned any reference (such as someInterface).
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but what about the methods

how many methods our object has???
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember: the SomeClass object never changes, what can change is the view of the SomeClass object from different references.

So, have a go at trying to figure out which methods are applicable for the reference of type Object.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some basic missunderstanding
i thought i understood the whole thing
apperantly thats not true
what represents the right side??
what represents the left side??

regarding the object

(please dont use the word refference this word maked me dizi )
[ March 21, 2008: Message edited by: donaldth smithts ]
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To understand Java, you first need to understand what a class, interface, type, object, reference, etc. are and the distinctions between these terms. This is fundamental Java so don't ask us not to use this terminology. Roger and I will help you understand these terms if you have specific questions. I think if you understand this terminology, you will understand the examples we have provided. Roger and I have carefully chosen our words in order to try to answer your questions.

To try to answer what I think are your questions in your most recent post, a new object is created whenever the new operator is used to instantiate a class on the right-hand side of an assignment operator. A reference is returned that is assigned to the variable on the left-hand side. Using that information, I suggest you re-read the examples Roger and I have provided in previous posts and see if those examples are clearer now.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i understood that the right side is responsible for the type of the variable

when we are makin a new variable its type is the right side Object
the problem is that you made an example where an interface is on the right side
a variable cannot be of type interface



i have trouble to understand that line
a refence from what???
return to whome??
this is a refernce A=B they point the same memory

i need a clearer understanding of the left side
[ March 22, 2008: Message edited by: donaldth smithts ]
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you read the JavaRanch Cup Size campfire story. Perhaps that will clear up your confusion.


i understood that the right side is responsible for the type of the variable


Not true. The type, be it a class OR interface, of variable is declared on the left-hand side. A reference to an object of that type (or null) must be assigned to that variable.


the problem is that you made an example where an interface is on the right side


Not quite true. The object on the right-hand side is always an instantiated class NOT an interface. If the declared reference to an object is an interface then the object assigned to that reference MUST implement that interface. Interfaces CANNOT be instantiated.


a variable cannot be of type interface


Yes it can. A variable is of a declared type. A type may be either a class OR an interface.

Read the lessons for Classes and Interfaces from the Java Tutorial.
[ March 22, 2008: Message edited by: Jay Damon ]
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok correct me if i am wronge
ther 3 kind of these formulas
the first is:
some_class A=new some_class();
which says that "A" has all the methods of some_class

the second is:

some_interface A=new some_class();

which says that "A" has only the methods which are written in
the some_interface
what if some class doesnt implements this interface??

the third one is:
some_class A=new some_interface ();
here i dont have any clue
i dont know what methiod our "A" variable has
 
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first suggestion
SomeClass A = new SomeClass();
A has all the methods etc of SomeClass-correct.

Your second suggestion
SomeInterface A = new SomeClass();
A has all the methods of the interface SomeInterface, as long as SomeClass implements SomeInterface-correct.

Your third suggestion
SomeClass A = new SomeInterface();
You haven't implemented the methods of SomeInterface. SomeInterface might not necessarily be a SomeClass; there is quite probably an AnotherClass class which implements SomeInterface: This code will fail to compile
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and you seem to have realised that the 3rd suggestion will fail to compile. And why. Well done.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that you have a better understanding of object references, how about trying to solve that little test I set a little while ago.
 
alex lotel
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the recurtion hell road to go threw
its a very important part of the test

so ill have to delay that after i will solve all the recursive questions
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic