1 / 12

Java Inheritance: Understanding Extended Classes

Learn how to extend existing classes in Java by adding or redefining methods and instance fields, with examples on inheritance, overriding methods, and implementing constructors.

jareda
Download Presentation

Java Inheritance: Understanding Extended Classes

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. Inheritance • INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class Vehicle{ String type; String model; }

  2. We want a class which to represent a Car. This class will need all of the attributes of a standard Vehicle plus some more..

  3. Inherited Fields are Private • Consider deposit method of CheckingAccountpublic void deposit(double amount){   transactionCount++;   // now add amount to balance   ...} • Can't just add amount to balance • balance is a private field of the superclass • Subclass must use public interface

  4. and all data of Vehicle are automaticaly inherited by class Car • Ok to call deposit, getBalance on SavingsAccount object • Extended class = superclass, extending class = subclass • Inheritance is different from realizing an interface • Interface is not a class • Interface supplies no instance fields or methods to inherit

  5. We want a class which to represent Cars. This class will need all of the behavior of a standard Vehicle plus some more.. • Constructor (calling superclass constructor

  6. Object: The Cosmic Superclass • All classes extend Object • Most useful methods: • String toString() • boolean equals(Object otherObject) • Object clone()

  7. The Object Class is the Superclass of Every Java Class

  8. Overriding the toString Method • Returns a string representation of the object • Useful for debugging • Example: Rectangle.toString returns something likejava.awt.Rectangle[x=5,y=10,width=20,height=30] • toString used by concatenation operator • aString + anObjectmeans aString + anObject.toString() • Object.toString prints class name and object addressBankAccount@d2460bf • Override toString:public class BankAccount{   public String toString()   {      return "BankAccount[balance=" + balance + "]";   }   . . .}

  9. Overriding the equals Method • equalstests for equal contents • == tests for equal location • Must cast the Object parameter to subclass • public class Coin{   public boolean equals(Object otherObject)   {      Coin other = (Coin)otherObject;      return name.equals(other.name) && value == other.value;   }}

  10. Two References to Equal Objects

  11. Two References to Same Object

  12. Overriding the clone Method • Copying object reference gives two references to same objectBankAccount account2 = account1; • Sometimes, need to make a copy of the object • Use clone:BankAccount account2 = (BankAccount)account1.clone(); • Must cast return value because return type is Object • Define clone method to make new object:public Object clone(){   BankAccount cloned = new BankAccount();   cloned.balance = balance;   return cloned;} • Warning: This approach doesn't work with inheritance--see Advanced Topic 11.6

More Related