logging in or signing up JAVA vasu2891 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: Embed: Flash iPad Dynamic Copy Does not support media & animations Automatically changes to Flash or non-Flash embed WordPress Embed Customize Embed URL: Copy Thumbnail: Copy The presentation is successfully added In Your Favorites. Views: 164 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: August 22, 2011 This Presentation is Public Favorites: 0 Presentation Description super class Comments Posting comment... Premium member Presentation Transcript A superclass variable can reference a subclass object: A superclass variable can reference a subclass object Presentation by, Revathi.S 09MTBI20 JAVAOBJECTIVES: Superclass variable can reference a subclass object Java program for Reference demo Keyword-’ SUPER ’ Using super to call superclass constructors Initialization of box attributes to BoxWeight using Super Java prog ., for implementation of Box weight General forms of super OBJECTIVESIntroduction: Reference variable of superclass assigned to subclass Boxweight -subclass of box-superclass IntroductionSlide 4: Plainbox is reference of box & weightbox is reference of boxweight when a reference to a subclass object is assigned to a superclass reference variable , you will have access only to those parts of the object defined by the superclass.Slide 5: Java prog .. For Reference variableSlide 6: class RefDemo { public static void main(String args []) { BoxWeight weightbox = new BoxWeight (3, 5, 7, 8.37); Box plainbox = new Box ();// superclass_name reference-variable=new object inialization double vol ; vol = weightbox.volume (); System.out.println ("Volume of weightbox is " + vol ); System.out.println (" Weight of weightbox is " + weightbox.weight ); System.out.println ();Slide 7: // assign BoxWeight reference to Box reference plainbox = weightbox ; vol = plainbox.volume (); //volume() defined in Box System.out.println ("Volume of plainbox is " + vol ); /* The following statement is invalid because plainbox doesnot define a weight member */ // System.out.println ("Weight of Plainbox is " + plainbox.weight ); } }Using SUPER: Subclass need to refer to its immediate superclass-’ super ’ keyword Two general forms. calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass Using SUPERUsing super to call superclass constructor: Subclass can call a constructor method defined by its superclass by use of the following form-SUPER Super(parameter-list); Using super to call superclass constructorA second use of super: second form of super Acts like ‘ this’- keyword Except that it always refers to the superclass of the subclass in which it is used A second use of superthis: Refer to the object that invoked it Used inside any method to refer to the current object Always a reference to an object on which the method was invoked t hisGeneral form: Super.member Member-method or an instance variable situations - member names of a subclass hide members by the same name in the superclass General formSlide 13: Java prog .. for Class hierarchySlide 14: //using super to overcome name hiding class A{ int i; } //creating a subclass by extending class A class B extends A{ int i ; // this i hides the i in A B( int a,int b){ super.i =a; // i in A i=b; // i in B }Slide 15: void show(){ System.out.println ("i in superclass:",+ super.i ); System.out.println ("i in subclass:",+ super.i ); } } class Usesuper { public static void main(String args []){ B subOb =new B(1,2); subOb.show (); } }output: i in superclass:1 i in subclass:2 outputSlide 17: Instance variable i hides in B hides the i in A,super allows access to I in A Super also used to call methods that are hidden by subclassConclusion: We have discussed about… Superclass variable can reference a subclass object Using super to call superclass constructors General forms of super calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass ConclusionReferences: The Complete Reference JAVA 2-Third Edition-Patrick Naughton and Herbert Schildt .(pg:198-205) References You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
JAVA vasu2891 Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: Embed: Flash iPad Dynamic Copy Does not support media & animations Automatically changes to Flash or non-Flash embed WordPress Embed Customize Embed URL: Copy Thumbnail: Copy The presentation is successfully added In Your Favorites. Views: 164 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: August 22, 2011 This Presentation is Public Favorites: 0 Presentation Description super class Comments Posting comment... Premium member Presentation Transcript A superclass variable can reference a subclass object: A superclass variable can reference a subclass object Presentation by, Revathi.S 09MTBI20 JAVAOBJECTIVES: Superclass variable can reference a subclass object Java program for Reference demo Keyword-’ SUPER ’ Using super to call superclass constructors Initialization of box attributes to BoxWeight using Super Java prog ., for implementation of Box weight General forms of super OBJECTIVESIntroduction: Reference variable of superclass assigned to subclass Boxweight -subclass of box-superclass IntroductionSlide 4: Plainbox is reference of box & weightbox is reference of boxweight when a reference to a subclass object is assigned to a superclass reference variable , you will have access only to those parts of the object defined by the superclass.Slide 5: Java prog .. For Reference variableSlide 6: class RefDemo { public static void main(String args []) { BoxWeight weightbox = new BoxWeight (3, 5, 7, 8.37); Box plainbox = new Box ();// superclass_name reference-variable=new object inialization double vol ; vol = weightbox.volume (); System.out.println ("Volume of weightbox is " + vol ); System.out.println (" Weight of weightbox is " + weightbox.weight ); System.out.println ();Slide 7: // assign BoxWeight reference to Box reference plainbox = weightbox ; vol = plainbox.volume (); //volume() defined in Box System.out.println ("Volume of plainbox is " + vol ); /* The following statement is invalid because plainbox doesnot define a weight member */ // System.out.println ("Weight of Plainbox is " + plainbox.weight ); } }Using SUPER: Subclass need to refer to its immediate superclass-’ super ’ keyword Two general forms. calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass Using SUPERUsing super to call superclass constructor: Subclass can call a constructor method defined by its superclass by use of the following form-SUPER Super(parameter-list); Using super to call superclass constructorA second use of super: second form of super Acts like ‘ this’- keyword Except that it always refers to the superclass of the subclass in which it is used A second use of superthis: Refer to the object that invoked it Used inside any method to refer to the current object Always a reference to an object on which the method was invoked t hisGeneral form: Super.member Member-method or an instance variable situations - member names of a subclass hide members by the same name in the superclass General formSlide 13: Java prog .. for Class hierarchySlide 14: //using super to overcome name hiding class A{ int i; } //creating a subclass by extending class A class B extends A{ int i ; // this i hides the i in A B( int a,int b){ super.i =a; // i in A i=b; // i in B }Slide 15: void show(){ System.out.println ("i in superclass:",+ super.i ); System.out.println ("i in subclass:",+ super.i ); } } class Usesuper { public static void main(String args []){ B subOb =new B(1,2); subOb.show (); } }output: i in superclass:1 i in subclass:2 outputSlide 17: Instance variable i hides in B hides the i in A,super allows access to I in A Super also used to call methods that are hidden by subclassConclusion: We have discussed about… Superclass variable can reference a subclass object Using super to call superclass constructors General forms of super calls superclass constructor used to access the member of the super class that has been hidden by member of a subclass ConclusionReferences: The Complete Reference JAVA 2-Third Edition-Patrick Naughton and Herbert Schildt .(pg:198-205) References