Upload
hang
View
55
Download
7
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
04/22/23 Peter Cappello [email protected]
Object-Oriented Programming Object-Oriented Programming in Javain Java
04/22/23 [email protected]
Object-Oriented ProgrammingObject-Oriented ProgrammingOutlineOutline
IntroductionSuperclass & subclassprotected membersRelationship between superclass objects &
subclass objectsConstructors & Finalizers in subclassesComposition versus Inheritance
04/22/23 [email protected]
Object-Oriented ProgrammingObject-Oriented ProgrammingOutline ...Outline ...
Dynamic Binding & Polymorphismfinal Methods & ClassesAbstract classes & Concrete subclassesPolymorphism examplesCreating & Using InterfacesType-Wrapper Classes for Primitive Types
04/22/23 [email protected]
IntroductionIntroduction
Essential features of object-oriented programming (OOP) versus just using objects are:– inheritance - enables reuse of classes– polymorphism - enables flexible use of
classes
04/22/23 [email protected]
Superclasses & SubclassesSuperclasses & 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
04/22/23 [email protected]
Human Being
HonestHuman Being
Criminal
OrganizedCriminal
Free-lanceCriminal
Burglar
Rapist
MurdererMafiaAgent
GovernmentAgent
Senator
Congressperson
IRS
BATF
DEA
04/22/23 [email protected]
protected membersprotected members
Protected members (data & methods) are accessible to all members in:– its class– its subclasses– classes in its package
04/22/23 [email protected]
Human Being
HonestHuman Being
Criminal
OrganizedCriminal
Free-lanceCriminal
Burglar
Rapist
MurdererMafiaAgent
GovernmentAgent
Senator
Congressperson
IRS
BATF
DEA
Bribe Source
Protected data
04/22/23 [email protected]
Relationship between Superclass Relationship between Superclass & Subclass Objects& Subclass Objects
An object can be referred to as a the type of its superclass.
Example: Zapem
04/22/23 [email protected]
Using Constructors & Finalizers Using Constructors & Finalizers in Subclassesin 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.
04/22/23 [email protected]
Composition versus InheritanceComposition versus Inheritance
Composition: has a relationInheritance: is a relationOddly, 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)
04/22/23 [email protected]
Point & CirclePoint & Circle
public class Point {// Instance variablesprivate double x, y; // coordinates of the Point// Methodspublic 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 + “]”; }
}
04/22/23 [email protected]
Point & Circle ...Point & Circle ...public class Circle {
// Instance variables: a circle has a ...private Point center; // center of the Circleprivate double radius; // radius of the Circle// Methodspublic 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; }
}
04/22/23 [email protected]
Dynamic Binding & PolymorphismDynamic 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.
04/22/23 [email protected]
final Methods & Classesfinal Methods & ClassesIf 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.
04/22/23 [email protected]
Abstract Classes & Concrete Abstract Classes & Concrete SubclassesSubclasses
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.
04/22/23 [email protected]
Abstract Classes & Concrete Abstract Classes & Concrete Subclasses ...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.
04/22/23 [email protected]
Polymorphism examplesPolymorphism examples
ZapemSolarSystem - 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.
04/22/23 [email protected]
Creating & Using InterfacesCreating & Using InterfacesInterface 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
04/22/23 [email protected]
Type-Wrapper Classes for Type-Wrapper Classes for Primitive TypesPrimitive 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.
04/22/23 [email protected]
Type-Wrapper Classes for Type-Wrapper Classes for Primitive TypesPrimitive 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.