Results 1 to 8 of 8
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default why i cant access a method in a sub class?

    why i can access my method in the subclass???

    Java Code:
    <code>
    
    public abstract class StockItem  implements Comparable, Identifiable{
    	
    	protected int category; //category of the product could be electrical, dress, or food
    	
    	
    	public StockItem(int category){
    		this.category = category;
    	}
    
    de>
    Last edited by javanew; 05-03-2010 at 06:22 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    s[0].setColor("red"); //<<<<<<<<error .. it cant access the method setColor in the abstract clas
    Because that abstract class doesn't have a setColor( ... ) method; neither abstract nor concrete.

    kind regards,

    Jos

  3. #3
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    Look at the concrete method in the concrete class DRESS ,, it has the setcolor method()

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    Look at the concrete method in the concrete class DRESS ,, it has the setcolor method()
    Yes I know but your array s is an array of StockItems and that class doesn't even mention the existence of that method so it can't be overridden in a subclass.

    kind regards,

    Jos

  5. #5
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    this is polymorphism,,,,
    can u help me what to do???
    write me a code please

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    this is polymorphism,,,,
    can u help me what to do???
    write me a code please
    A possibility is to add an abstract method setColor( ... ) in your StockItem class but then all the subclasses thereof are either abstract or have to implement that method.

    kind regards,

    Jos

  7. #7
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    i dont want to add an abstract method,,,
    the concrete class Dress conatains a method called setColor
    how can i access it from the driver:confused:

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    i dont want to add an abstract method,,,
    the concrete class Dress conatains a method called setColor
    how can i access it from the driver:confused:
    You edited your OP so it can't be seen anymore but you defined a few other abstract methods in that abstract superclass, what is against adding one more? Now StockItems don't have a setColor( ... ) method so you can't set the color when you have a StockItem. Only you know that you have a Dress, the compiler doesn't know that so it whines about it.

    kind regards,

    Jos

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  2. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:54 AM
  3. Annotations to change access level of method
    By jwilley44 in forum Advanced Java
    Replies: 1
    Last Post: 02-16-2009, 06:52 AM
  4. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •