Your SlideShare is downloading. ×
0
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
11 advance inheritance_concepts
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

1,659

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,659
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
84
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×