More oop in java

Download Report

Transcript More oop in java

3. JAVA INHERITANCE
…AND SLICK2D INTRO
INHERITANCE
• Main idea:
• Create a new class (Child) from an existing
one (Parent)
• Add new methods to the Child class
[optional]
• Re-implement methods from the Parent
class [optional]
• Advantages:
• Don't have to re-type old code
• And changes to Parent automatically
propogate
• Natural type-description
• If "Apple" is a type of "Fruit", use inheritance.
• Polymorphism
• Used extensively in Java
BASIC EXAMPLE
public class Shape
{
protected Color mColor;
public Shape(Color c) { mColor = c; }
public Color getColor () {return mColor; }
}
public class Circle extends Shape
{
protected float mRad;
public Circle(Color c, floar r) { super(c); mRad = r; }
public float getRadius() { return mRad; }
}
public class Box extends Shape
{
protected float mWidth, mHeight;
public Box(Color c, floar w, float h) { super(c);
mWidth = w; mHeight = h; }
public float getWidth() { return mWidth; }
}
BASIC EXAMPLE, CONT.
public static void main(String[] args)
{
Shape s = new Shape(Color.RED);
Circle c = new Circle(new Color(255,255,0), 5.0f);
Box b = new Box(Color.BLUE, 20.0f, 13.0f);
System.out.println(s.getColor()); // Java AWT Color(r=255,g=0,b=0)
System.out.println(c.getColor()); // Java AWT Color(r=255,g=255,b=0)
System.out.println(c.getRadius()); // 5.0
//System.out.println(b.getRadius());
// ERROR!
//System.out.println(s.getRadius());
// ERROR!
System.out.println(b.getWidth()); // 20.0
}
POLYMORPHISM
• The ability to assign derived-class instances to baseclass variables.
public static void main(String[] args)
{
Shape s = new Circle(new Color(255,255,0), 5.0f);
Shape t = new Box(Color.BLUE, 20.0f, 13.0f);
System.out.println(s.getColor());
System.out.println(c.getColor());
//System.out.println(s.getRadius());
//System.out.println(t.getWidth());
// ERROR!
// ERROR!
• You can, however cast a reference
Circle c = (Circle)s;
System.out.println(c.getRadius());
// 5.0
• But be careful…
Circle d = (Circle)t;
System.out.println(d.getRadius());
// Compiles…but a run-time error
JAVA LIMITATIONS
• Can only extend from one base class.
• No multiple inheritance
• Arguments for this:
• Avoids the “deadly diamond” [explain]
• Each object should only be “made from” another object
• Arguments against this:
• Sometimes makes super-deep inheritance trees
• Locks you into a rigid inheritance structure
• e.g. you have a GroundBasedObject that is the parent of Vehicle
which is the parent of WheeledCar which is the parent of Tank
• You later change your mind that you want your Tank to be a
spaceship. You now have to modify every parent class in the chain
• Enter interfaces
MISC
• @Override
• Remember that?
• [Refresher and connection to extends]
• Object class
• Everything indirectly inherits from this (except primitives)
• Generics
• From lab2: public class Foo<E>
• E only supports those methods that in Object
• If you want to require only certain types of generics, use:
public class Foo<E extends MinBase>
IMPLEMENTS
• Java classes can implement any number of
interface classes.
• An interface class looks like this:
// No method bodies allowed AND no attributes.
public interface Talker
{
public void talk();
}
• If you choose to implement this you do:
public class Dog implements Talker
{
public void talk()
{
System.out.println("Woof!");
}
}
• An interface is a contract – the implementing class
must define all methods from the interface.
IMPLEMENTS, CONT.
• The polymorphism rules still apply.
• If you choose to implement multiple interfaces, use:
MyClass implements ifaceA, ifaceB, ifaceC
{
}
ANOTHER ALTERNATIVE
• Inheritance is an “is-a” relationship
• Another option: “has-a”
• You create an instance of another class
• And call methods of that internal instance.
• Important Side-discussion: Stacks and Queues
ACCESS MODIFIER EXAMPLE AGAIN
// TheClass.java
package accessexample;
// OtherClass.java
package accessexample;
public class TheClass
{
public class OtherClass
{
In-class
Inpackage
}
import accessexample.*;
public class TestApp
{
public static void main(String[] args)
{
World
}
}
}
// OtherClass.java
import accessexample.TheClass;
public class OtherClass extends TheClass
{
In-subclass
}
This is the class of interest
ACCESS RULES
Can access?
Modifier
In-class
In-package
In-Subclass
World
public
Y
Y
Y
Y
protected
Y
Y
Y
N
no modifier (packageprivate)
Y
Y
N
N
private
Y
N
N
N
INTRO TO SLICK2D
• What is it?
• Cross-platform (Linux, OSX, Win)
• An example of an external dependency
• Also uses native libraries (written in C most likely)
• Windows = .dll
•
•
•
•
OSX = .dylib
Linux = .so
Graphics, Animation, Input
We'll use it for simple visualizations
Good example of inheritance
Has a few (mostly-easy) setup steps that are IDE / platform
specific
INHERITANCE IN SLICK2D
• Main class overview
• You Create a class (Foo) that extends BasicGame
(org.newdawn.slick.BasicGame)
• Override 3 main methods: init, update, and render
• Create (in main) an AppGameContainer (Bob) and a Foo inst
(Hat)
• Attach Hat to Bob (by passing it to Bob's constructor)
• Call Bob's start method – that will call the 3 methods of Hat at
appropriate times.
• Listener design pattern in Slick2D
• [On Board]
• make special note of the use of implements.