Professional Documents
Culture Documents
Confidential
Intro. to OOP
Inheritance
Properties of a data type can be passed down to a sub-type we can build
new types from old ones We can build class hierarchies with many levels of inheritance
Polymorphism
Operations used with a variable are based on the class of the object being accessed, not the class of the variable Parent type and sub-type objects can be accessed in a consistent way
Confidential
+
Confidential
In many applications, data is more complicated than just a simple value Ex: A Polygon a sequence of connected points
The data here are actually:
int [] xpoints an array of x-coordinates int [] ypoints an array of y-coordinates int npoints the number of points actually in the Polygon
Confidential
Confidential
Confidential
P2
We do not need to know the implementation details of a data type in order to use it
This includes the methods AND the actual data representation of the object
Confidential
Much of the functionality we need is already there, but some things need to be added or changed
We can achieve this in object-oriented languages using one of two ways
Composition Inheritance
Confidential
Composition
We include an object of the class whose functionality we need as a private instance variable in our own class
public class NewClass { private OldClass oldVar;
We then write methods that wrap around similar methods of included class
public int foo(int arg) { return oldVar.foo(arg); }
The new class then seems to provide the same functionality as the included class In addition to any new functionality
Confidential
Composition
This is ideal when an object of NewClass isnt logically an object of OldClass
Fails the is a test
This is scheme is used by the input and output stream classes of the java.io package
Ex:
DataOutputStream out = new DataOutputStream(new FileOutputStream(file.out));
out stores the passed in FileOutputStream as an instance variable A FileOutputStream isnt necessarily a DataOutputStream, nor is a DataOutputStream necessarily a FileOutputStream Both are still output stream objects, though
Confidential
Inheritance
Alternatively, our NewClass can directly inherit the properties of OldClass
Just then need to add the new properties
Can still augment the inherited interface Semantically defines a logical relationship between the two objects
Confidential
Inheritance and is a
We can understand this better by considering the is a idea
A subclass object is a superclass object However, some extra instance variables and methods may have been added and some other methods may have been changed
Confidential
Inheritance and is a
Animal
is a is a Fish
is a
Bird
Human
Bird, Human and Fish are all Animals However, an Animal is not necessarily a Bird, Human or Fish
Confidential
Extending Classes
We then continue the definition of NewClass as normal However, implicit in NewClass are all data and operations associated with OldClass
Even though we dont see them in the definition
Confidential
Inheritance Example
As another example
Compare MixedNumber class and MixedNumber2 class Both utilize the authors' RationalNumber class to do most of the "work" Both also have the same functionality, but MixedNumber uses composition and MixedNumber2 uses inheritance
Note simplicity of MixedNumber2 methods Read over the comments carefully! See RationalNumber.java, MixedNumber.java and MixedNumber2.java
Confidential
Polymorphism
Idea of polymorphism
http://www.wordiq.com/definition/Polymorphism_%28computer_science%29
Confidential
Method Overloading
This is called ad hoc polymorphism, or method
overloading
In this case different methods within the same class or in a common hierarchy share the same name but have different method signatures (name + parameters)
Confidential
Method Overloading
If an exact signature match is not possible, the one that is closest via widening of the values is used
Widening means that values of smaller types are cast into values of larger types Ex: int to long int to float float to double Fewer widenings provides a "closer" match
If two or more versions of the method are possible with the same amount of widening, the call is ambiguous, and a compilation error will result
See ex20.java Note: This type of polymorphism is not necessarily object-oriented can be done in non-object-oriented languages
Confidential
Polymorphism
Subclassing Polymorphism
1) Method overriding
A method defined in a superclass is redefined in a subclass with an identical method signature Since the signatures are identical, rather than overloading the method, it is instead overriding the method
For subclass objects, the definition in the subclass replaces the version in the superclass
Confidential
Polymorphism
2) Dynamic (or late) binding
data types (ex: draw different graphical objects using the Confidential
Polymorphism
Ex. Each subclass overrides the move() method in its own way Animal [] A = new Animal[3]; A[0] = new Bird(); A[1] = new Person(); A[2] = new Fish(); for (int i = 0; i < A.length; i++) A[i].move();
move()
move()
Composition or Inheritance
Caveats like those we just discussed arise often with Inheritance Also, In heritance permanently associates a superclass with our class at compile time
We can only inherit from a single class Composition allows our class the flexibility to wrap around different superclasses at run-time
Composition is generally preferred over Inheritance We loose polymorphism and dynamic binding with Composition though
In many cases, we need those capabilities We use abstract classes and Interfaces to help solve these problems
Confidential
Features of Java
Java is simple Java is object-oriented
Java is distributed
Java is interpreted Java is robust Java is secure Java is architecture-neutral Java is portable Javas performance Java is multithreaded Java is dynamic
Confidential