Results 1 to 9 of 9
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Question about abstract classes and methods

    if you have an abstract class with an abstract method. lets say the abstract method is:
    Java Code:
    public int getAnInstanceVariable();
    because the getAnInstanceVariable() doesn't actually access one of the private instance variable, how does your sub-class get access?
    this doesn't work:

    Java Code:
    sub-class
    
    public int getAnInstanceVariable()
    {
         return super.anInstanceVariable;
    }
    because if you call the abstract super method it hadn't done anything with that instance variable?

    do you have to also create the instance variable in the sub class, or is there a way to access it in the base-class?


    Thanks in advance?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Question about abstract classes and methods

    Declare the variable as protected in the top level class which will give the subclass access to it.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Question about abstract classes and methods

    whalah! Thank you!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Question about abstract classes and methods

    Hang on. I just realised why is the method abstract anyway? Why not simply make it a concrete method in the top level class? Unless you are trying to do somehting hinky. Please explain what you are trying to achieve not how you are trying to solve it.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Question about abstract classes and methods

    Quote Originally Posted by mwr1976 View Post
    whalah!
    Pssst! It's voila.

    I still think you are doing something wrong.

  6. #6
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Question about abstract classes and methods

    I am just trying to conceptually understand abstract classes and methods, so I am messing with a banking account exercise I found. We are not dealing
    with inheritance yet. I have an account class:

    Java Code:
    public abstract class Account
    {
    	protected int accountNum;
    	protected double accountBalance;
    	
    	public Account(int an)
    	{
    		accountNum = an;
    		accountBalance = 0;
    	}
    	
    	public void setAccountBalance(double b)
    	{
    		this.accountBalance = b;
    	}
    	
    	public abstract int getAccountNum();
    	
    	public abstract double getAccountBalance();
    }
    From what I read was that the reason you would use abstract methods so you could enforce the use of those methods in the sub-classes. So as I started doing this exercise I realized that I couldn't access the instance variable of the super-class, and calling the abstract methods would do no good because they never initialize the instance variables. The fact that I could modify them as protected went right over my head.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Question about abstract classes and methods

    OK, if this is just a learning exercise then go ahead. However if this was a real application then I would make getAccountNum and getAccountBalance concrete methods and not abstract.

  8. #8
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Question about abstract classes and methods

    Actually what is an example of a "real world situation" where you would want to use an abstract methods instead of concrete ones?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Question about abstract classes and methods

    Have a look at the Collections API.
    As an example, here is the AbstractList that sits behind most of the classes implementing List.

Similar Threads

  1. New to abstract methods and classes, few issues
    By monkeyhead in forum New To Java
    Replies: 3
    Last Post: 10-22-2011, 07:36 PM
  2. Replies: 12
    Last Post: 03-16-2010, 06:45 PM
  3. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 AM
  4. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 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
  •