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.

Ppt on this and super keyword


Published on

ppts are on this and super keyword used in java

Published in: Technology, Health & Medicine

Ppt on this and super keyword

  1. 1. Compiled by:-Tanu Jaswal
  2. 2. Whenever a subclass needs to refer to its immediatesuperclass, it can do so by use of the keyword super.If your method overrides one of its superclasss methods, youcan invoke the overridden method through the use of thekeyword super.You can also use super to refer to a hidden field (althoughhiding fields is discouraged).It has advantage so that you don’t to have to performoperations in the “parentclass” again.Only the immediate “parentclass’s” data and methods can beaccessed
  3. 3. Super has two general forms:-The first calls the superclass constructor.The second is used to access a member of thesuperclass that has been hidden by a member ofa subclass.
  4. 4. public class Superclass{ public void printMethod() { System.out.println("Printed in Superclass."); }}
  5. 5. Here is a subclass, called Subclass, thatoverrides printMethod(): public class Subclass extends Superclass { public void printMethod() // overrides printMethod in Superclass { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }
  6. 6.  Within Subclass, the simplename printMethod() refers to the one declaredin Subclass, which overrides the onein Superclass. So, to refer toprintMethod() inheritedfrom Superclass, Subclass must use a qualifiedname, using super as shown. Compiling andexecuting Subclass prints the following: • Printed in Superclass. • Printed in Subclass.
  7. 7.  The syntax for calling a superclass constructor is super(); --or– super(parameter list);With super(), the superclass no-argument constructor iscalled.With super(parameter list), the superclass constructor witha matching parameter list is called.If a constructor does not explicitly invoke a superclassconstructor, the Java compiler automatically inserts a call tothe no-argument constructor of the superclass.
  8. 8. If the super class does not have a no-argumentconstructor, you will get a compile-timeerror. Object does have such a constructor, so ifObject isthe only superclass, there is no problem.If a subclass constructor invokes a constructor of itssuperclass, either explicitly or implicitly, you might thinkthat there will be a whole chain of constructors called, allthe way back to the constructor of Object. In fact, this isthe case. It is called constructor chaining, and you need tobe aware of it when there is a long line of class descent.
  9. 9. class Box { private double width; private double height; private double depth;Box(Box ob) {// pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; }Box(double w, double h, double d) { width = w; height = h; depth = d; }// constructor used when no dimensions specifiedBox() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box }
  10. 10. Box(double len) { width = height = depth = len; }double volume() { return width * height * depth; } }class BoxWeight extends Box { double weight;BoxWeight(BoxWeight ob) { super(ob); weight = ob.weight; }BoxWeight(double w, double h, double d, double m) { super(w, h, d); weight = m; }
  11. 11. BoxWeight() { super(); weight = -1; }BoxWeight(double len, double m) { super(len); weight = m; } }class DemoSuper {public static void main(String args[]) {BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);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();}}
  12. 12. The second form of super acts somewhat likethis, except that it always refers to the superclass ofthe subclass in which it is used.This usage has the following general form: super.memberHere, member can be either a method or aninstance variable.This second form of super is most applicable tosituations in which member names of a subclass hidemembers by the same name in the superclass.
  13. 13. class A {int i;}class B extends A {int i; // this i hides the i in AB(int a, int b) {super.i = a; // i in Ai = b; // i in B}
  14. 14. 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);;}}
  15. 15. This program displays the following: i in superclass: 1 i in subclass: 2Although the instance variable i in B hides the iin A, super allows access to the I defined in thesuperclass. As you will see, super can also be usedto call methods that are hidden by a subclass.
  16. 16. Sometimes a method will need to refer to theobject that invoked it. To allow this, Javadefines the this keyword.this can be used inside any method to referto the current object. That is, this is always areference to the object on which the methodwas invoked.You can use this anywhere a reference toan object of the current class’ type ispermitted.
  17. 17. THIS KEYWORDThe keyword this is useful when you need torefer to instance of the class from its method.The keyword helps us to avoid name conflicts.As we can see in the program that we havedeclare the name of instance variable and localvariables same.Now to avoid the confliction between them weuse this keyword
  18. 18. class Rectangle{ int length,breadth; void show(int length,int breadth){ this.length=length; this.breadth=breadth; } int calculate(){ return(length*breadth); }}public class UseOfThisOperator{ public static void main(String[] args){ Rectangle rectangle=new Rectangle();,6); int area = rectangle.calculate(); System.out.println("The area of a Rectangle is : " + area); }}
  19. 19. The area of a Rectangle is : 30.Inthe example this.length and this.breadth refersto the instance variable length and breadth whilelength and breadth refers to the arguments passed inthe method.
  20. 20. THANK YOU