Results 1 to 5 of 5
- 12-06-2007, 03:24 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
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.
- 12-06-2007, 08:56 PM #2
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(); } }
- 12-06-2007, 09:45 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
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?
- 12-06-2007, 10:09 PM #4
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.
- 12-07-2007, 12:51 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
2 simple java questions
By jimJohnson in forum New To JavaReplies: 2Last Post: 02-02-2008, 09:35 AM -
www.javaadvice.com - The one stop resource for all your Java questions and answers
By JAdmin in forum Reviews / AdvertisingReplies: 0Last Post: 01-24-2008, 07:53 PM -
few java questions
By hiaslpix in forum New To JavaReplies: 4Last Post: 01-01-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks