Bit of Java Thoery

Associate
Joined
3 Oct 2006
Posts
2,304
Location
London
Bit of Java Theory

Hi There,

I'm just going over my Java programming notes (in preparation for completing a bit of coursework) and have a little query regarding Abstract Classes.

My notes say that not all methods in an abstract class have to be abstract. Could anyone enlighten me as to why this is the case and the pros and cons and what not having some methods as abstract allows you to do?

Many thanks

Jon
 
Last edited:
If all your derived classes should have a function which has the same functionality then putting it in the base class makes sense, rather than putting the same function in every derived class.

For example, if you just wanted your toString() method to return what type they are then
Code:
 public String toString(){ return getClass().getName(); }
in your base class will suffice.
 
Hi There,

I'm just going over my Java programming notes (in preparation for completing a bit of coursework) and have a little query regarding Abstract Classes.

My notes say that not all methods in an abstract class have to be abstract. Could anyone enlighten me as to why this is the case and the pros and cons and what not having some methods as abstract allows you to do?

Many thanks

Jon

Using an abstract class allows you to provide functionality at a generic level (through concrete methods), while allowing you to delegate some functionality to classes that derive from it (via abstract methods). In other words, abstract methods allow the base class to sepcify *what* derived classes should do, but not *how* they do it.
 
Thanks both for your input. Having just been for lunch and reading through my notes and how you have both explained it, I now understand the concept. It's also led me to conclude that for objects that will be simply used to store data (which is all these objects will be doing) and coupled with the fact they wont be performing anything more complex than get/set for the data, an abstract super-class is not really necessary.

Thanks for your help!
 
There are other advantages to using a superclass or interface too, such as storing objects in a generic collection and calling the methods without needing to cast. That is assuming you are going to have different kinds of objects,

e.g.

public interface Car {
public String getModel();
}

public class Ford implements Car {
public String model = "Ford";

public String getModel() {
return model;
}
}

public class Vauxhall implements Car{
private String model = "Vauxhall";

public String getModel() {
return model;
}
}

public class Test {
public static void main(final String args[]) {
Car x = new Ford();
Car y = new Vauxhall();

Vector<Car> cars = new Vector<Car>();
cars.add(x);
cars.add(y);
for(Car car : cars) {
System.out.println(car.getModel());
}
}
}

I realise you were not asking about interfaces directly, but an abstract superclass can be used in the same way, with the addition of possible concrete classes.
 
Last edited:
Hi, thanks for your response. I was just getting up to this part in my notes and I'm now changing my mind again. *Sigh* why do there have to be so many choices when programming!

I'll probably ask a question about what you have posted later but for now I have another unrelated question about overloaded constructors.

Code:
	// Empty AstroObject constructor
	public AstroObject() {
		this(0.0, 0.0, 0.0, 0.0);
	}
	
	// Empty AstroObject constructor
	public AstroObject(double ra, double d, double m, double dfe) {
		this.setRightAscension(ra);
		this.setDeclination(d);
		this.setMagnitude(m);
		this.setDistanceFromEarth(dfe);
	}

What would happen if I were to try and create an instance of this class with only the first two parameters present? Would the compiler kick out an error and tell me to sort it out?
 
There are other advantages to using a superclass or interface too, such as storing objects in a generic collection and calling the methods without needing to cast. That is assuming you are going to have different kinds of objects,

e.g.

Code:
public interface Car {
public String getModel();
}

public class Ford implements Car {
	public String model = "Ford";
	
	public String getModel() {
		return model;
	}
}

public class Vauxhall implements Car{
	private String model = "Vauxhall";
	
	public String getModel() {
		return model;
	}
}

public class Test {
	public static void main(final String args[]) {
		Car x = new Ford();
		Car y = new Vauxhall();
		
		Vector<Car> cars = new Vector<Car>();
		cars.add(x);
		cars.add(y);
		for(Car car : cars) {
			System.out.println(car.getModel());
		}
	}
}

I realise you were not asking about interfaces directly, but an abstract superclass can be used in the same way, with the addition of possible concrete classes.

Your last comment is spot on...the example you've given would be better coded as a hasModel interface (with just a getModel method), implemented in the Car class, which would be abstract.

The fact that your getModel methods are identical (and both use the model member variable) is a sure sign that the functionality should reside in a base class
 
Hi,

Personally I'd use an abstract class where the inherited classes satisfied the IsA relationship to the abstract class. In the case where the classes didn't satisfy the IsA relationship e.g they're not more specific types of the abstract class, and needed the same methods they would both implement the same interface(s).

Another advantage of interfaces is that a class can only have a single superclass but can implement many interfaces.

I think that's about right....

Jim
 
Ok, so I have another question for you kind fellows at OcUK

I have an ArrayList

Code:
List <Star> stars = new ArrayList<Star>();

I then populate the stars ArrayList with the following code:

Code:
BufferedReader in = new BufferedReader(new FileReader(starFile));
String line;
while ((line = in.readLine()) != null) {
	
	StringTokenizer tok = new StringTokenizer(line, "|");
	int value1 = Integer.parseInt(tok.nextToken().trim());
	double value2 = Double.parseDouble(tok.nextToken().trim());
	double value3 = Double.parseDouble(tok.nextToken().trim());
	double value4 = Double.parseDouble(tok.nextToken().trim());
	double value5 = Double.parseDouble(tok.nextToken().trim());
	String value6 = tok.nextToken().trim();
	String value7 = tok.nextToken().trim();
	System.out.println(value1);
	System.out.println(value2);
	System.out.println(value3);
	System.out.println(value4);
	System.out.println(value5);
	System.out.println(value6);
	System.out.println(value7);
	Star tempStar = new Star(value1, value2, value3, value4, value3, value6, value7);
	stars.add(tempStar);
	
	
}

Using the collections framework is there anyway I can sort the ArrayList via one of variables each of the object in the ArrayList has?

For example, each Object in the ArrayList has the attribute DistanceFromEarth, which can be accessed by the method getDistanceFromEarth. How would I go about sorting the ArrayList depending on the DistanceFromEarth attribute and thus allowing me to work out which is the closest or furthest star?
 
Last edited:
Hi another one for you. I'm having a bit of a problem with an ArrayList im creating.

my stars ArrayList is full of stars objects, each of which have a constellation attribute, a Three character string. I'm trying to create a list of all the unique constellations.

This is my current Code.

Code:
List<String> constellations = new ArrayList<String>();
		boolean isThere;
		for ( int i=0; i<stars.size(); i++ ) {
			
			isThere = false;
			
			for ( int j=0; j<constellations.size(); j++ ) {
				
				if (stars.get(i).getConstellation() == constellations.get(j)) {
					isThere = true;
				}
				
			}
			
			if (isThere == false) {
				constellations.add(stars.get(i).getConstellation());
			}
		}
		
		System.out.println(constellations);

Here is my current output.

Code:
[Phe, Oct, Psc, Psc, Peg, Scl, Psc, Cet, Cas, Cet, Psc, Peg, Cas, And, Peg, Cas, Scl, Phe, Psc, Oct, And, Cet, Scl, Scl, Peg, And, Peg, Cet,
 And, Cet, Tuc, Psc, Tuc, And, Scl, Cas, Psc, Hyi, Phe, Phe, Scl, Psc, Cet, And, Cet, Phe, Tuc, Tuc, Cas, Psc, Psc, Tuc, Cas, Cet, Cet, Phe, Psc,
 And, Cas, And, And, And, Psc, Psc, Cas, And, Phe, Phe, Cas, Tuc, Phe, Cas, Cet, Cet, Scl, Cas, Cas, Psc, Psc, And, Psc, Cas, Psc, Hyi, Psc, Cas, 
Psc, Cas, And, Psc, Cet, Phe, Cas, Cet, Psc, And, Cas, Tuc, Cet, Cas, Cas, And, And, Psc, Scl, Cet, Scl, Scl, Psc, And, Psc, Cet, Psc, Psc, Psc, 
Phe, Cet, Tuc, Cet, Phe, Psc, And, Cas, Psc, Phe, Cet, Cep, And, And, Psc, And, Cet, Cas, Cas, Psc, And, Psc, Psc, Cas, Cet, Psc, Psc, Psc, Cet, 
Cet, Phe, Tuc, Cet, Psc, Psc, Cet, Cas, Psc, And, And, Cet, Cet, Cas, Cas, Psc, Psc, Cet, And, Phe, Cet, And, Psc, Cas, Phe, Psc, Cas, Cet, Cet, 
Scl, And, Psc, Eri, And, Cas, And, Psc, And, Psc, Scl, Cas, Psc, Cas, Cas, Per, Cet, Psc, Scl, Ari, Cet, Ari, Cet, Per, Per, Tri, And, Ari, Psc, Phe, Phe, 
Cas, Ari, Hyi, Ari, And, Eri, Cas, And, Cet, Ari, Ari, Per, Hyi, Cet, Cet, Psc, For, Phe, Cas, Psc, Per, Tri, Cas, Cet, Cas, Ari, Cet, And, For, Cas, Cas, 
Ari, Ari, And, Ari, Tri, Ari, Cet, Cet, Tri, Cet, Ari, For, Cet, Ari, And, Per, Hyi, Hyi, Ari, Ari, Tri, Eri, Cet, Tri, Tri, Per, Per, Ari, Tri, And, And, Hyi, Cet, Cet, 
Per, For, Hyi, And, Ari, Hor, And, Cet, Eri, Tri, For, Cet, Tri, Tri, Cas, Hyi, UMi, Cet, Tri, Cet, For, For, Cet, Cet, Cet, For, Ari, For, Hor, Cet, For, Ari, 
Cet, Cet, Hyi, Eri, Hor, Eri, Ari, Cet, Per, Ari, Hor, Per, Cet, Ari, Per, Cet, Per, Ari, Cet, Ari, Eri, Hor, Hyi, Ari, Ari, Hor, For, Ari, For, Ari, For, Hyi, Per, 
For, Per, Eri, Ari, Per, For, Per, Per, Ari, Eri, Ari, Per, Eri, Ari, Eri, Eri, Per, Hor, Per, Ari, For, Eri, Cet, Eri, For, Ari, Hyi, Cet, Cet, Eri, Eri, Hor, Eri, Per, 
Per, Ari, Per, Per, Per, Ari, Per, Ari, For, Cet, Ari, Eri, Hyi, Ret, Per, Ret, Eri, Cet, Per, Per, Cet, Eri, Ari, Cep, Cet, Ari, Per, Ari, Ari, Ari, Per, Tau, Tau, 
For, Per, Ret, Tau, Per, Eri, Tau, Per, Tau, Eri, Eri, Tau, Eri, Per, Tau, Eri, Tau, Eri, For, Tau, Per, Per, Eri, Ret, Per, Eri, Tau, Tau, Eri, Tau, Per, Tau, 
Tau, Tau, Tau, Eri, Tau, For, Eri, Hyi, Tau, Eri, For, Tau, Tau, Tau, Per, Cam, Tau, Eri, Eri, Per, Eri, Tau, Per, Eri, Ret, Per, Eri, Tau, Ret, Ret, Eri, Tau, 
Tau, Tau, Tau, Tau, Per, Tau, Tau, Per, Per, Tau, Eri, Tau, Hor, Tau, Eri, Tau, Tau, Hor, Eri, Ret, Per, Per, Eri, Tau, Dor, Ret, Tau, Ret, Eri, Men, Tau, 
Tau, Tau, Tau, Tau, Tau, Per, Tau, Men, Ret, Tau, Tau, Tau, Tau, Eri, Tau, Eri, Tau, Per, Per, Tau, Tau, Tau, Tau, Tau, Tau, Tau, Tau, Tau, Tau, Tau, Tau, 
Tau, Tau, Tau, Tau, Cae, Eri, Eri, Tau, Eri, Dor, Eri, Eri, Tau, Tau, Eri, Eri, Tau, Tau, Eri, Tau, Tau, Cam, Cam, Tau, Eri, Cae, Cae, Tau, Pic, Per, Men, 
Eri, Eri, Dor, Eri, Eri, Cam, Eri, Ori, Aur, Eri, Ori, Pic, Ori, Tau, Cam, Ori, Aur, Eri, Ori, Cam, Ori, Ori, Ori, Cam, Men, Ori, Eri, Aur, Cam, Tau, Tau, Ori, 
Men, Aur, Eri, Eri, Aur, Cep, Eri, Aur, Aur, Men, Lep, Pic, Tau, Cam, Cae, Ori, Pic, Lep, Dor, Cam, Aur, Aur, Eri, Tau, Tau, Eri, Ori, Tau, Tau, Eri, Eri, 
Ori, Ori, Lep, Lep, Lep, Ori, Aur, Dor, Ori, Aur, Ori, Aur, Col, Ori, Aur, Aur, Ori, Tau, Pic, Lep, Lep, Aur, Ori, Aur, Ori, Cam, Lep, Ori, Tau, Ori, Ori, Aur, 
Ori, Ori, Tau, Dor, Ori, Tau, Tau, Aur, Tau, Tau, Lep, Tau, Ori, Cam, Ori, Lep, Col, Ori, Men, Ori, Ori, Tau, Aur, Lep, Col, Tau, Dor, Ori, Ori, Ori, Ori, Ori, 
Ori, Ori, Tau, Ori, Ori, Ori, Tau, Men, Tau, Col, Aur, Ori, Ori, Ori, Col, Tau, Ori, Tau, Lep, Ori, Lep, Dor, Aur, Col, Cam, Lep, Tau, Pic, Tau, Tau, Ori, Ori, 
Tau, Aur, Tau, Pic, Dor, Men, Tau, Col, Aur, Lep, Ori, Aur, Tau, Ori, Col, Tau, Ori, Aur, Ori, Cam, Ori, Col, Col, Lep, Col, Tau, Ori, Ori, Mon, Col, Aur, 
Aur, Aur, Aur, Aur, Mon, Ori, Ori, Gem, Ori, Ori, Lep, Aur, Dor, Aur, Col, Ori, Lep, Col, Dor, Cam, Men, Pic, Dor, Gem, Aur, Ori, Ori, Ori, Cam, Ori, 
Mon, Gem, Aur, Ori, Cam, Ori, Ori, Col, Ori, Lyn, Lyn, Mon, CMa, Aur, Col, Dor, CMa, Pic, Gem, Mon, Car, Aur, Dor, Lyn, Mon, CMa, Aur, Mon, Gem, 
Aur, Lyn, CMa, Pic, Mon, Mon, CMa, Aur, CMa, CMa, Lyn, Lyn, Gem, Pup, CMa, Aur, Aur, Aur, Aur, Men, Mon, Gem, Aur, Gem, Gem, CMa, Gem, 
CMa, Gem, Lyn, Aur, Lyn, CMa, Mon, Aur, Mon, Pic, Gem, CMa, Pup, Gem, Aur, Cam, Vol, Gem, Gem, Lyn, CMa, Cam, CMa, CMa, Gem, CMa, 
Gem, CMa, CMa, CMa, Aur, Men, Lyn, Lyn, CMa, Gem, CMa, Gem, Mon, CMa, CMa, Gem, Gem, CMa, Vol, Vol, Mon, Gem, Gem, Mon, Aur, Mon, 
CMa, Gem, Gem, CMa, Gem, CMa, Lyn, Gem, Vol, Pup, Aur, Gem, CMa, CMa, Gem, Gem, Aur, Lyn, Gem, CMa, Aur, Gem, CMi, Men, CMi, Gem, 
Lyn, Gem, CMi, Gem, CMi, CMi, Gem, Pup, Gem, CMi, Gem, Lyn, CMi, CMi, Gem, CMi, Gem, Gem, Mon, Gem, Gem, CMi, Gem, Mon, Vol, Lyn, 
Gem, Pup, Gem, Gem, Gem, Pup, Gem, CMi, Cam, Gem, Pup, Pup, Pup, Pup, CMi, Pup, Pup, Gem, Lyn, Gem, Car, Pup, Cnc, CMi, Pup, Mon, Cnc, 
Cnc, Mon, Cnc, Gem, Pup, Cnc, Cnc, Pup, Cnc, Vol, Lyn, Mon, Pup, Vel, Cnc, Pup, Pup, Cnc, Cnc, Pup, Cnc, Lyn, Cha, Vol, Vol, Cnc, Lyn, Cnc, Cha, 
Vol, Car, Lyn, Cnc, Hya, Vol]

As you can see I have multiples of the same constellation and I only want one of each. Can anyone spot where I'm going wrong? Many Thanks.
 
Last edited:
Problem solved on another forum:

you can use the 'contains' method available for collections.
I think it does not work because of == , you should use equals instead.

Try this code :

Code:
List<String> constellations = new ArrayList<String>();
		
                // loop on stars
		for ( int i=0; i<stars.size(); i++ ) {
			// add if it is not already there
			if(!constellations.contains(stars.get(i).getConstellation())) {
				constellations.add(stars.get(i).getConstellation());
			}
		}
		// showing results
		System.out.println(constellations);
 
Back
Top Bottom