multiple inheritance multiple inheritance
play

Multiple Inheritance Multiple Inheritance Some object oriented - PowerPoint PPT Presentation

Multiple Inheritance Multiple Inheritance Some object oriented languages allow a class to inherit from more than one unrelated class. This is called multiple inheritance and is different from the multi-level inheritance in this


  1. Multiple Inheritance Multiple Inheritance � Some object oriented languages allow a class to inherit from more than one unrelated class. � This is called multiple inheritance and is different from the multi-level � inheritance in this section. � Most of the things that can be accomplished via multiple inheritance in C++ can be handled by interfaces in Java.

  2. Overriding Methods � Overriding Method f1() { } � Extended Method f1() { super.f1(); System.out.println(“Adder”); } � Reorder Method f1() { System.out.println(“Adder”); super.f1(); }

  3. Overriding Methods: : The Solution The Solution Overriding Methods public class SlowCar extends Car { private static final double speedLimit = 112.65408; //kph == 70 mph public SlowCar(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers, int numDoors) { super(licensePlate, speed, maxSpeed, make, model, year, numberOfPassengers, numDoors); if (speed > speedLimit) this.speed = speedLimit; } public void accelerate(double deltaV) { double speed = this.getSpeed() + deltaV; if (speed > speedLimit) { super.accelerate(speedLimit - this.getSpeed()); } else { super.accelerate(deltaV); } } }

  4. Subclasses and Polymorphism

  5. toString() Methods � Print methods are common in some languages, but most Java programs operate differently. You can use System.out.println() to print any object. However for good results your class should have a toString() method that formats the object's data in a sensible way and returns a string. public String toString() { return (this.licensePlate + " is moving at " + this.speed + "kph and has a maximum speed of " + this.maxSpeed + "kph."); }

  6. Using toString() Methods class CarTest5 { public static void main(String args[]) { Car c = new Car("New York A45 636", 123.45); System.out.println(c); for (int i = 0; i < 15; i++) { c.accelerate(10.0); System.out.println(c); } } }

  7. Rules for toString() Methods � toString() methods should return a single line of text that does not contain any carriage returns or linefeeds. public String toString() { return "[Car: plate=" + this.licensePlate + " speed=" + this.speed + "MaxSpeed=" + this.maxSpeed +"]"); }

  8. Class or static Members Class or static Members � A method or a field in a Java program can be declared static. This means the member belongs to the class rather than to an individual object. � If a variable is static, then when any object in the class changes the value of the variable, that value changes for all objects in the class.

  9. Class or static Members public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour private static double speedLimit = 112.0; // kilometers per hour public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } // getter (accessor) methods public static double getSpeedLimit() { return speedLimit; } public boolean isSpeeding() { return this.speed > speedLimit; } public String getLicensePlate() { return this.licensePlate; }

  10. public double getMaxSpeed() { return this.maxSpeed; } public double getSpeed() { return this.speed; } // setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } }

  11. Invoking static methods � If a method or field is declared static, you access it by using the class name rather than a reference to a particular instance of the class. Therefore instead of writing Car c = new Car("New York", 89.7); double maximumLegalSpeed = c.getSpeedLimit(); � you just write double maximumLegalSpeed = Car.getSpeedLimit();

  12. The equals() () method method The equals � The equals() method of java.lang.Object acts the same as the == operator; that is, it tests for object identity rather than object equality. Thus most classes will override equals() with a version that does field by field comparisons before deciding whether to return true or false. public boolean equals(Car c) { if (this.licensePlate.equals(c.licensePlate)) return true; } return false; }

  13. The equals() () method method The equals public boolean equals(Object o) { if (o instanceof Car) { Car c = (Car) o; if (this.licensePlate.equals(c.licensePlate)) return true; } return false; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend