Results 1 to 6 of 6

Thread: In which class I will define the member method?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    In which class I will define the member method?

    Hi.

    I'm brand new learner of java programming language. I am following a book Complete Reference by Herbert Schildt. Here is the first program which calculates the volume. The program is ok to me to understand. Now that the first in this book the author is using method in the same program.

    Code:
    class Box {
    double width;
    double height;
    double depth;
    }
    // This class declares an object of type Box.
    class BoxDemo {
    public static void main(String args[]) {
    Box mybox = new Box();
    double vol;
    // assign values to mybox's instance variables
    mybox.width = 10;
    mybox.height = 20;
    mybox.depth = 15;
    // compute volume of box
    vol = mybox.width * mybox.height * mybox.depth;
    System.out.println("Volume is " + vol);
    }
    }
    Now this code gives same program with function.
    Code:
    class Box {
    double width;
    double height;
    double depth;
    // display volume of a box
    void volume() {
    System.out.print("Volume is ");
    System.out.println(width * height * depth);
    }
    }
    class BoxDemo3 {
    public static void main(String args[]) {
    
    Box mybox1 = new Box();
    Box mybox2 = new Box();
    // assign values to mybox1's instance variables
    mybox1.width = 10;
    mybox1.height = 20;
    mybox1.depth = 15;
    /* assign different values to mybox2's
    instance variables */
    mybox2.width = 3;
    mybox2.height = 6;
    mybox2.depth = 9;
    // display volume of first box
    mybox1.volume();
    // display volume of second box
    mybox2.volume();
    }
    }
    Question:
    1. Why the function has been defined in Box class and not in the BoxDemo3 class?
    2. Is there any rule that in which class we will define our functions, i.e. in this case the Box class or the BoxDemo3 class?

    This is the point where I can't go ahead because of not picking this reason?
    Thanks.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: In which class I will define the member method?

    It will be highly appreciable if someone could guide me for java learning website or some other book. But fundamental one, not the intermediary or advance.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: In which class I will define the member method?

    Quote Originally Posted by colrh View Post
    Question:
    1. Why the function has been defined in Box class and not in the BoxDemo3 class?
    2. Is there any rule that in which class we will define our functions, i.e. in this case the Box class or the BoxDemo3 class?

    This is the point where I can't go ahead because of not picking this reason?
    Thanks.

    1) Because the function is an aspect of the cube. If the class had been a Sphere, the volume calculations would be a different formula. So by putting Volume in the Cube or Sphere class, it associates that formula with that class. It's a form of encapsulation. Consider also this: you now take the Cube class and re-use it in another application. Since the Volume method for Cube is already in it, it's just there. It doesn't need to be re-written. Additionally, if Volume was in the app and not the class, then you would need to extract the dimensions each time in order to pass them to the function to do the calculation. Or pass the object, but then that causes a level of dependency that can cause problems down the road.
    2) Not a rule per-se, but a design pattern. The idea is to keep like things together. Consider the ubiquitous car analogy. Specifically the engine. When you start the engine, you tell the engine to start. You don't really tell the car to start, you tell the engine to start. The Car is just a container that happens to hold an Engine and other objects. There might be a public interface on the Car that you use to start it, but at the end it's going to call Engine.start(); at some point... Engine is like your Cube and Car is like BoxDemo3 ... you don't care how the engine start code works... you just call it. Diesel engines work differently from a regular gas engine. But you don't care because you simply call .start and the Engine class then takes care of the internals on making it work. When you've got something as simple as a box/cube, it's hard to see why you might go this route, and it isn't until you get to more complex objects and operations that it becomes clearer why this pattern exists.


    Quote Originally Posted by colrh View Post
    It will be highly appreciable if someone could guide me for java learning website or some other book. But fundamental one, not the intermediary or advance.
    Actually what you probably need at this point isn't a book on Java but a book on the principles of Object Oriented Design/Programming. The good ones will be language agnostic
    https://www.google.com/search?biw=15....0.4hZvg6Ob6Ns
    The Head First one I've seen at my local library. I've passed on it because I've already got the principles down, but O'Reiley books tend to be decent.

    This is a good all-around design pattern book
    https://www.editioncare.com/design-p...rson-pdf-ebook

    It's been around for some time and still holds up today. In fact, I need to pull my copy back out and re-read some sections on it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    157

    Re: In which class I will define the member method?

    As I said in my first post that I am brand new to Java. I'm afraid to said that your comments on my question just passed over my head. I didn't get anything. I know I am not familiar with java thing but these wordings are really difficult to understand.

    I am just searching for these books you have mentioned. I have studied this Complete Reference Book in order to understand the basic concepts and I picked up at basic level that what are these things.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: In which class I will define the member method?

    In simpler terms, you want a function that will calculate the volume of a box.
    Since the function will be associated with the box, that is where the function belongs.
    If someone wants to add a box to their program and gets a copy of the box class code and the volume function is not there, then they have to track that down or rewrite it.
    All the methods and properties that are part of one object, in this case a box, should be included as part of that class so it is a single "hunk" of code that can be reused.

    As techgnome said, to calculate the volume of a different type of object, like a sphere or a pyramid, etc... would be a different formula. It makes sense to include the formula for a given object in a class that represents that object, i.e. you box class, a sphere class, a pyramid class, etc...

    The person who wants to create boxes, spheres and pyramids using those classes, shouldn't have to write all the different types of functions needed to calculate the volume of those objects. The function should be part of the object's class.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: In which class I will define the member method?

    Quote Originally Posted by colrh View Post
    As I said in my first post that I am brand new to Java. I'm afraid to said that your comments on my question just passed over my head. I didn't get anything. I know I am not familiar with java thing but these wordings are really difficult to understand.

    I am just searching for these books you have mentioned. I have studied this Complete Reference Book in order to understand the basic concepts and I picked up at basic level that what are these things.
    Stop thinking in terms of Java... that's part of what I was trying to get across to you. The questions you asked have nothing to do with Java. Nothing. It has to do with a design principle, something that applies to MANY languages. That's why I suggested the books I did, because they will tackle it from a language agnostic view.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width