1 / 7

Lecture 8 Inheritance

Lecture 8 Inheritance. CS140 Dick Steflik. Inheritance. A class derived from another class is called a subclass also a a derived or child class The class that a subclass is derived from is called the super class also the parent class All classes are derived from class Object

imaran
Download Presentation

Lecture 8 Inheritance

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. Lecture 8Inheritance CS140 Dick Steflik

  2. Inheritance • A class derived from another class is called a subclass • also a a derived or child class • The class that a subclass is derived from is called the super class • also the parent class • All classes are derived from class Object • class Object has no super class

  3. Java Class Hierarchy All Java objects are derived from class Object

  4. An example public class Airplane{ public int speed, altitude, heading; public Airplane( int s, int a, int h){ speed = s; altitude = a; heading = h; } public setSpeed(int newSpeed) { speed = newSpeed; } public setAltitude(int newAltitude) { altitude = newAltitude; } public setHeading(int newHeading) { heading = newHeading; } public faster( int increment) { speed += increment;} public slower(int increment) { speed -= increment;} }

  5. a subclass of Airplane public class Fighter extends Airplane { public int ammo, missiles; public Fighter ( int initBullets, int initMissiles, initSpeed, int initAltit, int initHeading) { // let the super class initial its fields super( initSpeed, initSpeed, initHeading); ammo = initBullets; missiles = initMissiles; } public void launchMissile() { missile-- ;} public void fireBurst( int count) { ammo -= count)

  6. using the classes Fighter F18 = new F18(1000, 6, 0, 0, 0) F18.setSpeed = 100; while ( F18.getSpeed() < MACH1) { F18.faster(10); F18.climb(10); }

  7. Inherited public fields can be used directly • Inherited public methods can be used directly • you can declare new fields in the subclass that are not in the superclass • the constructor of the subclass can use the constructor of the super class • you can define new methods in the subclass that are not in the superclass • the subclass can have an instance method with the same signature as one in the superclass thus overriding it • you can write a new static method in the subclass that has the same signature as on in the super class thus hiding it • private fields and methods are not inherited

More Related