Kevin Foulger

Greenhorn
+ Follow
since Jan 08, 2008
Merit badge: grant badges
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin Foulger

Thanks. I've read and bookmarked it so I can refer back to it. It's good to have basic descriptions as is shown on those threads.
The best thing you can do is to get the SCJP Study Guide by Kathy Sierra and Bert Bates. It's all in there.
Hi,

I think I've just about understood Overloading and Overriding. Now I've just read about Redefining (as in redefining a Static method) on P. 147 of the Study Guide.
It seems very similar to overloading. What's the difference please?

Is there an easy way to get to grips with all these different concepts?

Thanks.

Kevin
Hi,

I've just seen the SCJP Java 6 book will be on sale on a web bookshop from 1st July 2008:

SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-055) (Paperback) by Katherine Sierra (Author), Bert Bates (Author)

I'm eager to know if there will be much difference between this and SCJP 5, which is what I'm currently working through.

Thanks

Kevin
I have just written three classes, Fruit (abstract) Fig and Apple. They all work fine and the package set up is OK.

Is the juice method in my Fig class overloading or overriding the juice method in my Fruit class? They don't have different argument lists, but they are in different classes.

How is it that I can have two methods called "juice" when they appear to have the same (or in my case don't have any) argument list? (Head First Java page 191). SCJP Study guide also says I MUST change the argument list for it to be overloaded (page 105). Page 106 implies that in this case, they are overloaded because they're in different classes.

They still both work fine even when taken out of the package. How does Java know the difference between the two juice methods?

Thanks.

Here are the classes:

// Fruit.java

package food;
public abstract class Fruit {

public static void fruity() {

System.out.println("This is a fruit");

}

public static void juice() {

System.out.println("This is juicy");

}
}

// Fig.java

import food.Fruit;
class Fig extends Fruit {

public static void main(String args[]) {
System.out.println("This is a fig");
fruity();
juice();
}

public static void juice() {

System.out.println("This is NOT as juicy");

}

// juice method say "This is NOT as juicy"

}


// Apple.java

import food.Fruit;
class Apple extends Fruit {

public static void main(String args[]) {
System.out.println("This is an Apple");
fruity();
juice();
}

// juice method says "This is juicy" (from the abstract Fruit class)

}
Hi Ulf.

Thanks. I understand now the calling business and the data types. I see that you can call the methods from either one place, or after each method.
5.0 is a double calling the double 800 main method and the 5.0f is a float calling the float f main method.

This now works fine. Thanks again.

class maincheck
{
public static void main(String args[])
{
System.out.println("This is for String");
main(5);
}

public static void main(int x)
{
System.out.println("This is for Integer:");
main(5.0);
}

// customizing from here onwards

public static void main(double y)
{
y = 800;
System.out.println(y);
main();
}

public static void main()
{
System.out.println("I can't believe this compiles and runs!");
main(5.0f);
}

public static void main(float f)
{
System.out.println("Now it prints!");
}

// The result is:
// This is for String
// This is for Integer:
// 800.0
// I can't believe this compiles and runs!
// Now it prints!

}

Thanks Ulf for your welcome and your answers. I'm still finding my way around the JavaRanch.

I didn't realise I had to call these particular methods. What is puzzling me is that this piece of code prints some output:

public static void main(int x)
{
System.out.println("This is for Integer:");
}

But, for example, this piece of code doesn't have any output:

public static void main()
{
System.out.println("I can't believe this compiles and runs!");
}

The two methods (as all the moethods in my class) appear to be very similar. None of them seem to be getting called anywhere, yet the first two have output, but the others don't. Please could you briefly explain why? Thanks.
This refers to a mock question ID:4101281 on the uCertify PrepKit

This question gave me a class with two main methods in it.
What I didn�t realise, was that one of the main methods had been overloaded.

I�ve now looked into overloading and played around with the code and realised I can have at least five main methods. I thought at first you could only ever have one.

Is there a main (parent � Mother of Main) method in the code or are they all main methods?

How can I tell which methods will produce results and which won�t?

How can I tell when a main method will not compile?

Lastly, why is MAIN not a keyword?

Any advice or discussion gratefully accepted. Thanks.

Here�s the code. The first part is straight from the practice question referred to above.

class maincheck

// These two main methods print

{
public static void main(String args[])
{
System.out.println("This is for String");
main(5);
}

public static void main(int x)
{
System.out.println("This is for Integer:");
}

// customizing from here onwards

// This should print 800, but doesn't - why?

public static void main(double y)
{
y = 800;
System.out.println(y);
}

// This doesn't print either - why?
public static void main()
{
System.out.println("I can't believe this compiles and runs!");
}

public static void main(float f)
{
System.out.println("This won't print");
}

// The result is:
// This is for String
// This is for Integer:
}
Enums allow you to make a list of values, like an array and these can be assigned to a variable. The order the values are listed is taken into account, just as they are in an array. The values are constants and should be in capital letters.
You can treat the values in enums like objects and assign instance variables to each of those objects.

Here is an example that I�ve customized myself from an example in the SCJP Exam Study Guide (Page 63 & 64). I�ve looked into this subject and taken the code to pieces to try and understand the subject myself. I�ve also made the code a little more complicated than it is in the book. I�m currently studying for the exam.

Save this in a file called Engines.java in your java directory:

public class Engines {

enum EngineSize {
SPORTS_CAR(2.0),
SALOON(1.6),
OFF_ROADER(4.0);

EngineSize(double litres) {
this.litres = litres;
}

public double litres;

public double getLitres() {
return litres;
}
}
}


Save this in the same directory, in a file called Car.java

class Car extends Engines {
EngineSize size;

public static void main(String[] args) {

Car engine1 = new Car();
engine1.size = EngineSize.SPORTS_CAR;

Car engine2 = new Car();
engine2.size = EngineSize.SALOON;

Car engine3 = new Car();
engine3.size = EngineSize.OFF_ROADER;

System.out.println("The Sports Car's engine size is " + (engine1.size.getLitres()) + " litres");
System.out.println("The Saloon's engine size is " + (engine2.size.getLitres()) + " litres");
System.out.println("The Off Roader's engine size is " + (engine3.size.getLitres()) + " litres");

}
}


End of code

Compile both the files separately and run Car.java. You have to run this, because it contains the Main method.

16 years ago