Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Super keyword.23


Published on

  • Be the first to comment

  • Be the first to like this

Super keyword.23

  1. 1. Using super keyword 1
  2. 2. ObjectivesOn completion of this period, you would be able tolearn• ‘super’ keyword• Two general uses of super keyword• Example programs using super keyword
  3. 3. Details About super• Whenever a sub class needs to refer to its immediate super class it can do so by using keyword super• super keyword has two general uses • To call super class constructor • To access a member of super class that is hidden by a member of sub class
  4. 4. To Call Super Class Constructors• A sub class can call a constructor method defined by its super class as follows super(parameter-list);• parameter-list specifies any parameter needed by the constructor in the super class• super() must always be the first statement executed inside a constructor of subclass 4
  5. 5. To Call Super Class Constructors contd..• Constructors are not inherited, even though they have public visibility• Yet we often want to use the parents constructor to set up the "parents part" of the object• The super reference can be used to refer to the parent class, and often is used to invoke the parents constructor
  6. 6. To Call Super Class Constructors contd..• Used to construct the instance variables of both the subclass and the superclass• Uses the keyword super to involve the constructer method of the superclass
  7. 7. To Call Super Class Constructors contd..• The keyword super is used subject to the following conditions• Super may only be used within a subclass constructor method• The call to super class constructer must appear as the first statement within the subclass constructer• The parameters in the super call must match the order and type of the instance variable declared in the super class
  8. 8. Example on super class constructorclass Box{ private double width; private double height; private double depth;// construct clone of one objectBox(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth;} 8
  9. 9. Example on using super reference//Constructor used when all dimensions specifiedBox(double w, double h, double d) { width = w; height = h; depth = d;}// constructor used when no dimensions specifiedBox(){
  10. 10. Example on using super reference Contd.. width = -1; // use -1 to indicate an un initialized box height = -1; depth = -1;}// constructor used when a cube is createdBox(double len){width = height = depth = len;}
  11. 11. Example on using super reference Contd..// Compute and return volumedouble volume() {return width * height * depth; }}// BoxWeight now fully implements all constructors.class BoxWeight extends Box {double weight; // weight box// construct clone of an objectBoxWeight( BoxWeight ob) { // pass object to constructor
  12. 12. Example on using super reference Contd.. super(ob); weight = ob.weight; }// constructor when all parameters are specifiedBoxWeight ( double w, double h, double d, double m) { super(w,h,d); //call super class constructor weight = m;} // default constructorBoxWeight() {super(); weight = -1;}
  13. 13. Example on using super reference Contd.. // constructor used when cube is created BoxWeight( double len, double m) { super(len); weight = m; } }
  14. 14. Example on using super reference Contd.. class DemoSuper {public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); BoxWeight mybox3 = new BoxWeight(); // default BoxWeight mycube = new BoxWeight(3, 2); BoxWeight myclone = new BoxWeight(mybox1);double vol;vol = mybox1.volume();System.out.println( “Volume of mybox1 is” + vol);System.out.println( “Weight of mybox1 is” +mybox1.weight);System.out.println();
  15. 15. Example on using super reference Contd.. vol = mybox2.volume();System.out.println( “Volume of mybox2 is” + vol);System.out.println( “Weight of mybox2 is” +mybox2.weight);System.out.println();vol = mybox3.volume();System.out.println( “Volume of mybox3 is” + vol);System.out.println( “Weight of mybox3 is” +mybox3.weight);System.out.println();vol = myclone.volume();System.out.println( “Volume of myclone is” + vol);System.out.println( “Weight of myclone is” +myclone.weight);System.out.println();
  16. 16. Example on using super reference Contd..vol = mycube.volume();System.out.println( “Volume of mycube is” + vol);System.out.println( “Weight of mycube is” +mycube.weight);System.out.println(); }}
  17. 17. Output of programVolume of mybox1 is 3000.0Weight of mybox1 is 34.3Volume of mybox2 is 24.0Weight of mybox2 is 0.076Volume of mybox3 is -1.0Weight of mybox3 is -1.0Volume of myclone is 3000.0Weight of myclone is 34.3Volume of mycube is 27.0Weight of mycube is 2.0 17
  18. 18. Accessing hidden member of super class• Second form of super class refers to the super class of the sub class in which it is used. super.memberhere the member can be either method or instance variable.This form of super is most applicable to member names of a subclass to access hider members by the same name in the super class.
  19. 19. Example Program// using super to overcome name hiding class A { int i; }// create 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 }
  20. 20. Example Program Contd..void show() {System.out.println(“I in superclass : “ +super.i);System.out.println(“I in subclass : “ +i); } }class Usesuper { public static void main(String args[]) {B subob = new B(1,2);; Out put of the program is } i in superclass : 1 } i in subclass : 2
  21. 21. SummaryIn this class we have discussed • super keyword • Uses of super keyword • To call super class constructor • To access a member of superclass hidden by member of subclass
  22. 22. Quiz1. Whenever a sub class needs to refer to its immediate super class it can do so by using keyword _______a) superb) thisc) newd) All the above
  23. 23. Frequently Asked Questions• What are the uses of super keyword ?• Explain two general methods of super with suitable examples. 23