• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we declare more than one public class in single .java file  RSS feed

 
bunty wadekar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't. You should put each public class in a file named after that class -- as the error messages explain rather clearly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!