Top Banner
Question of the Day Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’) Should have equal values on both sides of equals sign Not limited to what is possible in Java
30

Question of the Day Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’) Should have equal values.

Dec 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Question of the Day

Write valid mathematical equation using:2 3 4 5one addition operator (‘+’)one equality operator (‘=’) Should have equal values on both sides of

equals sign Not limited to what is possible in Java

Page 2: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Question of the Day

Write valid mathematical equation using:2 3 4 5 one addition operator (‘+’)one equality operator (‘=’) Should have equal values on both sides of

equals sign Not limited to what is possible in Java

5 + 4 = 32

Page 3: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

LECTURE 11:ABSTRACT CLASSES & INTERFACES

Page 4: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Announcement

Page 5: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Inheritance Issues

Multiple inheritance causes many problems What if there are multiple superclasses

declare field? Which constructor will super() chain to? Since classes extend only 1 class, Java

avoids problem Often mix multiple ideas within class,

however

Page 6: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Inheritance Issues

Multiple inheritance causes many problems What if there are multiple superclasses declare

field? Which constructor will super() chain to? Since classes extend only 1 class, Java avoids

problem Often mix multiple ideas within class,

howeverpublic class Mammal { … }

public class Bird { … }

public class Platypus extends

Page 7: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Inheritance Issues

Multiple inheritance causes many problems What if there are multiple superclasses declare

field? Which constructor will super() chain to? Since classes extend only 1 class, Java avoids

problem Often mix multiple ideas within class,

howeverpublic class Mammal { … }

public class Bird { … }

public class Platypus extends

Page 8: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

abstract

Can declare methods as abstract Methods cannot be defined; promise

functionality Methods can have parameters, return type,

etc. Bodies are illegal; declaration only lists

signaturepublic abstract void foo(int i);public abstract Beer bar(String o);

Page 9: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

abstract class

If we have a class containing abstract methods Class considered abstract no matter how declared

abstract classes similar to regular classes… static & non-static fields allowed in class Class will always extend exactly 1 other class Mix of abstract & normal methods can be included May use default one or may define a constructor Can be extended by other classes

Page 10: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

abstract class

If we have a class containing abstract methods Class considered abstract no matter how

declared abstract classes similar to regular

classes… …but cannot instantiate abstract class

And abstract methods inherited by subclasses

Page 11: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

abstract class

If we have a class containing abstract methods Class considered abstract no matter how

declared abstract classes similar to regular

classes… …but cannot instantiate abstract class

And abstract methods inherited by subclasses But subclass can override method & make it

concrete

Page 12: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Interfaces in Real World

Page 13: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Interfaces

Can only declare important constant fields public static final must be used for

fields Interface declares public abstract

methods Methods callable anywhere by any class But method’s body cannot be defined in

interface

Page 14: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Adding to an Interface

Interfaces have sub-interfaces Sub-interface extends super-interface Super-interface’s methods inherited by sub-

interface Methods from super-super-interface also

inherited Sub-interface can extend multiple

interfaces Methods not defined so does not cause

problems

Page 15: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Declaring Interfaces

public interface Drawable { public void setColor(Color c); public Color getColor(); public void draw(Graphics g);}

public interface TranDraw extends Drawable{ public void setTransparency(int tLevel);}

public interface MoveDraw extends Drawable{ public void setPosition(int x, int y);

}

Page 16: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Interfaces

Classes implement interfaces Implementing classes define interface’s methods

Any number of interfaces may be implemented Multiple inheritance issues ignored -- methods

empty Unrelated to superclass chosen or used by a class

Classes must implement all interface’s methods Includes methods inherited from super-interface

Page 17: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Implementing Interfaces

public class Square implements Drawable {private Color c;private int x, y, side;

public Square(Color col, int len) { c = col; side = len;}

public void setColor(Color col){ c=col; }public Color getColor() { return c; }public void draw(Graphics g) { g.drawRect(x, y, side, side, c);}

}

Page 18: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Implementing Interfaces

public class Oval implements Drawable {private Color c;private int x, y, majRad, minRad;

public Oval(Color co, int maj, int min){ c = co; majRad = maj; minRad = min;}

public void setColor(Color col){ c=col; }public Color getColor() { return c; }public void draw(Graphics g) { g.drawOval(x, y, majRad, minRad, c);}

}

Page 19: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Using Interfaces

Cannot instantiate an interface… Not possible, since only create instances of

class Variables of interface type are perfectly

legal Variable can refer to implementing class

instance Methods must be implemented in actual

class Remember: which method called by

instance type

public void drawRed(Drawable d, Graphics g) {d.setColor(Color.red);d.draw(g);

}

Page 20: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Using Interfaces

Cannot instantiate an interface… Not possible, since only create instances of

class Variables of interface type are perfectly

legal Variable can refer to implementing class

instance Methods must be implemented in actual

class Remember: which method called by

instance type

public void drawRed(Drawable d, Graphics g) {d.setColor(Color.red);d.draw(g);

}

Page 21: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Using Interfaces

Cannot instantiate an interface… Not possible, since only create instances of

class Variables of interface type are perfectly

legal Variable can refer to implementing class

instance Methods must be implemented in actual

class Remember: which method called by

instance type

public void drawRed(Drawable d, Graphics g) {d.setColor(Color.red);d.draw(g);

}

Page 22: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Interface vs. Abstract Class

Both concepts serve similar purposes Cannot instantiate either of these types But can be used for variable, field, &

parameter types Used in other classes to identify important

features But very important differences define when

each used

Page 23: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Interface vs. Abstract Class

Can extend classes & implement interfaces Both used to mark instances have methods

defined Fields, params, & locals can use either as

their type Use abstract class when…

…classes should use common method or private field

…place in object hierarchy as subclass of existing class

Otherwise use interface for abstract methods More flexible: class can implement many

interfaces Can mark classes; do not have to declare

methods

Page 24: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Typecasting

int i = 13;Square s = ((Square)i);

Only exist to “assist” compiler with code Changes variable’s type so compilation

continues Not in *.class file – does not affect

instance at all Only when KNOW instance & variables

types differ Errors at runtime instead of during

compilation Illegal code will compile, but still illegal

Page 25: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Typecasting

int i = 13;Square s = ((Square)i);

Only exist to “assist” compiler with code Changes variable’s type so compilation

continues Not in *.class file – does not affect

instance at all Only when KNOW instance & variables

types differ Errors at runtime instead of during

compilation Illegal code will compile, but still illegal

Page 26: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Narrowing Conversions

Java cannot compile narrowing conversions Assigns superclass/interface to lower

variable Compiler will not allow it, but could be

legal Typecasting required for these assignments

Object obj = new String(“bye”);

Page 27: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Narrowing Conversions

Java cannot compile narrowing conversions Assigns superclass/interface to lower

variable Compiler will not allow it, but could be

legal Typecasting required for these assignments

Object obj = new String(“bye”);

String sad = obj; // Does not work

Page 28: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Narrowing Conversions

Java cannot compile narrowing conversions Assigns superclass/interface to lower

variable Compiler will not allow it, but could be

legal Typecasting required for these assignments

Object obj = new String(“bye”);

String sad = obj; // Does not work

String glad = (String)obj; // works!

Page 29: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

Your Turn

Get into your groups and complete activity

Page 30: Question of the Day  Write valid mathematical equation using: 2 3 4 5 one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.

For Next Lecture

Read GT2.5 for Mon. and be ready to discuss What is a generic type? Why are they going to save our wrists this

year? How can they be used correctly?

As usual, there is weekly assignment on Angel Due by 5PM Tuesday via Assignment

Submitter Each problem graded using provided JUnit

tests