Intro_to_Java_2

Download Report

Transcript Intro_to_Java_2

Object-Oriented
Programming with Java
Classes and Inheritance
Lecture 2
Object-Orientation & Java
Contents
 Object-Oriented Programming in Java
 Fields and methods
 Implementing Inheritance
 Method overloading
 Abstract classes
The Members of a Class

Class fields


Class methods


public static double
radiansToDegrees(double rads) {…}
Instance fields


public static final double PI = 3.1416;
public double radius;
Instance methods

public double circumference() {…}
Class Fields
public static final double PI = 3.14159
of type double
 Named PI (capitalise constants)
 Assigned a value of 3.14159
 The static modifier tags this as a Class
Field
 A field

Associated with the class in which it is defined
final modifier means it cannot be
changed
 The
Class Fields…
is only one copy of PI
 Any instance of Class can refer to this
field as PI
 PI is essentially a Global Variable
 There
BUT
 Methods that are not part of Circle
access this as Circle.PI

No name collisions
Class Methods
public static double
radiansToDegrees(double rads) {
return rads * 180 / PI;
}
parameter of type double and
returns a value of type double
 Is essentially a “global method”
 Single
// how many degrees is 2.0 radians?
double d =
Circle.radiansToDegrees(2.0);
Instance Fields
public double radius;
 Each Circle object can have a have a radius
independent of other Circle objects
 Outside a class, a reference to an instance field
must be prepended by a reference to the object
that contains it
Circle c
c.radius
Circle d
d.radius
=
=
=
=
new Circle();
2.0;
new Circle();
c.radius; Are they the same object?
Instance Methods

Instance methods operate on instances of a
Class, and not on the Class itself
 E.g.



area()
circumference()
If an instance method is used from outside the
Class itself, it must be prepended by a reference
to the instance to be operated on:



Circle c = new Circle();
c.radius = 2.0;
double a = c.area();
Creating an Instance
 Every
Class has at least one constructor
 This is used as a default constructor - a
method with the same name as the Class
Circle c = new Circle();
 The new operator creates a new
uninitialised instance of the Class
 The constructor method is then called,
with the new object passed implicitly
Initialising an Instance

A Constructor can use arguments placed
between the parentheses to perform initialisation
 Define a new Constructor for Circle
public Circle(double r) {this.r = r;}

Now two ways:
Circle c = new Circle();
c.r = 0.25;

Or
Circle c = new Circle(0.25);
Multiple Constructors
public Circle() { r = 1.0; }
public Circle(double r) {this.r = r;}
 This
is a simple example of method
overloading
Method Overloading
 Definition
of multiple methods with the
same name.
 This is perfectly legal in Java, provided
each version of the method has a different
parameter list (so there is no ambiguity)
 E.g.


Circle( )
Circle(double r)
This and that

Consider the following code fragment:
Circle c = new Circle(1.0);
double a = c.area();

What are those empty parentheses doing there?
 How does a function with no parameters know
what data to operate on?
 There is an implicit argument named this:


Holds a reference to the object c
We also use “this” in order to make it clear an
object is accessing its own fields
Destroying Objects
 Java
automatically reclaims the memory
occupied by an object when it is no longer
needed

Garbage Collection
 The
Java interpreter can determine when
an object is no longer referred to by any
other object or variable

Also works for cycles
Implementing Inheritance
Circle
radius
circumference
area
PlaneCircle
cx
cy
isInside
“PlaneCircle” as a subclass
public class PlaneCircle extends Circle {
// automatically inherit fields and methods of Circle
public double cx, cy;
PlaneCircle
public PlaneCircle(double r, double x, double y) {
super(r);
this.cx = x;
this.cy = y;
}
cx
cy
isInside
public boolean isInside(double x, double y) {
…
}
}
Subclass Constructors
public PlaneCircle(double r, double x, double y) {
super(r);
// invoke constructor of superclass
this.cx = x; // initialise instance field cx
this.cy = y; // initialise instance field cy
}

In this case, the word “super”:



Invokes the constructor method of the superclass
Must only be used in this way within a constructor
method
Must apear within the first statement of the
constructor method
Making more circles

PlaneCircle pc = new PlaneCircle(1.0, 0.0, 0.0);
// Create a unit circle at the origin

double a = pc.area( );
// Calculate it’s area by invoking an inherited method

boolean test = pc.isInside(1.5, 1.5);
// Test if the point (1.5, 1.5) is inside the PlaneCircle pc
or not

What other methods might we want in
PlaneCircle?
Overriding methods
Account
number
balance
credit
debit
SavingsAccount
credit
debit
Method Overriding
public class Account {
public class SavingsAccount
extends Account {
public int number;
public double balance;
// instance fields inherited
public void credit(double x) {
// do some sums
}
public void debit(double y) {
// do checking then sums
}
public void credit(double x) {
// do some sums
// update interest rate
}
public void debit(double y) {
// do checking then sums
// update interest rate
}
}
}
Overloading vs. Overriding
 Overloading:
Multiple methods with the
same name


In the same class, but
Different parameter lists
 Overriding:
Multiple methods methods with
the same name


With exactly the same signatures, but
In different classes in an inheritance hierarchy
Abstract Classes
EllipticalShape
circumference
area
Circle
radius
Ellipse
semiMinorAxis
semiMajorAxis
Abstract Classes
abstract class cannot be instantiated
 A subclass of an abstract class can only
be instantiated if:
 An


It overrides each of the abstract methods of
its superclass, and
Provides a concrete implementation of them
 It’s
then known as a concrete class
Java Definition
public abstract class Elliptical Shape {
public abstract double area( ) ;
public abstract double circumference( ) ;
// Note semicolons instead of body of methods
}