Inherited and Sub Classes
Up until now you have had one class per file and each class named after the file, all of these classes have been completly seperate from other classes, but their are times when a Class may not deserve its own file it might be really small and only be used from one other class for instance (sub classes), there may also be times when you want loads of classes which are simular to each other (inherited classes).
Sub classes
Code:
class PARENT{
CHILD child = new CHILD();
class CHILD{
String property1;
String property2;
}
}
PARENT parent = new PARENT();
parent.child.property1="sub class!";
Sub classes are placed within larger classes and can be used to hide code or more advanced options.
Inherited classes
There are two types of inheritances: extends and implements
Extends, extends your new class by copying all you variables and functions over from the old class
Implements, forces your class to require the variables and functions from the old class (list of rules)
We will be creating classes using "extents", the main time you will use implements is to implement listners when doing Java windows interface!
Task
Ok to demonstrate both extends and sub classes
We are going to be a shop, we are going to have both customers and employees
customers and employees will be both be people and will have charectoristics of people, name, address etc...
Our customers will have their own ID, employees will have their job roles, hours etc...
Person: will be the parent class holding the persons name and address
Address: has multiple properties, postcode / house number / state country, and will be a subclass of person to hide these properties
Employee: will extend person he/she has a name and address but we also want to know their NI number and jobroles
Customer: will extend person he/she has a name and address but we also want to know their customer ID and last shpped
We will use our main class just to show off the properties made avaliable to the above classes
Create a new project named: AdvancedClasses
Create a new class Main with the main function tick
Create a new class Person
Create a new class Customer
Create a new class Employee
Notice:
Address does not need its own class, it will be part of Person
Person
Code:
public class Person {
String title;
String forename;
String surname;
Address address;
class Address{
String postcode;
String flat;
String city;
String state;
String country;
}
}
Customer
Code:
public class Customer extends Person{
long customerID=0;
String joined;
String lastShopped;
}
Employee
Code:
public class Employee extends Person {
String NI;
String department;
String jobrole;
int hours;
}
Main
Code:
public class Main {
public static void main(String[] args) {
Person person1 = new Person();
person1.title = "mr";
person1.forename = "martin";
person1.surname = "sykes";
person1.address.flat = "hidden";
person1.address.postcode = "hidden";
person1.address.city = "Rotherham";
person1.address.state = "South Yorkshire";
person1.address.country = "England";
Employee person2 = new Employee();
person2.title = "mr";
person2.forename = "martin";
person2.surname = "sykes";
person2.address.flat = "hidden";
person2.address.postcode = "hidden";
person2.address.city = "Rotherham";
person2.address.state = "South Yorkshire";
person2.address.country = "England";
person2.department = "software";
person2.jobrole = "programmer";
person2.hours = 39;
person2.NI = "some NI number";
Customer person3 = new Customer();
person3.title = "mr";
person3.forename = "martin";
person3.surname = "sykes";
person3.address.flat = "hidden";
person3.address.postcode = "hidden";
person3.address.city = "Rotherham";
person3.address.state = "South Yorkshire";
person3.address.country = "England";
person3.customerID = 1;
person3.joined="12/9/08";
person3.lastShopped="12/9/08";
}
}
Note, how many properties are exposed by so little code, this is re-using code at its best the Basic language can only look on in envy of this.
Notice:
Inheritance and sub-classes will only become more and more valuable as you move on to using GUI based Java interfaces.
Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Add Comment
Comments