Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree3Likes

Thread: Object and Class

  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Object and Class

    They say that objects in java are just like real-life objects. Like a dog has a state and behavior. Similarly a java object has state(variable) and behaviour(method).

    My question what would a real-life example of a class?

    Or it is stupid to compare a class in java to an equivalent in real-life?

    Pardon my ignorance.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    Just use your example:
    Java Code:
    class Mammal {
       ...
    }
    
    class Dog extends Mammal  {
       private Color mainColor;
    
       public void setColor(Color c) {
          this.mainColor = c;
       } 
       
       public void bark() {
           //inner workings to bark go here: breatheIn() etc...
       }
    }
    You can go crazy with this concept:

    Java Code:
    class Shepherd extends Dog {}
    
    class BelgianShepherd extends Shepherd{}
    
    interface Domesticated {
       public String getName();
       public Person getOwner();
    }
    
    class Dougie extends BelgianShepherd implements Domesticated {
       public String getName() {
          return "Dougie";
       }
    
       public Person() getOwner() {
          return new SurfMan();
       }
    }
    As you can see I own a Belgian Shepherd named Dougie. Lovely dog. Very obedient, playful, loyal.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Object and Class

    You mean to say

    Real-Life Examples of
    class:Mammal
    object:Dog

    Thanks for your time.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Object and Class

    Quote Originally Posted by jmohandos304 View Post
    Or it is stupid to compare a class in java to an equivalent in real-life?
    The exact opposite actually. You should start to suspect bad code design if you have classes which do not have any kind of real-life representation.
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by jmohandos304 View Post
    You mean to say

    Real-Life Examples of
    class:Mammal
    object:Dog

    Thanks for your time.
    No. Both are classes. Even Dougie is a class, but d1 and d2 are objects:
    Java Code:
    Dougie d1 = new Dougie();
    Dougie d2 = new Dougie();
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Object and Class

    So if I say

    Java Code:
    Dog d=new Dog();
    Mammal-Class
    Dog-Class
    d-Object of Dog

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by jmohandos304 View Post
    So if I say

    Java Code:
    Dog d=new Dog();
    Mammal-Class
    Dog-Class
    d-Object of Dog
    Yes. Consider the class a blueprint, and the instantiated bits an object. The term usually used in Java is: "d is an instance of Dog".
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Object and Class

    I just hope that I do not confuse you now:

    A class is also an object. The class dog is an instance of Class.

    Or to take the blueprint example: a blueprint describes how something is build but it is also an object which you can touch, take, do something with it...

    Konrad

  9. #9
    R03
    R03 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Object and Class

    if i'm not wrong and per as my understanding..

    object = something you can use to extended..
    class = template.. a blue print from which individual objects are created.

    this is an example as my understanding..

    class = a house (object too)
    object = a room (can be class too). you can create another object with a room. brother room, sister room, parent room, young sister room, young brother room. etc

    a room have states and behaviour
    room states = dirty, messy, clean, etc
    room behaviour = book position in your room, etc


    CMIIW

    Regards,

    R03.

  10. #10
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Object and Class

    A class is also an object. The class dog is an instance of Class.
    What?? I think you are confusing with Object class which is superclass of all classes.

  11. #11
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Object and Class

    I don't think that I am confused about this.

    Java has a classloader which is responsible to load a Class. This loaded Class is an object/instance like all other objects in Java. It has methods and fields. So you can use it to get mor Information from it or even do Stuff with it like creating a new instance.
    If you want to look that up, then search for Reflection.

    You could also check the method Object.getClass(). This returns a Class instance. So classes inside the runtime environment are also simple instances.

    Konrad

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    For now, forget the "class is also an object" parts. I know what Konrad is talking about (and he is correct) but at this point when you are learning Object Oriented stuff, it's too much.

    Stick with the class= blueprint, and object = concrete instance of that blueprint.

    Quote Originally Posted by R03 View Post
    object = something you can use to extended..
    class = template.. a blue print from which individual objects are created.
    Close, but not quite. Extending is done with classes. Like in my example: Dog extends Mammal because Dog adds more behaviour and properties to it. It wouldn't make sense to do this: class Goldfish extends Mammal, since a Goldfish is not a Mammal.

    Quote Originally Posted by R03 View Post
    class = a house (object too)
    object = a room (can be class too). you can create another object with a room. brother room, sister room, parent room, young sister room, young brother room. etc
    Again close, but not quite. A House is a class, a Room is a class, the Bookshelf is a class. When you start building your house, then you instantiate your house: House myHome = new House(). Now "myHome" is an object of type House. If you build a Street class, you might have a method to add a House:
    Java Code:
    House myHouse = new House();
    Street ssb = new Street("Sunset Boulevard");
    ssb.addHouse( myHouse, 14); //14 is the number of the house.
    Quote Originally Posted by R03 View Post
    a room have states and behaviour
    room states = dirty, messy, clean, etc
    room behaviour = book position in your room, etc
    Again close, but not quite. Book position is state of the bookshelf, not behaviour of the room.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Object and Class

    Quote Originally Posted by SurfMan View Post
    For now, forget the "class is also an object" parts. I know what Konrad is talking about (and he is correct) but at this point when you are learning Object Oriented stuff, it's too much.
    Fully agreed. Keep it simple, there is plenty of time later to dive in deep later.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Object and Class

    Quote Originally Posted by SurfMan View Post
    Extending is done with classes.
    And Interfaces. One interface may extend another.

    Regards,
    Jim
    SurfMan likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by jim829 View Post
    And Interfaces. One interface may extend another.

    Regards,
    Jim
    Good catch.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Object and Class

    Thanks! I actually thought about this for a while. I didn't want to further complicate the explanations to the OP.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Object and Class

    Quote Originally Posted by jim829 View Post
    Thanks! I actually thought about this for a while. I didn't want to further complicate the explanations to the OP.

    Regards,
    Jim
    Well, it's his first steps in OO, so don't push it :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    R03
    R03 is offline Member
    Join Date
    Apr 2014
    Posts
    10
    Rep Power
    0

    Default Re: Object and Class

    Quote Originally Posted by SurfMan View Post
    For now, forget the "class is also an object" parts. I know what Konrad is talking about (and he is correct) but at this point when you are learning Object Oriented stuff, it's too much.

    Stick with the class= blueprint, and object = concrete instance of that blueprint.


    Close, but not quite. Extending is done with classes. Like in my example: Dog extends Mammal because Dog adds more behaviour and properties to it. It wouldn't make sense to do this: class Goldfish extends Mammal, since a Goldfish is not a Mammal.


    Again close, but not quite. A House is a class, a Room is a class, the Bookshelf is a class. When you start building your house, then you instantiate your house: House myHome = new House(). Now "myHome" is an object of type House. If you build a Street class, you might have a method to add a House:
    Java Code:
    House myHouse = new House();
    Street ssb = new Street("Sunset Boulevard");
    ssb.addHouse( myHouse, 14); //14 is the number of the house.

    Again close, but not quite. Book position is state of the bookshelf, not behaviour of the room.
    Hi Surfman,

    Thank you for correction and explaination..
    so, the key for creating the object is a "new" keyword, right ?

    i quite understood now.. thank you..

    R03.

  19. #19
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Object and Class

    Yes, that is correct. New instances are created with new.
    R03 likes this.

  20. #20
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: Object and Class

    I think I asked the following question but when I searched for it I could not find that thread. Hence I am posting the question here:

    Suppose I take the line:

    Java Code:
    A a = new A();
    We all know that constructor does not have a return value. Then how does the object "a" get initialized?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  4. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •