Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

just trying to understand

 
Bhakundo Karki
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fellows, i am absolute beginner to Java and just wanted to understand few things from the example was looking at...

public class House {
long propertyNumber;
String propertyType;
byte Stories;
public int Bedrooms;
double MarketValue;
}


public class Main {
public static void main(String[] args) {
House property = new House();

property.propertyNumber = 283795;
property.propertyType = "Single Family";
property.Bedrooms = 4;
property.MarketValue = 652880;
}
}

ok in this example i was wondering (in the main class) why do we have to define a variable in a different class and refer to it would it not be easier if we just define the propertynumber and all others in the "main" class excluding class house? umm i cant even explain what i m not understanding :P
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you start from OO concepts here.

 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to Ranch

I suggest you should start with the link Vijitha has posted. Hope it helps you.
 
K. Tsang
Bartender
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. It's all to do with OO. Then encapsulation (making variables private access and have getter/setters)
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Freak ... property file concept is very useful when you will build a large project.
 
Karan Johar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Football Freak wrote:Hello fellows, i am absolute beginner to Java and just wanted to understand few things from the example was looking at...

public class House {
long propertyNumber;
String propertyType;
byte Stories;
public int Bedrooms;
double MarketValue;
}


This is called Encapsulation, where you hide all the properties of a House in a House class. Makes things easy to manage and relate together.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Football Freak wrote:


Please check your private messages for an important administrative matter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karan Johar wrote:This is called Encapsulation, where you hide all the properties of a House in a House class. Makes things easy to manage and relate together.
No that is called a mistake when you forget to mark the fields "private".
 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Football Freak wrote:
ok in this example i was wondering (in the main class) why do we have to define a variable in a different class and refer to it would it not be easier if we just define the propertynumber and all others in the "main" class excluding class house?


What if I have some project involving "houses" and would like to reuse your house code? If you have put everything needed to work with
houses in a House class instead of putting the variables and methods in your actual program, then it would be much easier for me to reuse
that abstraction/functionality. If I had to wait for you to extract all of the relevant variables and methods from your program code, it would
not be worth it (for me to wait, or for you to take the time to pull out all of the "house" stuff).

So as others have pointed out this boils down to the object-oriented principles of software reuse. A further aspect of this object-oriented
software reuse would be that if, in the future, you need to do something with some special type of house -- maybe a condo -- you could
start with the House class you already have and extend it with inheritance mechanisms.
 
Bhakundo Karki
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone! i think i will start with the link given for OO and thanks for all the responses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic