210 likes | 223 Views
Learn the basics of object-oriented programming in Java, including inheritance, composition, polymorphism, and interfaces.
E N D
Object-Oriented Programming in Java Peter Cappello
Object-Oriented ProgrammingOutline • Introduction • Superclass & subclass • protected members • Relationship between superclass objects & subclass objects • Constructors & Finalizers in subclasses • Composition versus Inheritance
Object-Oriented ProgrammingOutline ... • Dynamic Binding & Polymorphism • final Methods & Classes • Abstract classes & Concrete subclasses • Polymorphism examples • Creating & Using Interfaces • Type-Wrapper Classes for Primitive Types
Introduction • Essential features of object-oriented programming (OOP) versus just using objects are: • inheritance - enables reuse of classes • polymorphism - enables flexible use of classes
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
Human Being Honest Human Being Criminal Organized Criminal Free-lance Criminal Government Agent Mafia Agent Burglar Murderer Rapist Senator IRS BATF Congressperson DEA
protected members • Protected members (data & methods) are accessible to all members in: • its class • its subclasses • classes in its package
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
Relationship between Superclass & Subclass Objects • An object can be referred to as a the type of its superclass. • Example: Zapem
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.
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)
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 + “]”; } }
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; } }
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.
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.
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.
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.
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.
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
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.
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.