240 likes | 354 Views
Internet Software Development. Classes and Inheritance Paul J Krause. Classes and Inheritance. Contents Object-Oriented Programming in Java Fields and methods Implementing Inheritance Method overloading Abstract classes. The Members of a Class. Class fields
E N D
Internet Software Development Classes and Inheritance Paul J Krause
Classes and Inheritance Contents • Object-Oriented Programming in Java • Fields and methods • Implementing Inheritance • Method overloading • Abstract classes
The Members of a Class • Class fields • public static final double PI = 3.1416; • Class methods • public static double radiansToDegrees(double rads) {…} • Instance fields • public double radius; • Instance methods • public double circumference() {…}
Class Fields public static final double PI = 3.14159 • A field of type double • Named PI (capitalise constants) • Assigned a value of 3.14159 • The static modifier tags this as a Class Field • Associated with the class in which it is defined • The final modifier means it cannot be changed
Class Fields… • There is only one copy of PI • Any instance of Class can refer to this field as PI • PI is essentially a Global Variable 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; } • Single parameter of type double and returns a value of type double • Is essentially a “global method” // 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 = new Circle(); c.radius = 2.0; Circle d = new Circle(); d.radius = 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
Circle radius circumference area PlaneCircle cx cy isInside Implementing Inheritance
PlaneCircle cx cy isInside “PlaneCircle” as a subclass public class PlaneCircle extends Circle { } // automatically inherit fields and methods of Circle public double cx, cy; public PlaneCircle(double r, double x, double y) { super(r); this.cx = x; this.cy = y; } public boolean isInside(double x, double y) { … }
Subclass Constructors • 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 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 }
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?
Account number balance credit debit SavingsAccount credit debit Overriding methods
public class Account { public int number; public double balance; public void credit(double x) { // do some sums } public void debit(double y) { // do checking then sums } } public class SavingsAccount extends Account{ // instance fields inherited public void credit(double x) { // do some sums // update interest rate } public void debit(double y) { // do checking then sums // update interest rate } } Method Overriding
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
EllipticalShape circumference area Circle Ellipse radius semiMinorAxis semiMajorAxis Abstract Classes
Abstract Classes • An abstract class cannot be instantiated • A subclass of an abstract class can only be instantiated if: • 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 }