File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes in what ways can we change this formula.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "in what ways can we change this formula.." Watch "in what ways can we change this formula.." New topic
Author

in what ways can we change this formula..

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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 ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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 ]

Ping & DNS - updated with new look and Ping home screen widget
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
and what is the differece between these two objects??
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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??
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 279
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 ]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Two very common examples:



Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
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

Joined: Jul 31, 2001
Posts: 279
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 01, 2008
Posts: 191
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 01, 2008
Posts: 191
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

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 01, 2008
Posts: 191
but what about the methods

how many methods our object has???
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 01, 2008
Posts: 191
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

Joined: Jul 31, 2001
Posts: 279
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

Joined: Feb 01, 2008
Posts: 191
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

Joined: Jul 31, 2001
Posts: 279
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

Joined: Feb 01, 2008
Posts: 191
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
. . . and you seem to have realised that the 3rd suggestion will fail to compile. And why. Well done.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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

Joined: Feb 01, 2008
Posts: 191
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: in what ways can we change this formula..
 
Similar Threads
Lack of concentration
Getting Version Numbers
Overloading interface methods
question exam lab. thread, sleep
Inheritance confusion