3. Java Inheritance …and Slick2d intro.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Written by: Dr. JJ Shepherd
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Advanced Object-Oriented Programming Features
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
CSSE501 Object-Oriented Development
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Inheritance using Java
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
JAVA BASICS Prepared by The Smartpath Information Systems
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Programming in Java CSCI-2220 Object Oriented Programming.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Inheritance and Access Control CS 162 (Summer 2009)
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Introduction to Object-oriented Programming
Inheritance.
Written by: Dr. JJ Shepherd
Modern Programming Tools And Techniques-I
Reference: Object Oriented Design and Programming (Horstmann)
Polymorphism.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Week 4 Object-Oriented Programming (1): Inheritance
An Introduction to Inheritance
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
More About Inheritance & Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
C++ Programming CLASS This pointer Static Class Friend Class
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

3. Java Inheritance …and Slick2d intro

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

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

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

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>

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

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

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.