Headline

Class inheritance in Java

Characteristics

Basic style of OO programming is applied. A simple object model for companies is provided with methods to implement a query for totaling salaries and a transformation for cutting salaries. In fact, class inheritance is leveraged. Because of the use of class inheritance, the aforementioned methods are actually virtual methods. Closed serialization in the sense of Technology:Object Streams is enabled by means of the marker interface Serializable, which is applied to the classes of the object model for companies.

Illustration

The data model is implemented using class inheritance. A subunit of Feature:Company is defined like this:

/**
 * A subunit is either an employee unit or a department unit
 * 
 */
public abstract class Subunit {
	public abstract double total();	
	public abstract void cut();
}

Concrete subunits (Departments and Employees) extend this class. E.g. Department:

/**
 * A department has a name, a manager and a list of subunits
 * 
 */
public class Department extends Subunit implements Serializable {

	private static final long serialVersionUID = -2008895922177165250L;

	private String name;
	private Employee manager;
	private List<Subunit> subunits = new LinkedList<Subunit>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Employee getManager() {
		return manager;
	}

	public void setManager(Employee manager) {
		this.manager = manager;
	}

	public List<Subunit> getSubunits() {
		return subunits;
	}

	public double total() {
		double total = 0;
		total += getManager().total();
		for (Subunit s : getSubunits())
			total += s.total();
		return total;		
	}	
	
	public void cut() {
		getManager().cut();
		for (Subunit s : getSubunits())
			s.cut();
	}	
}

Feature:Closed serialization is implemented using Language:Java Technology:Object Streams:

    /*
     * Read (say, deserialize) a company.
     */
    public static Company deserializeCompany(String filename) {

        Object o = null;

        try {
            FileInputStream fis = new FileInputStream(filename);
            ObjectInputStream in = new ObjectInputStream(fis);
            o = in.readObject();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return (Company) o;
    }
    /*
     * Write (say, serialize) a company.
     */
    public static boolean serializeCompany(Company c, String filename) {

        FileOutputStream fos = null;
        ObjectOutputStream out = null;

        try {
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(c);
            out.close();
            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
    }

Feature:Total and Feature:Cut are implemented as calls of POJO methods:

    public static double total(Company c) {
        // Total is implemented in the Company class
        return c.total();
    }

    public static void cut(Company c) {
        // Cut is implemented in the Company class
        c.cut();
    }

Test cases are implemented for all Namespace:Features.

Relationships

For basic OO without inheritance see Contribution:javaComposition.

For basic OO with inheritance see Contribution:javaInheritance.

For modular OO programming with static methods see Contribution:javaStatic.

For use of Java reflection see Contribution:javaReflection (data processing) and Contribution:javaSyb (SYB-style generic programming).

For design patterns see Contribution:javaTemplate (template design pattern), Contribution:javaVisitor (visitor design pattern) and Contribution:javaExorcism (excessive illustration of design patterns).

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.