Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Java
class Toyota
{
    int modelelNumber;
    
    public Car()
    {
        this.modelNumber = 2011;
    }
    
    public void setModelNumber(int modelNumber)
    {
        
        this.modelNumber = modelNumber;
    }
}

class Car
{
    public static void main(String []main)
    {
        Toyota newCar = new Toyota();
        
        newCar.setModelNumber(2012); <--------------- 1
        newCar.modelNumber = 2013;   <-----------------2
        
    }


}


Can you explain the difference between two of them. The line 1 and 2 that I am pointing to. Whats the difference between them. I know the first one is simply setting the model number. What about the second one. And whats the difference between them. We assume here that the member of the Toyota class is accessible in the Car class.
Thanks
Posted
Updated 18-Sep-14 22:40pm
v3
Comments
Sergey Alexandrovich Kryukov 18-Sep-14 21:23pm    
Excuse me, between what two? Could you draw a line between #1 and #2? It would be the best to separate it into 2 different pre elements. The code you show now cannot even compile.
Besides, the question about "difference" is incorrect. What would you mean by that?
How is that all related to "aliasing"?

Probably, you are mixing up two different things: 1) inheritance; 2) having two or more different references to an object.

—SA
xcelme 18-Sep-14 22:40pm    
I corrected my question and updated it. Thanks for your information and correcting me.

Your code is still wrong. The Car class should be the base of all the other car types, so Toyota would inherit Car. See http://docs.oracle.com/javase/tutorial/java/index.html[^] for full details abot classes and inheritance.
 
Share this answer
 
Comments
xcelme 21-Sep-14 10:44am    
First of all, there is nothing wrong with the code. Secondly please do not redirect to other sources here and try not to explain unless you know understand the concept genuinely. This is one of the disadvantage of forum where people give more of their opinions and less of their expertise and knowledge. Do you even know the concept of aliasing. Dude you need more help in Java than I. And dont comments on others questions arbitrarily just for the sake of increasing your reputation.
Richard MacCutchan 21-Sep-14 13:52pm    
First of all, the code is wrong; you obviously do not understand inheritance. Secondly, I gave you that link since it explains all about classes and inheritance in much greater detail than would fit on this page. And thirdly, I am offering this help free, if you don't want to accept it then that is your prerogative.
xcelme 21-Sep-14 19:37pm    
The point that I am making in above code is not about inheritance and sub classes. Read my question properly and look at the line number that I am pointing to. The classes are there just to make understand the whole code. I know that the code doesn't compile but its not the thing that I am trying to solve nor referring to. Read my question again and if you could then try to comprehend the two lines of codes that I am pointing to if it makes any sense to you.
Richard MacCutchan 22-Sep-14 2:57am    
OK, the difference: In the first, you call the setModelNumber method of the class, which sets the value of the ModelNumber property. In the second you set the property value directly. There is no real difference in terms of code and result. However, method 1 is the recommended way of doing it, so the class can hide the actual implementation, and also allows for more complicated code under the covers. One of the reasons I posted the link above was because this, and other features of classes, is covered in detail in the tutorials.
xcelme 22-Sep-14 11:51am    
Thats what I am looking for man. Thanks
One is field access, the other is a method call (properties also can have method calls for set and get).

The difference is if you need to do something else other than setting values like validation or computing other values while changing the one in question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900