Your SlideShare is downloading. ×
11 advance inheritance_concepts
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

11 advance inheritance_concepts


Published on

Published in: Technology, News & Politics

1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Java Programming Fifth Edition Advanced Inheritance Concepts
  • 2. Objectives
    • Create and use abstract classes
    • Use dynamic method binding
    • Create arrays of subclass objects
    • Use the Object class and its methods
    Java Programming, Fifth Edition
  • 3. Objectives (continued)
    • Use inheritance to achieve good software design
    • Create and use interfaces
    • Create and use packages
    Java Programming, Fifth Edition
  • 4. Creating and Using Abstract Classes
    • Abstract class
      • Cannot create any concrete objects
      • Can inherit
      • Usually has one or more empty abstract methods
      • Use keyword abstract
      • Provide superclass from which other objects can inherit
      • Example:
        • public abstract class Animal
    Java Programming, Fifth Edition
  • 5. Creating and Using Abstract Classes (continued)
    • Abstract method does not have:
      • Body
      • Curly braces
      • Method statements
    • To create abstract method
      • Keyword abstract
      • Header including method type, name, and arguments
      • Include semicolon at end of declaration
        • public abstract void speak();
    Java Programming, Fifth Edition
  • 6. Creating and Using Abstract Classes (continued)
    • Subclass of abstract class
      • Inherits abstract method from parent
        • Must provide implementation for inherited method
        • Or be abstract itself
      • Code subclass method to override empty superclass method
    Java Programming, Fifth Edition
  • 7. Using Dynamic Method Binding
    • Every subclass object “is a” superclass member
      • Convert subclass objects to superclass objects
      • Can create reference to superclass object
        • Create variable name to hold memory address
        • Store concrete subclass object
        • Example:
        • Animal ref;
        • ref = new Cow();
    Java Programming, Fifth Edition
  • 8. Using Dynamic Method Binding (continued)
    • Dynamic method binding
      • Also called late binding
      • Application’s ability to select correct subclass method
      • Makes programs flexible
    • When application executes
      • Correct method attached to application based on current one
    Java Programming, Fifth Edition
  • 9. Using Dynamic Method Binding (continued) Java Programming, Fifth Edition
  • 10. Using a Superclass as a Method Parameter Type
    • Use superclass as method argument
      • Pass in subclass
    • Use dynamic method binding
      • public static void talkingAnimal (Animal animal)
      • Dog dog = new Dog();
      • talkingAnimal(dog);
    Java Programming, Fifth Edition
  • 11. Java Programming, Fifth Edition
  • 12. Using a Superclass as a Method Parameter Type (continued) Java Programming, Fifth Edition
  • 13. Creating Arrays of Subclass Objects
    • Create superclass reference
      • Treat subclass objects as superclass objects
        • Create array of different objects
        • Share same ancestry
    • Creates array of three Animal references
      • Animal[] ref = new Animal[3];
      • Reserve memory for three Animal object references
    Java Programming, Fifth Edition
  • 14. Using the Object Class and Its Methods
    • Object Class
      • Every Java class extension of Object class
      • Defined in java.lang package
      • Imported automatically
      • Includes methods to use or override
    Java Programming, Fifth Edition
  • 15. Java Programming, Fifth Edition
  • 16. Using the toString() Method
    • toString() method
      • Converts Object into String
      • Contains information about Object
      • Output
        • Class name
        • @ sign
        • Hash code
    Java Programming, Fifth Edition
  • 17. Using the toString() Method (continued)
    • Write overloaded version of toString() method
      • Display some or all data field values for object
      • Can be very useful in debugging a program
        • Display toString() value
        • Examine contents
    Java Programming, Fifth Edition
  • 18. Using the toString() Method (continued) Java Programming, Fifth Edition
  • 19. Using the equals() Method
    • equals() method
      • Takes single argument
        • Same type as invoking object
      • Returns boolean value
        • Indicates whether objects are equal
      • Considers two objects of same class to be equal
        • Only if they have same hash code
    Java Programming, Fifth Edition
  • 20. Using the equals() Method (continued)
    • Example of equals() method:
      • if(someObject.equals (someOtherObjectOfTheSameType))
      • System.out.println("The objects are equal");
    • To consider objects to be equal based on contents
      • Must write own equals() method
    Java Programming, Fifth Edition
  • 21. Using the equals() Method (continued)
    • Object method hashCode()
      • Returns integer representing hash code
      • Whenever you override equals() method
        • Should override hashCode() method as well
        • Equal objects should have equal hash codes
    Java Programming, Fifth Edition
  • 22. Java Programming, Fifth Edition
  • 23. Using Inheritance To Achieve Good Software Design
    • Create powerful computer programs more easily
      • If components used “as is” or slightly modified
    • Make programming large systems more manageable
    Java Programming, Fifth Edition
  • 24. Using Inheritance To Achieve Good Software Design (continued)
    • Advantages of extendable superclasses
      • Save development time
        • Much code already written
      • Save testing time
        • Superclass code already tested
      • Programmers understand how superclass works
      • Superclass maintains its integrity
        • Bytecode not changed
    Java Programming, Fifth Edition
  • 25. Creating and Using Interfaces
    • Multiple inheritance
      • Inherit from more than one class
      • Prohibited in Java
      • Variables and methods in parent classes might have identical names
        • Creates conflict
        • Which class should super refer when child class has multiple parents?
    Java Programming, Fifth Edition
  • 26. Creating and Using Interfaces (continued)
    • Interface
      • Alternative to multiple inheritance
      • Looks like a class
        • Except all methods and data items implicitly public , abstract , and final
      • Description of what class does
      • Declares method headers
    Java Programming, Fifth Edition
  • 27. Java Programming, Fifth Edition
  • 28. Creating and Using Interfaces (continued) Java Programming, Fifth Edition
  • 29. Creating and Using Interfaces (continued) Java Programming, Fifth Edition
  • 30. Creating and Using Interfaces (continued)
    • Create an interface
      • Example: public interface Worker
    • Implement an interface
      • Keyword implements
        • Requires subclass to implement own version of each method
      • Use interface name in class header
        • Requires class objects to include code
        • public class WorkingDog extends Dog implements Worker
    Java Programming, Fifth Edition
  • 31. Creating and Using Interfaces (continued)
    • Abstract classes versus interfaces
      • Cannot instantiate concrete objects of either
      • Abstract classes
        • Can contain nonabstract methods
        • Provide data or methods that subclasses can inherit
        • Subclasses maintain ability to override inherited methods
        • Can include methods that contain actual behavior object performs
    Java Programming, Fifth Edition
  • 32. Creating and Using Interfaces (continued)
    • Abstract classes versus interfaces (continued)
      • Interface
        • Methods must be abstract
        • Programmer knows what actions to include
        • Every implementing class defines behavior that must occur when method executes
        • Class can implement behavior from more than one parent
    Java Programming, Fifth Edition
  • 33. Creating Interfaces to Store Related Constants
    • Interfaces can contain data fields
      • Must be public , static , and final
    • Interfaces contain constants
      • Provide set of data that many classes can use without having to redeclare values
    Java Programming, Fifth Edition
  • 34. Creating Interfaces to Store Related Constants (continued) Java Programming, Fifth Edition
  • 35. Creating and Using Packages
    • Package
      • Named collection of classes
      • Easily imports related classes into new programs
      • Encourages other programmers to reuse software
      • Helps avoid naming conflicts or collisions
      • Give every package a unique name
    Java Programming, Fifth Edition
  • 36. Creating and Using Packages (continued)
    • Create classes for others to use
      • Protect your work
        • Do not provide users with source code in files with .java extensions
        • Provide users with compiled files with .class extensions
      • Include package statement at beginning of class file
        • Place compiled code into indicated folder
    Java Programming, Fifth Edition
  • 37. Creating and Using Packages (continued)
    • Compile file to place in package
      • Use compiler option with javac command
        • -d option places generated .class file in folder
      • Use type-import-on-demand declaration:
        • import com.course.animals.*
      • Or use separate import statements for each class
        • Cannot import more than one package in one statement
    Java Programming, Fifth Edition
  • 38. Creating and Using Packages (continued)
    • Java ARchive (JAR) file
      • Delivered to users
      • Compress and store data
        • Reduce size of archived class files
      • Based on Zip file format
    Java Programming, Fifth Edition
  • 39. You Do It
    • Creating an abstract class
    • Extending an abstract class
    • Extending an abstract class with a second subclass
    • Instantiating objects from subclasses
    • Using object references
    Java Programming, Fifth Edition
  • 40. You Do It (continued)
    • Overriding the Object class equals() method
    • Eliminating duplicate user entries
    • Creating a package
    Java Programming, Fifth Edition
  • 41. Don’t Do It
    • Don’t write a body for an abstract method
    • Don’t forget to end an abstract method header with a semicolon
    • Don’t forget to override any abstract methods in any subclasses you derive
    • Don’t mistakenly overload an abstract method instead of overriding it
    • Don’t try to instantiate an abstract class object
    • Don’t forget to override all the methods in an interface that you implement
    Java Programming, Fifth Edition
  • 42. Summary
    • Abstract class
      • Class that you create only to extend from, but not to instantiate from
      • Usually contain abstract methods
        • Methods with no method statements
    • Can convert subclass objects to superclass objects
    • Dynamic method binding
      • Create a method that has one or more parameters that might be one of several types
      • Create an array of superclass object references but store subclass instances in it
    Java Programming, Fifth Edition
  • 43. Summary (continued)
    • Interface
      • Similar to a class
      • All methods implicitly public and abstract
      • All of its data implicitly public , static , and final
      • Create class that uses interface
        • Include keyword implements and interface name in class header
    • Place classes in packages
      • Convention uses Internet domain names in reverse order
    Java Programming, Fifth Edition