• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

simple java question

Joined
Oct 11, 2001
Messages
581
ive been leqrning java for a couple of weeks now (originally learnt c) i just have a query about classes. If im going to create a 'patient' class (info on patients of a medical practice) how do i go about making sub classifications in the class for things like name and address (like in c how you have sup structures).

qould i just make a superclass of 'Patient' with two sub classes which are just used from the superclass?

that seems obvious, just wondering if there was a better way to do it?
 
I think this is what you are asking.

Code:
public class Patient
{
  private String myFName;
  private String myLName;
  private Address myAddress;
  . . .
}

public class Address
{
  private String myCity;
  . . .
}
 
No. It sounds like you don't understand what extending does and what super classes and base classes are used for.

Consider the following

Code:
public abstract class Employee
{
  private String myFName;
  private String myLName;
  private Address myAddress;
}

public class Engineer extends Employee
{
}

public class Manager extends Employee
{
}

You use super classes to define some basic functionality. A company has many employees. All employees are going to have a name and address for example. Then their are different types of Employees such as an Engineer and Manager. Each of these different types are going to have information that is unique to them but they also still have the basic information that is defined in Employee.

Note - Employee doesn't necessarily have to be abstract
 
Originally posted by Harry_Hardcore
wouldnt i have address and name as a class that extends patient?
it can take a while to fully understand how classes work. i know it took me some time, of course i've never really implemented any kind of complex class, only in cs2.....

anyway...

i haven't done any c programming, only minimal c++. i get the idea that c doesn't have actual classes, i've only heard the term struct thrown around a couple times.

here's an even more expanded idea of what Stupendous posted (i can't remember why i'm posting this, not sure if it actually makes things any clearer)
Code:
public class Patient
{
  private String myFName;
  private String myLName;
  private double mySalary;
  private Address myAddress;
  . . .

  // Accessor
  public String getMyFName()
  {
       return this.myFName;
  }
  . . .

  // Mutator
  public void giveRaise(double addAmount)
  {
      this.salary += addAmount;
  }

    // Subclass
    private class Address
    {
      private String line1;
      private String line2;
      private String myCity;
      . . .

      public String toString()
      {
        return this.line1 + "\n" + this.line2 + "\n" + this.myCity;
      }
    }
}
note that you can have a private class within employee for the address (instead of just making several more String instance variables within the employee class). it's often called an inner or nested class but it is not a sub class of employee, it's simply within employee.

you may declare them as seperate classes like Stupendous originally did, but then you would need a seperate file for the address class. it's simplier imho to put it as a nested class. also, there probably aren't other needs for the address class except within the employee class.
 
Back
Top