Results 1 to 5 of 5
  1. #1
    ldb88 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Exclamation A few questions about Java and design

    T/F and an explanation if not T please

    1. All user-defined interfaces are subtypes of the type Interface.
    2. A method call is sent to an object and the method of the actual type is executed.
    3. A data model specifies the user interface.
    4. If classes B and C are subclasses of A, then any state variable of A is also a state variable of B and C.

    I looked in the JavaDocs at Sun, but couldn't find a type Interface anywhere, but I think I remember hearing something about it. I think you have to use dispatching for #2, and #3 I really have no clue. For #4, does the visibility matter, or is it a state variable in B and C even if it was declared private in A?

    Thanks.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    All user-defined interfaces are subtypes of the type Interface.
    Interfaces
    Using an Interface as a Type
    A method call is sent to an object and the method of the actual type is executed.
    Defining Methods
    A data model specifies the user interface.
    Models are usually designed to function independently of user interfaces.
    If classes B and C are subclasses of A, then any state variable of A is also a state variable of B and C.
    Controlling Access to Members of a Class
    Java Code:
    public class InheritanceTest {
        public static void main(String[] args) {
            A a = new A();
            System.out.println("a = " + a);
            B b = new B();
            System.out.println("b = " + b);
            C c = new C();
            System.out.println("c = " + c);
        }
    }
    
    class A {
        public    String fieldOne   = "field one";
        protected String fieldTwo   = "field two";
        private   String fieldThree = "field three";
                  String fieldFour  = "field four";
    
        public String toString() {
            String s = getClass().getName();
            return s + "[fieldOne:"   + fieldOne +
                       " fieldTwo:"   + fieldTwo +
                       " fieldThree:" + fieldThree +
                       " fieldFour:"  + fieldFour + "]";
        }
    }
    
    class B extends A {
        public String toString() {
            return "B[fieldOne:"   + fieldOne +
                    " fieldTwo:"   + fieldTwo +
    //                " fieldThree:" + fieldThree +
                    " fieldFour:"  + fieldFour + "]";
        }
    }
    
    class C extends A {
        public String toString() {
            return super.toString();
        }
    }

  3. #3
    ldb88 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    Ok.
    1. I didn't find anything on this.
    2. This should be true, right?
    3. This one would be false.
    4. B and C cannot directly access it, so it would not be considered a state
    variable, right? So false?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Language semantics is the problem here. I don't know where/how you are learning java.
    If I answered these from my (limited) point of view/language I would say:
    1. All user-defined interfaces are subtypes of the type Interface.
    Awkward way of asking. Probably, yes.
    2. A method call is sent to an object and the method of the actual type is executed.
    Strictly speaking, no. But probably yes. The jvm selects the method according to its name, return type and the number and type of arguments.
    3. A data model specifies the user interface.
    Agreed: false.
    4. If classes B and C are subclasses of A, then any state variable of A is also a state variable of B and C.
    A "state" variable is generally considered a member or instance variable in class scope. Subclasses can access public, protected and package-private (from within the package) variables of a superclass but not private variables. I tried to show this in the test app.
    So, generally speaking, yes, except for private variables. Access of *any* variable in the superclass, no.

  5. #5
    ldb88 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

Similar Threads

  1. 2 simple java questions
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 09:35 AM
  2. Replies: 0
    Last Post: 01-24-2008, 07:53 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
  •