Re-using existing software

$64,000 question:   what is the easiest way to write programs to solve a problem:

  •  If  you are a new programmer and do not have access to any programs, you probably need to write the whole program from scratch:

But things will change when you have programmed for many years and have written many programs

Re-using existing software

Problem description:   we need to write a program (software) to solve a new problem:

  • We need to write the NewClass class to solve a new problem:
    We have written many programs before....

How can we write the NewClass class efficiently ? (i.e.: use the least effort as possible)

Re-using existing software

How to re-use existing sofware to build more complex software prior to ~1995

  • We find a program (class) that can be used as the starting point to build our more complex software (= programs)

Before the invention of Object Oriented Programming (OOP)

Re-using existing software

How to re-use existing sofware to build more complex software prior to ~1995

  • We make a copy of the program (class) (and must rename the class otherwise we have 2 classes with the same name !):

Before the invention of Object Oriented Programming (OOP)

Re-using existing software

How to re-use existing sofware to build more complex software prior to ~1995

  • Then make changes to the copy of software so the new software (= program) does what we want

Before the invention of Object Oriented Programming (OOP)

Problems with this technique for re-using existing software

Redundancy:

  • Redundancy: we can have multiple copies of the same method (because we made a copy)
     

Problem:   redundancy make maintenance very difficult (think: many copies, making a change is difficult)

Re-using existing software

How to re-use existing sofware to build more complex software with OOP

  • Just like before:   we find a program (class) that can be used as the starting point to build our more complex software (= programs)

After the invention of Object Oriented Programming (OOP)

Re-using existing software

How to re-use existing sofware to build more complex software with OOP

  • New: we extend ( no copying) the new class (= software) using an existing class:
    The original class is called the super class and the new class is called the subclass

The new class (= software) will inherit (= "receive") all the variables and (normal) methods from an existing class

Experiment    Trace the execution of b.method1() and b.method2()

DEMO: demo/13-inheritance/01-extends/Demo.java + SomeClass.java + NewClass.java

public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }

    public void method1( )
    {
        System.out.println("I am SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("I am SomeClass.method2(). x = " + x);
    }
}

public class NewClass extends SomeClass { NewClass() { } // No other methods defined ! }
public class myProg { public static void main(String[] args) { NewClass b = new NewClass(); b.method1(); // NewClass contains a method1() - It's SomeClass.method1() b.method2(); // NewClass contains a method2() - It's SomeClass.method2() } }

What happens when we modify the existing software after inheritance

Advantage of re-using existing sofware with inheritance:

  • There is only one copy of the code (it's in the original class)
    When we modify the existing software, the inherited code are also changed:

DEMO: 13-inheritance/01-extends/Demo.java + SomeClass.java + NewClass.java   --   edit SomeClass.java

Re-using existing software

What if some method that was inherited does not work correctly for a NewClass object ?

  • If an inherited method is not appropriate (= does not do what we want), we override (= replace) that method with a new method with the same signature:

Rule:   methods defined inside NewClass will supersede an inherited method with the same method signature
(This mechanism is called overriding)

Experiment    Trace the execution of b.method1() and b.method2()

DEMO: demo/13-inheritance/02-override/Demo.java + SomeClass.java + NewClass.java

public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }

    public void method1( )
    {
        System.out.println("I am SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("I am SomeClass.method2(). x = " + x);
    }
}

public class NewClass extends SomeClass { NewClass() { } public void method1( ) // Overrides the inherited method1 { System.out.println("I am **NewClass**.method1(). x = " + x); } }
public class myProg { public static void main(String[] args) { NewClass b = new NewClass(); b.method1(); // Invokes NewClass.method1() b.method2(); // Invokes SomeClass.method2() } }

Re-using existing software

What if the original class does not have a method to perform some action in the NewClass object ?

  • If original class does not have a suitable method for some task in the new class, we can add new method(s) to our new class to perform that task:

These new methods will only be available in a NewClass object (and will not be available in a SomeClass object)

Experiment    Trace the execution of b.method3()

DEMO: demo/13-inheritance/03-add/Demo.java + SomeClass.java + NewClass.java

public class SomeClass
{
    public int x;

    public SomeClass()
    {
        x = 99;
    }

    public void method1( )
    {
        System.out.println("I am SomeClass.method1(). x = " + x);
    }
    
    public void method2( )
    {
        System.out.println("I am SomeClass.method2(). x = " + x);
    }
}

public class NewClass extends SomeClass { NewClass() { } public void method1( ) // Overrides the inherited method1 { System.out.println("I am **NewClass**.method1(). x = " + x); } public void method3( ) // Defines a new method { System.out.println("I am **NewClass**.**method3**(). x = " + x); } }
public class myProg { public static void main(String[] args) { NewClass b = new NewClass(); b.method1(); // Invokes NewClass.method1() b.method2(); // Invokes SomeClass.method2() b.method3(); // Invokes NewClass.method3() // SomeClass a; // a.method3(); // is ILLEGAL --> SomeClass does not have method3() ! } }

Accessing an overridden method and an overriding method

Note:   a NewClass object has two (different) methods named method1(...) with the same signature:

  • (1) The original method1(...) in SomeClass (the overridden method is still there !)
    (2) The new method1(...) in NewClass (the overriding method)

When writing code in NewClass, both methods are available for use (= accessible) !

Accessing an overridden method and an overriding method

How to invoke the two (different) methods named method1(...) inside NewClass:

  • (1) method1(...)            will invoke the overriding method (i.e.: the default case)
    (2) super.method1(...) will invoke the overridden method

The super keyword always refers to members in the super class
DEMO: demo/13-inheritance/03a-super/Demo.java + NewClass.java

Summary: re-using existing software

How to re-use existing sofware to build more complex software using OOP

  • Inherit from a suitable parent class X:

      1. Find a class X (= software) that can be used as the starting point to build the new (more complex) class (= software)

      2. Define the new class Y as a subclass of X using the extends keyword:

           public class Y extends X   ...

        Class Y will inherit (= receive) all the (1) variables and (2) (non-constructor) methods in class X

  • Add/modify:

      • Add additional properties (= member variables)

      • Override (= replace) any inappropriate inherited method

      • Add any required methods not available in the superclass X

Important note:    Accessibility modifiers are enforced on inherited members !

  • Note:   the subclass and its superclass are separate classes

  • Therefore:   methods defined (= written) inside a subclass can not access private members in the super class:

    (Only the unmodified inherited methods can access the inherited private members)

DEMO: demo/13-inheritance/03a-super/Demo.java + NewClass.java    --    change x from public to private

Object Oriented Thinking

  • In order to maximize the inheritance mechanism to re-use existing software, you need to adopt the Object Oriented Design methodology when you develop (= write) your classes

  • The Object Oriented Design methodology organizes object class in a hierarchy according to their shared properties and actions:

  • The Object Oriented Design methodology can miximize the re-use of variables and methods !!! (Study next !)