Author Topic: Inheritance  (Read 5163 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Inheritance
« on: February 06, 2006, 04:40:18 PM »
The topic on inheritance contained a lot of new concepts: constructors, overloading, overriding and leads to polymorphism.

The base class for phone:

Code: [Select]

/**
 *  A base class for phones
 */
public class Phone
{
protected String telNo;
protected int areaCode;

public Phone()
{
telNo = "not assigned";
areaCode = 0;
}

public Phone(String telNo, int areaCode)
{
this.telNo = telNo;
this.areaCode = areaCode;
}

public void setTelNo(String newTelNo)
{
telNo = newTelNo;
}

public void setAreaCode(int newAreaCode)
{
areaCode = newAreaCode;
}

public void startCall()
{
System.out.println("Inside Phone : Call has been started .. ");
}

public void endCall()
{
System.out.println("Inside Phone : Call has ended.");
}
}

Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
a subclass cellphone for the super class phone
« Reply #1 on: February 06, 2006, 04:43:21 PM »
Code: [Select]


/**
 * A class that derives from phone
 */
public class CellPhone extends Phone
{
private String brand, model;

public CellPhone()
{
super();
brand = "not specified";
model = "not specified";
}

public CellPhone(String telNo, int areaCode, String brand, String model)
{
this.telNo = telNo;
this.areaCode = areaCode;

this.brand= brand;
this.model = model;
}

public void setBrand(String newBrand)
{
brand = newBrand;
}

public void setModel(String newModel)
{
model = newModel;
}

public String getModel()
{
return model;
}

public String getBrand()
{
return brand;
}

/**
* overrides the startCall method of Phone
*/
public void startCall()
{
System.out.println("Finding nearest cellsite...");
System.out.println("A call has started.");
System.out.println();
}

public void sendText()
{
System.out.println("A text message was sent from  " + telNo);
System.out.println();
}

public void info()
{
System.out.println("Tel No    : " + telNo);
System.out.println("Area Code : " + areaCode);
System.out.println("Brand     : " + brand);
System.out.println("Model     : " + model);
System.out.println();
}
}

Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
A subclass fixedphone of the superclass phone
« Reply #2 on: February 06, 2006, 04:45:17 PM »
A subclass fixedphone of the superclass phone

Code: [Select]

/**
 * A class that derives from phone representing fixed phones
 */
public class FixedPhone extends Phone
{
private String category;
private boolean faxEnabled;

public FixedPhone()
{
super();
category = "not specified";
faxEnabled = false;
}

public FixedPhone(String telNo, int areaCode, String category, boolean faxEnabled)
{
this.telNo = telNo;
this.areaCode = areaCode;
this.category = category;
this.faxEnabled = faxEnabled;
}

public void setCategory(String newCategory)
{
category = newCategory;
}

public String getCategory()
{
return category;
}

public void faxEnabled(boolean newfaxEnabled)
{
faxEnabled = newfaxEnabled;
}

public boolean getFaxEnabled()
{
return faxEnabled;
}

public void info()
{
System.out.println("Tel No    : " + telNo);
System.out.println("Area Code : " + areaCode);
System.out.println("Category  : " + category);
System.out.println("Fax ?     : " + faxEnabled);
System.out.println();
}

public void startCall()
{
System.out.println("A landline call has started.");
System.out.println();
}
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
test program for the phone classes
« Reply #3 on: February 06, 2006, 04:47:16 PM »
test program for the phone classes

Code: [Select]

/**
 * A test program for cellphone class
 */
public class TestProgram
{
public static void main(String[] args)
{
CellPhone mycell = new CellPhone();
System.out.println("A newly created cellphone object:");
mycell.info();

mycell.setTelNo("09194335689");
mycell.setAreaCode(33);
mycell.setBrand("Nokia");
mycell.setModel("6600");

System.out.println("Cellphone object with new properties:");
mycell.info();

System.out.println("Using the cellphone:  ");
mycell.sendText();
mycell.startCall();
mycell.endCall();

System.out.println("\n\nUsing other constructor:");
CellPhone otherCell = new CellPhone("09214569807", 33, "Motorola", "C65");
otherCell.info();
otherCell.sendText();
otherCell.startCall();
otherCell.endCall();

System.out.println("\n\nCreating a landline object:");
FixedPhone myFixed = new FixedPhone("320-3031", 33, "Business", true);
myFixed.info();
myFixed.startCall();
myFixed.endCall();
}
}

Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Inheritance
« Reply #4 on: February 07, 2006, 08:58:48 AM »
As a side-note, one should try to avoid printing \n characters in Java, because unlike in C, there is not a conversion between \n and the native text file format.  The best thing to do is to call println multiple times, otherwise you will get inconsistent line endings on platforms other than UNIX, which uses \n line endings natively.
Gillius
Gillius's Programming http://www.gillius.org/