Multiple Inheritance Multiple Inheritance Some object oriented - - PowerPoint PPT Presentation

multiple inheritance multiple inheritance
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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.

slide-2
SLIDE 2

Overriding Methods

Overriding Method

f1() { }

Extended Method

f1() { super.f1(); System.out.println(“Adder”); }

Reorder Method

f1() { System.out.println(“Adder”); super.f1(); }

slide-3
SLIDE 3

Overriding Methods Overriding Methods: : The Solution The Solution

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); } } }

slide-4
SLIDE 4

Subclasses and Polymorphism

slide-5
SLIDE 5

toString() Methods

Print methods are common in some languages, but most Java programs

  • perate 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."); }

slide-6
SLIDE 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); } } }

slide-7
SLIDE 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 +"]"); }

slide-8
SLIDE 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.

slide-9
SLIDE 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; }

slide-10
SLIDE 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; } } }

slide-11
SLIDE 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

  • f writing

Car c = new Car("New York", 89.7); double maximumLegalSpeed = c.getSpeedLimit();

you just write

double maximumLegalSpeed = Car.getSpeedLimit();

slide-12
SLIDE 12

The equals The equals() () method method

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

slide-13
SLIDE 13

The equals The equals() () method method

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