Lecture15

Lecture15 - UML: Abstract Class and Abstract Methods 1...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 UML: Abstract Class and Abstract Methods
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Abstract Class Members An abstract class may have static fields and static methods. You can use these static members with a class reference. For example, AbstractClass.staticMethod() —as you would with any other class. Abstract Static method? abstract class foo { abstract void bar( ); // < ‐‐ this is ok abstract static void bar2(); //< ‐‐ this isn't why? } "abstract" means: "Implements no functionality", and "static" means: "There is functionality even if you don't have an object instance". And that's a logical contradiction.
Background image of page 2
3 Chapter 8: Polymorphism polymorphism means "having many forms", it is based on the late binding technique. Outline Late binding Polymorphism via Inheritance Polymorphism via Interfaces Late binding Connecting a method call to a method body is called binding. obj.doIt(); When binding is performed before the program is run (by the compiler and linker, if there is one), it is called early binding. Java defers method binding until run time -- this is called dynamic binding or late binding
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Java's use late binding that allows you to declare an object as one class at compile-time but executes based on the actual class at runtime. Outline Late binding Polymorphism via Inheritance Polymorphism via Interfaces Polymorphism via Inheritance Shape draw() erase() Circle draw() erase() Triangle draw() erase() Line draw() erase() Shape s = new Circle(); Shape s = new Triangle(); Shape s = new Line(); Upcast The process of treating a child class as though it were its parent class . Considered to be a widening conversion, and can be performed by simple assignment
Background image of page 4
5 When you call one of the parent class methods (that have been overridden in the child classes), it is the type of the object being referenced, not the reference type, that determines which method is invoked. Shape s = new Circle(); s.draw(); Shape s = new Line(); s.draw(); Shape s = new triangle(); s.draw(); Shape draw() erase() Circle draw() erase() Triangle draw() erase() Line draw() erase() The proper draw( ) is called because of late binding. s is polymorphic because it has the ability to take on different forms. It will "act like" an object of the class it currently references. Method-call mechanism determines the type of the object at run-time and calls the appropriate method.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 class LB_1 { public String retValue(String s) { return "In LB_1 with " + s; } } class LB_2 extends LB_1 { public String retValue(String s) { return "In LB_2 with " + s; } } main(): LB_2 lb2 = new LB_2(); LB_1 lb3 = lb2; // compiles ok System.out.println(lb3.retValue("Today"));
Background image of page 6
7 abstract class Shape { void draw() {}; //abstract void draw(); void erase() {}; //abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("Circle.draw()"); } void erase() { System.out.println("Circle.erase()"); }} class Square extends Shape { void draw() { System.out.println("Square.draw()"); } void erase() { System.out.println("Square.erase()");
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 26

Lecture15 - UML: Abstract Class and Abstract Methods 1...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online