Download presentation
Presentation is loading. Please wait.
Published byMelinda Dennis Modified over 9 years ago
1
Internet Software Development Classes and Inheritance Paul J Krause
2
Classes and Inheritance Contents Object-Oriented Programming in Java Fields and methods Implementing Inheritance Method overloading Abstract classes
3
The Members of a Class Class fields public static final double PI = 3.1416; public static final double PI = 3.1416; Class methods public static double radiansToDegrees(double rads) {…} public static double radiansToDegrees(double rads) {…} Instance fields public double radius; public double radius; Instance methods public double circumference() {…} public double circumference() {…}
4
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 Associated with the class in which it is defined The final modifier means it cannot be changed
5
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 No name collisions
6
Class Methods public static double radiansToDegrees(double rads) { return rads * 180 / PI; 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);
7
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?
8
Instance Methods Instance methods operate on instances of a Class, and not on the Class itself E.g. area() area() circumference() 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(); Circle c = new Circle(); c.radius = 2.0; c.radius = 2.0; double a = c.area(); double a = c.area();
9
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
10
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);
11
Multiple Constructors public Circle() { r = 1.0; } public Circle(double r) {this.r = r;} This is a simple example of method overloading
12
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( ) Circle(double r) Circle(double r)
13
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 Holds a reference to the object c We also use “this” in order to make it clear an object is accessing its own fields
14
Destroying Objects Java automatically reclaims the memory occupied by an object when it is no longer needed Garbage Collection 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 Also works for cycles
15
Implementing Inheritance Circle radius circumference area PlaneCircle cx cy isInside
16
“PlaneCircle” as a subclass public class PlaneCircle extends Circle { } public double cx, cy; public PlaneCircle(double r, double x, double y) { super(r); this.cx = x; this.cy = y; } // automatically inherit fields and methods of Circle PlaneCircle cx cy isInside public boolean isInside(double x, double y) { … }
17
Subclass Constructors In this case, the word “super”: Invokes the constructor method of the superclass Invokes the constructor method of the superclass Must only be used in this way within a constructor method Must only be used in this way within a constructor method Must apear within the first statement of the 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 }
18
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?
19
Overriding methods Account number balance credit debit SavingsAccount credit debit
20
Method Overriding 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 } }
21
Overloading vs. Overriding Overloading: Multiple methods with the same name In the same class, but In the same class, but Different parameter lists Different parameter lists Overriding: Multiple methods methods with the same name With exactly the same signatures, but With exactly the same signatures, but In different classes in an inheritance hierarchy In different classes in an inheritance hierarchy
22
Abstract Classes EllipticalShape circumference area Circle radius Ellipse semiMinorAxis semiMajorAxis
23
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 It overrides each of the abstract methods of its superclass, and Provides a concrete implementation of them Provides a concrete implementation of them It’s then known as a concrete class
24
Java Definition public abstract class Elliptical Shape { public abstract double area( ) ; public abstract double circumference( ) ; // Note semicolons instead of body of methods }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.