CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2009
    Posts
    45

    Theory questions about Java

    I've been reading my text book and I'm studying for an upcoming test and I have some questions. As you might notice my questions are pretty basic but that's because we're only beginning to cover this topic and therefore we won't have anything serious on inheritance on the test but it will be there mildly. Anyways, on to my questions (sorry if they're badly worded):

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1) If a class is not abstract but it has an abstract method, is it true that it still CAN NOT be instantiated? Or do abstract methods only exist inside abstract classes?

    2) The purpose of an abstract METHOD is to make sure the subclass overrides the method but what's the purpose of an abstract CLASS? Is the purpose of an abstract CLASS to be able to store the abstract METHODS? (that is IF I am right in saying that abstract methods HAVE TO BE in abstract classes)

    3) Imagine I had an array called, and set up as, array[rows][columns]. Doing array.length would give me the length of the rows but is array[].length the way to get the length of the columns?

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Any input would be greatly appreciated!
    Thanks in advance!

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Theory questions about Java

    Quote Originally Posted by s3a View Post
    1) If a class is not abstract but it has an abstract method, is it true that it still CAN NOT be instantiated? Or do abstract methods only exist inside abstract classes?
    Yes, abstract methods exist inside abstract classes (and interfaces). To be instantiated a class must be FULLY concrete.

    2) The purpose of an abstract METHOD is to make sure the subclass overrides the method but what's the purpose of an abstract CLASS? Is the purpose of an abstract CLASS to be able to store the abstract METHODS? (that is IF I am right in saying that abstract methods HAVE TO BE in abstract classes)
    If one method or more is abstract then the class MUST be declared abstract. If no method is abstract a class still CAN be declared abstract. The reason for the latter is to enforce inheritance of the class. You prevent the class from being directly instantiated.

    You have this continuum of increasing "abstractness":

    1. A fully concrete class.
    2. An abstract class with concrete methods only.
    3. An abstract class with a mix of concrete and abstract methods.
    4. A fully abstract class.
    5. An interface.

    Only 1 can be directly instantiated. All other must be inherited.

    4 and 5 are almost identical with the difference that interfaces can be multiply inherited.
    Last edited by nuzzle; March 24th, 2010 at 12:12 AM.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Theory questions about Java

    Quote Originally Posted by s3a View Post
    2) The purpose of an abstract METHOD is to make sure the subclass overrides the method but what's the purpose of an abstract CLASS? Is the purpose of an abstract CLASS to be able to store the abstract METHODS? (that is IF I am right in saying that abstract methods HAVE TO BE in abstract classes)
    In terms of your object model, an abstract class will contain code and/or data common to its subclasses, but won't make sense to instantiate itself. For example, when modelling animals, you might have Vertebrates as the abstract base class, divided into classes for Fish, Amphibians, Reptiles, Birds, Mammals, etc., but if you're going to be dealing with individual species, then the species would be the concrete instantiable classes, and all their superclasses would be abstract (i.e. you could instantiate a Tiger, but not a generic Mammal). Whether a class should be concrete or abstract generally depends on the level of detail in your model. Ideally, only the leaf classes (the most derived) should be concrete - subclassing concrete classes can cause problems and some gurus recommend avoiding it altogether.

    3) Imagine I had an array called, and set up as, array[rows][columns]. Doing array.length would give me the length of the rows but is array[].length the way to get the length of the columns?
    Almost right - you need to specify the row you want the length of, e.g:
    Code:
    int len = array[0].length;
    In principle, each row could be a different length (for example an array of words could be seen as an array of character arrays).

    The important thing in science is not so much to obtain new facts as to discover new ways of think about them...
    W. Brag
    Last edited by dlorde; March 24th, 2010 at 05:32 AM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Sep 2009
    Posts
    45

    Re: Theory questions about Java

    Thanks to both of you. But, I just have another question (which I got when read what dlorde said (or you - depending on who's reading this)): how can the length of the 2nd dimension of an array vary?

    Here is an example I made for myself and no matter the integer value of the "index" variable, the length is always the same:

    Code:
    public class temp
    {
    	public static void main(String[] args)
    	{
    		char[][] array = new char[5][10];
    		for(int index = 0; index < array.length; index++)
    			int theLength = array[index].length;
    		System.out.println(theLength);
    	}
    }
    Last edited by s3a; March 24th, 2010 at 07:37 PM.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Theory questions about Java

    Quote Originally Posted by s3a View Post
    how can the length of the 2nd dimension of an array vary?
    In Java a two-dimensional array is an array of arrays. You can set up a two-dimensional array like this for example,
    Code:
    int[][] a = new int[2][];
    a[0] = new int [5];
    a[1] = new int [10];
    (I don't have a compiler on my computer so I'm not 100&#37; sure about the syntax but it should be close)

    The outer dimension will be 2 and the inner will be 5 and 10. It's called a jagged array (C# has them too but not for example Fortran).

    This is general so an N-dimensional array is an array of arrays of arrays ...... of arrays. The innermost dimension arrays can vary in length.
    Last edited by nuzzle; March 25th, 2010 at 04:55 AM.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Theory questions about Java

    Quote Originally Posted by nuzzle View Post
    I don't have a compiler on my computer so I'm not 100% sure about the syntax but it should be close
    Spot on

    Education is not the filling of a pail, but the lighting of a fire...
    W.B. Yeats
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Sep 2009
    Posts
    45

    Re: Theory questions about Java

    I didn't know you can do that, thanks!

    Edit: I just tested to see if this works for one dimensional arrays and it doesn't.

    Code:
    public static void main(String[] args)
    	{
                   int[] a = new int[];
    		a = new int[2];
           }
    gives a syntax error saying "array dimension is missing." So, that "tricky thing" is only doable with multidimensional arrays?
    Last edited by s3a; March 25th, 2010 at 06:06 AM.

  8. #8
    Join Date
    Feb 2008
    Posts
    966

    Re: Theory questions about Java

    The problem with the code you have is that you are saying:

    int [] a = new int[];

    You are not supplying the size of the array. If you look at nuzzle's example, the outer dimension is set to 2 initially ( new int[2][]), and you don't have to set the other dimension(s) until later. With either array ( a single dimension or a 2d array, well any array really) you have to give it an initial size. Since the 2d array can be jagged, you only have to set the outer size and not the inner at the time of creation.

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Theory questions about Java

    See The Java Tutorials : Arrays.

    I cannot teach anybody anything, I can only make them think...
    Socrates
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

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

Featured