| Author |
Can we declare more than one public class in single .java file
|
bunty wadekar
Greenhorn
Joined: Jan 24, 2007
Posts: 1
|
|
public abstract class Person { protected int dollars; protected int friends; public void makeDollar ( ){ dollars++; } public void makeFriend (){ friends++; } /* method that calculates level of happiness; to be implemented by subclasses ( differnet kinds of people derive their happiness from different things ) */ public abstract int getHappiness(); } public class GreedyPerson extends Person { public int getHappiness() { int happinessValue; int moneyWeight = 10; int friendsWeight = 1; happinessValue = (moneyWeight * dollars) + ( friendsWeight * friends); return happinessValue; } } public class SocialPerson extends Person { public int getHappiness() { int happinessValue; int moneyWeight = 1; int friendsWeight = 10; happinessValue = (moneyWeight * dollars) + ( friendsWeight * friends); return happinessValue; public static void main(String[] args){} } } I got the follwing error :: SocialPerson.java:1: class Person is public, should be declared in a file named Person.java public abstract class Person { ^ SocialPerson.java:23: class GreedyPerson is public, should be declared in a file named GreedyPerson.java public class GreedyPerson extends Person { ^ 2 errors
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
No, you can't. You should put each public class in a file named after that class -- as the error messages explain rather clearly.
|
[Jess in Action][AskingGoodQuestions]
|
 |
 |
|
|
subject: Can we declare more than one public class in single .java file
|
|
|