1 / 10

Big Ideas behind Inheritance

Big Ideas behind Inheritance. Extending classes. Sub classes extend super classes IS A relationship (arrow) Constructors that use good style will always call the super constructor. The call to super() must be the first action in constructors.

debra
Télécharger la présentation

Big Ideas behind 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. Big Ideas behind Inheritance

  2. Extending classes • Sub classes extend super classes • IS A relationship (arrow) • Constructors that use good style will always call the super constructor. The call to super() must be the first action in constructors. • All fields and methods are inherited by sub classes • Private fields in super class CANNOT be accessed in a sub class. • Methods need only be overridden when a changed to the method is needed in the sub class. • Super is only used with methods when a method has been overridden. i.e. distinguish between super.speak() and speak() • super.super.someMethod() is not allowed in Java.

  3. Polymorphism and casting • Polymorphism the process of choosing which class’ methods will be execute at run time. When polymorphic code is written it is NOT known which method will be called at COMPILE time; the decision is made at RUN time. This is also called “dynamic binding,” “late binding” or “run time binding.”

  4. The Cosmic superclass • The Object class is know as the cosmic superclass because all other java classes extend it by default (implied). • It has some standard methods such as toString(), and equals(Object other) which all other classes inherit and often override.

  5. Method checked at Compile Time • Consider this: Object obj = new BankAccount(); Will the complier like this? ((BankAccount)obj). getBalance(); Why or Why not?

  6. Another example BankAccount b1 = new CheckingAccount(100); Is this OK? b1.printCheck(); Is this OK? b1.deposit();

  7. Which method runs determined is at RUN TIME BankAccount b1 = new CheckingAccount(100); b1.deposit(); //which method runs? b1.withdraw(); //which method runs? b1.getBalance(); //which method runs?

  8. Bottom Line • The compiler checks method calls at compile time based on the type of OBJECT VARIABLE • When a method is overridden, which method is used (super or sub) is determined at run time by the type of OBJECT

  9. Try This • Create BankAccount, CheckingAccount, and SavingsAccount objects • Refer to each with BankAccount variables. • Try all methods out – ask questions if the results do not make sense. Try to predict them and see if you are correct.

  10. Abstract Classes and Interfaces • Inheritance can be used to REQUIRE OR SPECIFCY WHAT methods MUST be overridden. • An abstract method MUST be overridden. • An interface is a totally abstract class. • Interfaces are essential to uniformity in OOP and when leveraged correctly they are powerful timesavers.

More Related