More OOP. Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public.

Slides:



Advertisements
Similar presentations
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Advertisements

INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
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,
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Recitation 11. Applet Applets. An applet is a Java program that is started by a browser (e.g. netscape or internet explorer) when an html file has a.
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Chapter 10 Classes Continued
Threads. Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Abstract classes and Interfaces. Abstract classes.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Using Jeroo To Teach Object-Oriented Concepts By Christian Digout.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Internet Software Development Applets Paul J Krause.
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.
JAPPLET.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
CS305j Introduction to Computing Inheritance and Polymorphism 1 Topic 26 Introduction to Inheritance and Polymorphism "One purpose of CRC cards [a design.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
Programming With Java ICS Chapter 8 Polymorphism.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Object Oriented Programming Lecture 5: Refactoring by Inheritance and Delegation - A simple Design Pattern for animation applets, A Generic Function Plotter.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Creating an Object that can draw itself The paint method can ‘draw’ because it is passed a graphics environment as a parameter. If a class method is passed.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Abstract Classes, Abstract Methods, Override Methods
Modern Programming Tools And Techniques-I
Chapter 7- Inheritance.
Chapter 11: Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Object Oriented Analysis and Design
Inheritance, Polymorphism, and Interfaces
Object-Oriented Programming
Inheritance, Polymorphism, and Interfaces. Oh My
Week 6 Object-Oriented Programming (2): Polymorphism
METHOD OVERRIDING in JAVA
Lecture 18: Polymorphism (Part II)
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

More OOP

Extending other’s classes extend Java platform classes, e.g. class Applet public class MyApplet extends Applet { public void init() { } public void paint(Graphics g) { } overriding the method defininitons

Overriding class Rectangle { private int x,y,width,height; public Rectangle(int width,int height) { ? } public double area() { ? } public void draw(Graphics g) { ? }... } class RedRectangle extends Rectangle { public void draw(Graphics g) { g.setColor(Color.red); g.drawRectangle(getX(),getY(),getWidth(),getHeight()); }

Overriding BounceRectangleAroundTheScreen(Rectangle a); Rectangle b=new RedRectangle(10,10); BounceRectangleAroundTheScreen(b); RedRectangle is a Rectangle (it behaves slightly differently) everytime draw method is called for object of type RedRectangle it is drawn in RED A variable of type reference to Rectangle can point to object of type RedRectangle

Overriding class Rectangle { private int x,y,width,height; public Rectangle(int width,int height) { ? } public double area() { ? } public void draw(Graphics g) { ? }... } class RedRectangle extends Rectangle { public void draw(Graphics g) { g.setColor(Color.red); g.drawRectangle(x,y,w,h); } protected

Overriding - rules methods declared with final cannot be overriden overriden method has to have the same return type which method will be used depends only on the object’s type (casting does not change the object’s type, just the way we look at it) (non-private instance methods) you cannot override private methods – you do not inherit them

Extending classes – constructors since RedRectangle is Rectangle the constructor for Rectangle should be called (something important might be initialized in the constructor) class RedRectangle { public void draw(Graphics g) { g.setColor(Color.red); g.drawRectangle(getX(),getY(),getWidth(),getHeight()); } public RedRectangle(int width,int height) { super(width,height); ? }

Constructors - rules you should call super in the first line of a constructor if you don’t then if parent class has constructor with no parameters => it is called (before the body of the constructor) if parent class has no constructor with no parameters => ERROR remember – default constructor has no parameters, but if it is defined only if there is no other constructor.

Exercise class Number { protected int x; public Number(int y) { x=y; } public void increment() { x++; } public void increment2() { increment(); increment(); } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { super(y); } public void increment() { x--; } } Number a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment(); System.out.println(“”+c.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); What is the output?

Exercise class Number { protected int x; public Number(int y) { x=y; increment(); } public void increment() { x++; } public void increment2() { increment(); increment(); } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { super(y); increment(); } public void increment() { x--; } } Number a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment(); System.out.println(“”+c.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); What is the output?

Exercise class Number { protected int x; public Number(int y) { x=y; increment(); } private void increment() { x++; } public void increment2() { increment(); increment(); } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { super(y); increment(); } private void increment() { x--; } } Number a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment2(); System.out.println(“”+a.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); c.increment2(); System.out.println(“”+c.getNumber()); What is the output?

Exercise class Number { protected int x; public Number(int y) { x=y; } public Number() { x=13; } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { System.out.println(“Yupeee!”); } } Number a=new Number(10); BadNumber b=new BadNumber(10); System.out.println(“”+a.getNumber()); System.out.println(“”+b.getNumber()); What is the output?

Exercise class Number { private int x; public Number(int y) { x=y; } public Number() { x=13; } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { System.out.println(“Yupeee!”); x=y; } } Number a=new Number(10); BadNumber b=new BadNumber(10); System.out.println(“”+a.getNumber()); System.out.println(“”+b.getNumber()); What is the output?

Invoking an overriden method class Rectangle { private int x,y,width,height; public Rectangle(int width,int height) { ? } public double area() { ? } public void draw(Graphics g) { ? }... } class RectangleWithPrintedArea { public void draw(Graphics g) { super.draw(g); g.drawString(“”+area(),get(X),get(Y)+20); }

Exercise class Number { protected int x; public Number(int y) { x=y; } public void increment() { x++; } public void increment2() { increment(); increment(); } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { super(y); } public void increment() { super.increment(); x--; } } Number a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment(); System.out.println(“”+a.getNumber()); a.increment2(); System.out.println(“”+a.getNumber()); b.increment(); System.out.println(“”+b.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); What is the output?

Exercise class Number { protected int x; public Number(int y) { x=y; } private void increment() { x++; } public void increment2() { increment(); increment(); } public int getNumber() { return x; } } class BadNumber extends Number { public BadNumber(int y) { super(y); } private void increment() { super.increment(); x--; } } Number a=new Number(10); BadNumber b=new BadNumber(10); Number c=new BadNumber(10); a.increment2(); System.out.println(“”+a.getNumber()); b.increment2(); System.out.println(“”+b.getNumber()); What is the output?

Inheritance – using others code class Rectangle { protected int width,height; public Rectangle(int width,int height) { ? } public double diameterOfCircumCircle() { ? } } class RectangleOnScreen extends Rectangle { int x,y; public RectangleOnScreen(int x,int y,int w,int h) { super(w,h); this.x=x; this.y=y; } public drawCircumCircle(Graphics g) { ? } }

Inheritance – using others code public drawCircumCircle(Graphics g) { g.drawCircle(x+width/2,y+height/2, diameterOfCircumCircle()); } We can easily change/use other’s code! Private variables cannot be used in the derived class!

Polymorphism Shape Circle Polygon Square We want to have list of various shapes which are placed on the screen. We want to move them, draw them, compute area.

abstract class Shape { private int x,y; // coordinates Shape(int initialX,int initialY) { x=initialX; y=initialY; } public void moveUp(int pixels) { y-=pixels; }.... abstract double area(); abstract void draw(Graphics g); } Shape is “too abstract”, we cannot compute area or draw

Shape Circle class Circle extends Shape { public static final double PI=3.1415; private int diameter; Circle(int x,int y,int diam) { super(x,y); diameter=diam; } public double area() { return PI*diameter*diameter/4.0; } public void draw(Graphics g) {... } }

Shape Square class Square extends Shape { public static final double PI=3.1415; private int side; Square(int x,int y,int side) { super(x,y); this.side=side; } public double area() { return side*side; } public void draw(Graphics g) {... } } calling constructor of the parent class

Circle is a Shape! Shape a=new Circle(10,10,5); a.moveUp(5); System.out.println(“The area is ”+a.area()); Shape[] a=new Shape[2]; a[0]=new Circle(10,10,5); a[1]=new Square(20,20,10); if (a[1].area()>a[0].area()) a[1].draw(); else a[0].draw();

Shape is not a Circle Circle a=new Shape(); Circle b=new Rectangle(); Shape c=new Shape();

EXERCISE #12: class Animal { void wish() { System.out.println(“I don’t know!”); } } class Rabbit extends Animal { void wish() { System.out.println(“I want carrot!”); } } class Turtle extends Animal { void wish() { System.out.println(“I want shrimp!”); } } Animal a=new Animal(); a.wish(); Rabbit b=new Rabbit(); b.wish(); Animal c=new Rabbit(); c.wish(); Animal d=new Turtle(); ((Turtle) d).wish();

Applets

public class MyApplet extends Applet { public void init() { } public void paint(Graphics g) { } Simple applets executed at the beginning executed when the applet is redrawn

public class MyApplet extends Applet { int x=0; public void init() { setBackground(Color.black); } public void paint(Graphics g) { x++; g.setColor(Color.white); g.drawString(“”+x,20,20); } Simple applets Import? java.applet.Applet java.awt.Graphics java.awt.Color MyApplet

Applets – start(), stop(), destroy()

public class MyApplet extends Applet implements Runnable { public void init() { } public void paint(Graphics g) { } public void run() { } Applets that run executed at the beginning executed when the applet is redrawn we will create a new Thread which will execute this method

Interfaces we will not go into details public class MyApplet extends Applet implements Runnable { public void init() { } public void paint(Graphics g) { } public void run() { } says which methods must be implemented in MyApplet (new Thread(this)).start(); expects parameter of type Runable

public class MyApplet extends Applet implements Runnable { public void init() { (new Thread(this)).start(); } public void paint(Graphics g) { } public void run() { } Applets that run we will create a new Thread out of this Object (note that this Object is Runnable) its run method will be this. We will start it!

public class MyApplet extends Applet implements Runnable { int x=0; public void init() { setBackground(Color.black); (new Thread(this)).start(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“”+x,20,20); } public void run() { while (true) { x++; repaint(); } Applets that run request for repainting (If there are enough resources – it is repainted) MyApplet different Thread

public class MyApplet extends Applet implements Runnable { int repaints=0,requests=0; public void init() { setBackground(Color.black); (new Thread(this)).start(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } public void run() { while (true) { requests++; repaint(); } Applets that run – wasting resources request for repainting (If there are enough resources – it is repainted) MyApplet

public class MyApplet extends Applet implements Runnable { int repaints=0,requests=0; public void init() { setBackground(Color.black); (new Thread(this)).start(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } public void run() { while (true) { requests++; repaint(); } Applets that run MyApplet try { Thread.currentThread().sleep(40); } catch (InterruptedException e) {};

Why not run in init? public class MyApplet extends Applet { int repaints=0,requests=0; public void init() { setBackground(Color.black); while (true) { requests++; repaint(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } MyApplet

Why not run in init? public class MyApplet extends Applet { int repaints=0,requests=0; public void init() { setBackground(Color.black); while (true) { requests++; repaint(); } public void paint(Graphics g) { g.setColor(Color.white); g.drawString(“Repaints: ”+(++repaints),20,20); g.drawString(“Requests: “+requests,20,40); } MyApplet the thread which runs init needs to do some things – e.g. start the paint thread...