Click to See Complete Forum and Search --> : Theory questions about Java


s3a
March 23rd, 2010, 08:21 PM
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!

nuzzle
March 24th, 2010, 12:08 AM
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.

dlorde
March 24th, 2010, 05:30 AM
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: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

s3a
March 24th, 2010, 07:21 PM
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:

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);
}
}

nuzzle
March 25th, 2010, 04:53 AM
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,

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% 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.

dlorde
March 25th, 2010, 05:30 AM
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 :thumb:

Education is not the filling of a pail, but the lighting of a fire...
W.B. Yeats

s3a
March 25th, 2010, 06:01 AM
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.


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?

ProgramThis
March 25th, 2010, 07:21 AM
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.

dlorde
March 25th, 2010, 08:13 AM
See The Java Tutorials : Arrays (http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html).

I cannot teach anybody anything, I can only make them think...
Socrates