The Classes, Before an Interface Is Implemented


Our example will use four classes: Person , Student , Instructor , and Course . The class Person will provide the first and last name attributes (data members ) and will serve as the base class for Student and Instructor . The Course class will not be derived from anything, and for the sake of simplicity will contain nothing but the course name.

The Java classes are defined as follows :

 /////// Person.java 
package Registration;
public class Person {
protected String FirstName, LastName;
public void Modify( String First, String Last )
{
FirstName = First;
LastName = Last;
}
public void Display()
{
System.out.print( FirstName + " " + LastName);
}
}
/////// Student.java
package Registration;
public class Student extends Person {
protected int GraduationYear;
public Student() {
GraduationYear = 0;
}
public void Modify( String First, String Last, int Graduation ) {
super.Modify( First, Last );
GraduationYear = Graduation;
}
public void Display() {
super.Display();
System.out.print( " " + GraduationYear );
}
} /////// Instructor.java
package Registration;
public class Instructor extends Person {
protected boolean IsTenured;
public Instructor() {
IsTenured = false;
}
public void Modify( String First, String Last, boolean Tenured ) {
super.Modify( First, Last );
IsTenured = Tenured;
}
public void Display() {
super.Display();
if( IsTenured )
System.out.print( " (Tenured)" );
else
System.out.print( " (Not tenured)" );
}
} /////// Course.java
package Registration;
public class Course {
protected String Name;
public Course() {
}
public void Modify( String CourseName ) {
Name = CourseName;
}
public void Display() {
System.out.print( Name );
}
}

The matching classes in C# would be declared as follows:

 /////// Person.cs 
using System;
namespace Registration
{
public class Person {
protected string FirstStr, LastStr;
public void Modify( String First, String Last ) {
FirstStr = First;
LastStr = Last;
}
public void Display() {
System.Console.Out.Write( FirstStr + " " + LastStr );
}
}
}

/////// Student.cs
using System;
namespace Registration
{
public class Student : Person
{
protected int GraduationYear;
public Student() {
GraduationYear = 0;
}
public void Modify( String First, String Last, int Graduation )
{
base.Modify( First, Last );
GraduationYear = Graduation;
}
public void Display() {
base.Display();
System.Console.Out.Write( " " + GraduationYear );
}
}
}

/////// Instructor.cs
using System;
namespace Registration
{
public class Instructor: Person
{
protected bool TenuredBool;
public Instructor() {
TenuredBool = false;
}
public void Modify( String First, String Last, bool Tenured )
{
base.Modify( First, Last );
TenuredBool = Tenured;
}
public void Display() {
base.Display();
if( TenuredBool )
System.Console.Out.Write( " (Tenured)" );
else
System.Console.Out.Write( " (Not tenured)" );
}
}
} /////// Course.cs
using System;
namespace Registration
{
public class Course
{
protected string NameStr;
public void Modify( String Name ) {
NameStr = Name;
}
public void Display() {
System.Console.Out.Write( NameStr );
}
}
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net