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 with
OOP
- 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
- 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
(normal) methods from
an existing class
Experiment
Trace the execution of b.method1()
and b.method2()
DEMO:
demo/04-inheritance/01-extends
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(); // Invokes SomeClass.method1()
b.method2(); // Invokes SomeClass.method2()
}
}
|
Re-using existing software
How to
re-use
existing sofware to
build
more complex software with
OOP
- 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()
DEMO:
demo/04-inheritance/02-override
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
How to
re-use
existing sofware to
build
more complex software with
OOP
- 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()
DEMO:
demo/04-inheritance/03-add
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
Notice
there are
two (different)
methods
named
method1(...)
with the same signature:
- The original
method1(...) in
SomeClass
(the overridden method)
The new
method1(...) in
NewClass
(the overriding method)
|
When
writing
methods in
NewClass,
both
methods are
available for
use (= accessible) !
Accessing an
overridden method
and an
overriding method
Notice
there are
two (different)
methods
named
method1(...)
with the same signature:
-
method1(...)
will
invoke
the
overriding
method (i.e.: the default case)
super.method1(...)
will
invoke
the
overridden
method
|
The
super keyword
always
refers to
members in the
super class
DEMO:
demo/04-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:
-
Find a
class X (= software)
that can be used as
the starting point to
build the
new (more complex)
class (= software)
- Define the
new
class Y as
a
subclass of
X:
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 !
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 !)
|
❮
❯