1 / 24

Internet Software Development

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

riva
Download Presentation

Internet Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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; • Class methods • public static double radiansToDegrees(double rads) {…} • Instance fields • public double radius; • Instance methods • 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 • 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

  6. 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);

  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() • 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();

  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(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 • 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 • The Java interpreter can determine when an object is no longer referred to by any other object or variable • Also works for cycles

  15. Circle radius circumference area PlaneCircle cx cy isInside Implementing Inheritance

  16. 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) { … }

  17. 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 }

  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. Account number balance credit debit SavingsAccount credit debit Overriding methods

  20. 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

  21. 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

  22. EllipticalShape circumference area Circle Ellipse radius semiMinorAxis semiMajorAxis Abstract Classes

  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 • 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 }

More Related