Re-using existing software
Problem description:
we need to
write
a program (software)
to solve a
new problem:
- Write
the NewClass class
to solve a
new problem:
|
How can we
do this
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 !!!)
|
Hard to maintain programs:
when we update the
original software
(from which we made the new software),
we may need to
update our
programs that are
based on the
existing software
Re-using existing software
How to
re-use
existing sofware to
build
more complex software
nowadays
- 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
nowadays
- We
define (no copying !) the
new
class (= software) to
inherit from the
original class:
|
The new
class (= software) will
inherit (= "receive")
all the
variables and
methods from
class X
Experiment
Trace the execution of b.method1()
and b.method2()
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 class myProg
{
public static void main(String[] args)
{
SomeClass a = new SomeClass();
a.method1();
a.method2();
NewClass b = new NewClass();
b.method1(); // Trace this
b.method2(); // Trace this
}
}
|
Re-using existing software
How to
re-use
existing sofware to
build
more complex software
nowadays
- If an inherited method is
not appropriate
(= does not do what we want), we
replace (= override) that
method
with
a new method with
the same signature:
|
Methods defined
inside the NewClass will
take priority over
an inherited method
with the same method signature
(this mechanism is
called
overriding)
Experiment
Trace the execution of b.method1()
and b.method2()
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)
{
SomeClass a = new SomeClass();
a.method1();
a.method2();
NewClass b = new NewClass();
b.method1(); // Trace this
b.method2(); // Trace this
}
}
|
Re-using existing software
How to
re-use
existing sofware to
build
more complex software
nowadays
- 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
defined in the
NewClass
(and will not be defined (inherit)
in the orignal class (SomeClass))
Experiment
Trace the execution of b.method3()
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)
{
SomeClass a = new SomeClass();
a.method1();
a.method2();
NewClass b = new NewClass();
b.method1();
b.method2();
b.method3(); // Trace this
}
}
|
Important note:
Accessibility modifiers
are enforced on
inherited members !
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
common properties/actions:
- The
Object Oriented Design
methodology
can
miximize the
re-use
of variables and
methods !!!
(Study next !)
|
❮
❯