1 / 22

CS202 Java Object Oriented Programming Advanced OOP Topics

CS202 Java Object Oriented Programming Advanced OOP Topics. Chengyu Sun California State University, Los Angeles. Overview. Abstract Classes Multiple inheritance and Interfaces Nested classes. Attributes Location Length, width, Radius Operations Move Draw. Shapes. Rectangle. (x,y).

jamessexton
Download Presentation

CS202 Java Object Oriented Programming Advanced OOP Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS202 Java Object Oriented ProgrammingAdvanced OOP Topics Chengyu Sun California State University, Los Angeles

  2. Overview • Abstract Classes • Multiple inheritance and Interfaces • Nested classes

  3. Attributes Location Length, width, Radius Operations Move Draw Shapes Rectangle (x,y) height width Circle Point radius (x,y) (x,y)

  4. Shape Class public class Shape { protected int x, y; // initial location public Shape( int x, int y ) { this.x = x; this.y = y; } public void move( int newX, int newY ) { x = newX; y = newY; } public void draw() { ??? } }

  5. An abstract class Some operations are known and some are not Unknown operations can be declared as abstract methods Cannot be instantiated Abstract Shape Class public abstract class Shape { int x, y; // location public Shape( int x, int y ) { this.x = x; this.y = y; } void move( int newX, int newY ) { x = newX; y = newY; } public abstract void draw(); }

  6. Subclasses of Shape • Point, Rectangle, and Circle • A concrete class • A subclass of an abstract superclass • Must implement (override) the abstract methods • Can be instantiated • Why do we need a superclass when there’s so little code reuse??

  7. Sort Integers public void sort( int a[] ) { int left = 0; while( left < a.length-1 ) { int index = left; for( int i=left ; i < a.length ; ++i ) if( a[i] < a[index] ) index = i; // swap a[index] and a[left] int tmp = a[index]; a[index] = a[left]; a[left] = tmp; ++left; } }

  8. Sort Objects • Any objects that has a lessThan() method public abstract class Comparable { public Comparable() {} // return true if this object is less than o public abstract boolean lessThan( ?? o ); }

  9. A More General Sort public void sort( Comparable a[] ) { int left = 0; while( left < a.length-1 ) { int index = left; for( int i=left ; i < a.length ; ++i ) if( a[i].lessThan(a[index]) ) index = i; // swap a[index] and a[left] int tmp = a[index]; a[index] = a[left]; a[left] = tmp; ++left; } }

  10. The Need for Multiple Inheritance • What if we want to sort an array of Point? • Inherit both Shape and Comparable?

  11. The Problem of Multiple Inheritance public class A { … … public int x; public void foobar() { … } } public class B { … … public int x; public void foobar() { … } } public class C extends A, B { … … } • Which x or foobar() does C inherit?

  12. Interface • Java’s answer to multiple inheritance • A interface only contains • Method declarations • No method implementations • All methods are implicitly public and abstract • Constants • All constants are implicitly public, static, and final

  13. Interface Examples public interface ActionListener { public void actionPerformed(ActionEvent ae); } public interface AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent e); } public interface MouseListener { public void mousePressed(); public void mouseClicked(); public void mouseReleased(); public void mouseEntered(); public void mouseExited(); }

  14. Comparable Interface public interface Comparable { boolean lessThan( Object c ); }

  15. Interface Usage public class Point extends Shape implements Comparable { public Point( int x, int y ) { super(x,y); } public void draw() { … } public boolean lessThan( Object o ) { Point p = (Point) o; // cast to a Point for comparable ?? } } // end of class Point

  16. Exercise: Interface Constants public interface InterA { int x = 10; void print(); } public interface InterB { int x = 20; void print(); } public class C implements InterA, InterB { void print() { System.out.println(x); } public static void main( String args[] ) { C c = new C(); c.print(); } } • Try run the code above, observe the error, and correct it

  17. Abstract class An incomplete class Class variables Constructors Methods and abstract methods extends Single inheritance Cannot be instantiated Interface Not a class at all Only constants No constructors Only abstract methods (method declarations) implements Multiple implementation Cannot be instantiated Abstract Class vs. Interface

  18. Nested Classes • A class inside another class pubic class A { … // a nested class class B { … } }

  19. An “Instance Class” Class Foo { int a; public void print() { System.out.print(a); } class Bar { int b; } } instance variable instance method “instance class” An instance of a nested class is always associated with an instance of the outer class.

  20. A Nested Class Example • ArrayWrapper • ArrayWrapperIterator • Iterator • hasNext() • next ()

  21. Properties of Nested Class • Type • Inside outer class: InnerClassName • Outside outer class: OuterClassName.InnerClassName • Instantiation • Inside outer class: new • Outside outer class: outerClassObject.new • Can access all members of the outer class, including private members

  22. Variants of Nested Classes • Non-static nested class: Inner Class • Static nested class • Anonymous class • Local class

More Related