Presentation is loading. Please wait.

Presentation is loading. Please wait.

3. Java Inheritance …and Slick2d intro.

Similar presentations


Presentation on theme: "3. Java Inheritance …and Slick2d intro."— Presentation transcript:

1 3. Java Inheritance …and Slick2d intro

2 inheritance Main idea: Advantages: Used extensively in Java
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

3 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; }

4 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 }

5 Polymorphism The ability to assign derived-class instances to base-class 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()); // ERROR! //System.out.println(t.getWidth()); // 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

6 Java Limitations Can only extend from one base class. Enter interfaces
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

7 Misc @Override Object class Generics 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>

8 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.

9 implements, cont. The polymorphism rules still apply.
If you choose to implement multiple interfaces, use: MyClass implements ifaceA, ifaceB, ifaceC { }

10 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

11 Access Modifier example again
// TheClass.java package accessexample; public class TheClass { } // OtherClass.java package accessexample; public class OtherClass { } import accessexample.*; public class TestApp { public static void main(String[] args) } World In-class In-package // OtherClass.java import accessexample.TheClass; public class OtherClass extends TheClass { } In-subclass This is the class of interest

12 Access Rules Can access? Modifier In-class In-package In-Subclass
World public Y protected N no modifier (package-private) private

13 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

14 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.


Download ppt "3. Java Inheritance …and Slick2d intro."

Similar presentations


Ads by Google