1 / 21

Object-Oriented Programming in Java

Object-Oriented Programming in Java. Object-Oriented Programming Outline. Introduction Superclass & subclass protected members Relationship between superclass objects & subclass objects Constructors & Finalizers in subclasses Composition versus Inheritance.

hang
Télécharger la présentation

Object-Oriented Programming in Java

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. Object-Oriented Programming in Java Peter Cappello

  2. Object-Oriented ProgrammingOutline • Introduction • Superclass & subclass • protected members • Relationship between superclass objects & subclass objects • Constructors & Finalizers in subclasses • Composition versus Inheritance

  3. Object-Oriented ProgrammingOutline ... • Dynamic Binding & Polymorphism • final Methods & Classes • Abstract classes & Concrete subclasses • Polymorphism examples • Creating & Using Interfaces • Type-Wrapper Classes for Primitive Types

  4. Introduction • Essential features of object-oriented programming (OOP) versus just using objects are: • inheritance - enables reuse of classes • polymorphism - enables flexible use of classes

  5. Superclasses & Subclasses • Reuse a class via inheritance when a new class is a refinement of it. • Examples: • Saturn is a Heavenlybody • Square is a Rectangle • Rectangle is a quadralateral • Quadralateral is a Polygon • Polygon is a shape

  6. Human Being Honest Human Being Criminal Organized Criminal Free-lance Criminal Government Agent Mafia Agent Burglar Murderer Rapist Senator IRS BATF Congressperson DEA

  7. protected members • Protected members (data & methods) are accessible to all members in: • its class • its subclasses • classes in its package

  8. Human Being Honest Human Being Criminal Organized Criminal Free-lance Criminal Protected data Bribe Source Government Agent Mafia Agent Burglar Murderer Rapist Senator IRS BATF Congressperson DEA

  9. Relationship between Superclass & Subclass Objects • An object can be referred to as a the type of its superclass. • Example: Zapem

  10. Using Constructors & Finalizers in Subclasses • When constructing an object, you can invoke the superclass constructor to initialize its instance variables: • Invoke the superclass constructor, using super([args]); as the first statement of the constructor. • Example: Saturn.java • If you do not invoke super(…), Java run-time invokes the no-argument superclass constructor to initialize its variables.

  11. Composition versus Inheritance • Composition: has a relation • Inheritance: is a relation • Oddly, the Point, Circle example in the text seems precisely off the point (a Circle is not a Point): • The attributes of a Circle are its center and its radius: it has a: • center (a Point) • radius (a double)

  12. Point & Circle public class Point { // Instance variables private double x, y; // coordinates of the Point // Methods public Point(double X, double Y) { setPoint(X, Y); } public void setPoint(double X, double Y) { x = X; y = Y; } public double getX() { return x; } public double getY() { return y; } public String toString() { return “[“ + x + “, “ + y + “]”; } }

  13. Point & Circle ... public class Circle { // Instance variables: a circle has a ... private Point center; // center of the Circle private double radius; // radius of the Circle // Methods public Circle(double x, double y, double r) { center = new Point(x, y); radius = r; } public double getRadius() { return radius; } public double area() { return Math.PI*radius*radius; } }

  14. Dynamic Binding & Polymorphism • Consider the statement: objectname.methodname([args]) • When this statement executes, it: • detects the type of object that objectname actually refers to; • invokes the appropriate version of the methodname method. • Look at the draw method in the Zapem example.

  15. final Methods & Classes • If a method is declared final, it cannot be overridden in a subclass. • A private method is implicitly final. • A static method is implicitly final. • If a class is declared final: • it cannot be subclassed (i.e., extended); • all its methods are implicitly final.

  16. Abstract Classes & Concrete Subclasses • An abstract class is a class that is: • designed to be extended, • not designed to construct objects. • An example of such a class is our Roach class: An actual roach is either a Rectangle or an Oval. • You cannot construct objects of an abstract class.

  17. Abstract Classes & Concrete Subclasses ... • You declare a class abstract by using the abstract keyword. • The purpose of doing so is to provide: • interface: method names & signatures • [implementation]: implement some methods for its extensions. • Please see modified Zapem example.

  18. Polymorphism examples • Zapem • SolarSystem - Please see use of Heavenly body class, and draw & move methods. • We can add new Roach subclasses & HeavenlyBody classes without recompiling the zapem & SolarSystem Applets.

  19. Creating & Using Interfaces • Interface isn’t just an idea, it’s a keyword! • An interface is used instead of a[n abstract] class when: • there is no implementation to inherit • the class already extends another class. • A modified Zapem uses an interface. • You also have seem their use in ActionListener and MouseListener

  20. Type-Wrapper Classes for Primitive Types • Each primitive data type has a corresponding wrapper class that has the same name but with the 1st letter capitalized • Integer, Double, Boolean, … • These classes allow you to treat their objects as objects: • refer to them polymorphically • pass them call-by-reference to methods.

  21. Type-Wrapper Classes for Primitive Types • Each defines some methods. E.g.: • int Integer.parseInt(String) • Double valueOf(String) • String toString() • Look in the java.lang package of the Java Core API Specification to find out more about these methods.

More Related