SlideShare una empresa de Scribd logo
1 de 23
Using super keyword




                      http://improvejava.blogspot.in
                                                       1
Objectives

On completion of this period, you would be able to
learn

• ‘super’ keyword
• Two general uses of super keyword
• Example programs using super keyword




                   http://improvejava.blogspot.in
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



                      http://improvejava.blogspot.in
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
                 http://improvejava.blogspot.in
                                                  4
To Call Super Class Constructors contd..


• Constructors are not inherited, even though they have

 public visibility

• Yet we often want to use the parent's constructor to set

 up the "parent's part" of the object

• The super reference can be used to refer to the parent

 class, and often is used to invoke the parent's constructor


                       http://improvejava.blogspot.in
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




                 http://improvejava.blogspot.in
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

                    http://improvejava.blogspot.in
Example on super class constructor
class Box{
 private double width;
 private double height;
 private double depth;
// construct clone of one object
Box(Box ob) {             // pass object to constructor
 width = ob.width;
 height = ob.height;
 depth = ob.depth;
}
                    http://improvejava.blogspot.in
                                                          8
Example on using super reference


//Constructor used when all dimensions specified
Box(double w, double h, double d) {
    width = w;
    height = h;
    depth = d;
}
// constructor used when no dimensions specified
Box(){

                      http://improvejava.blogspot.in
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 created
Box(double len){
width = height = depth = len;
}


                        http://improvejava.blogspot.in
Example on using super reference Contd..

// Compute and return volume
double volume() {
return width * height * depth;
       }
}
// BoxWeight now fully implements all constructors.
class BoxWeight extends Box {
double weight; // weight box
// construct clone of an object
BoxWeight( BoxWeight ob) { // pass object to constructor
                        http://improvejava.blogspot.in
Example on using super reference Contd..

 super(ob);
 weight = ob.weight;
 }
// constructor when all parameters are specified
BoxWeight ( double w, double h, double d, double m) {
   super(w,h,d);      //call super class constructor
 weight = m;
}
       // default constructor
BoxWeight() {
super();
 weight = -1;
}

                      http://improvejava.blogspot.in
Example on using super reference Contd..



     // constructor used when cube is created
     BoxWeight( double len, double m) {
      super(len);
      weight = m;
       }
     }




                    http://improvejava.blogspot.in
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();
                      http://improvejava.blogspot.in
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();
                       http://improvejava.blogspot.in
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();
  }
}




                       http://improvejava.blogspot.in
Output of program
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

Volume of mybox2 is 24.0
Weight of mybox2 is 0.076

Volume of mybox3 is -1.0
Weight of mybox3 is -1.0

Volume of myclone is 3000.0
Weight of myclone is 34.3

Volume of mycube is 27.0
Weight of mycube is 2.0
                    http://improvejava.blogspot.in
                                                     17
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.member
here 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.

                   http://improvejava.blogspot.in
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
 }
                     http://improvejava.blogspot.in
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);
Subob.show();                  Out put of the program is
   }                           i in superclass : 1
 }                             i in subclass : 2


                          http://improvejava.blogspot.in
Summary
In 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




                     http://improvejava.blogspot.in
Quiz

1. Whenever a sub class needs to refer to its
    immediate super class it can do so by using
    keyword _______
a) super
b) this
c) new
d) All the above



                 http://improvejava.blogspot.in
Frequently Asked Questions
• What are the uses of super keyword ?

• Explain two general methods of super with
  suitable examples.




                 http://improvejava.blogspot.in
                                                  23

Más contenido relacionado

La actualidad más candente

C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in javaHitesh Kumar
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxRkGupta83
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationHoneyChintal
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 

La actualidad más candente (20)

Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java utility classes
Java utility classesJava utility classes
Java utility classes
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Multiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptxMultiple inheritance in java3 (1).pptx
Multiple inheritance in java3 (1).pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Exception handling
Exception handlingException handling
Exception handling
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 

Destacado (6)

Java keywords
Java keywordsJava keywords
Java keywords
 
Selenium ide material (2)
Selenium ide material (2)Selenium ide material (2)
Selenium ide material (2)
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Fundamentals
FundamentalsFundamentals
Fundamentals
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 

Similar a Super keyword.23

6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .happycocoman
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchymyrajendra
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22myrajendra
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oopAHHAAH
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.pptAshwathGupta
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptxsarthakgithub
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceKuntal Bhowmick
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 

Similar a Super keyword.23 (20)

6.INHERITANCE.ppt(MB).ppt .
6.INHERITANCE.ppt(MB).ppt                    .6.INHERITANCE.ppt(MB).ppt                    .
6.INHERITANCE.ppt(MB).ppt .
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
7_-_Inheritance
7_-_Inheritance7_-_Inheritance
7_-_Inheritance
 
Multi level hierarchy
Multi level hierarchyMulti level hierarchy
Multi level hierarchy
 
Inheritance
Inheritance Inheritance
Inheritance
 
Basics of inheritance .22
Basics of inheritance .22Basics of inheritance .22
Basics of inheritance .22
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Mpl 9 oop
Mpl 9 oopMpl 9 oop
Mpl 9 oop
 
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
5. OBJECT ORIENTED PROGRAMMING USING JAVA - INHERITANCE.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptx
 
Inheritance
Inheritance Inheritance
Inheritance
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
Sdtl assignment 03
Sdtl assignment 03Sdtl assignment 03
Sdtl assignment 03
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Más de myrajendra (20)

Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 
Properties
PropertiesProperties
Properties
 

Super keyword.23

  • 1. Using super keyword http://improvejava.blogspot.in 1
  • 2. Objectives On completion of this period, you would be able to learn • ‘super’ keyword • Two general uses of super keyword • Example programs using super keyword http://improvejava.blogspot.in
  • 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 http://improvejava.blogspot.in
  • 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 http://improvejava.blogspot.in 4
  • 5. To Call Super Class Constructors contd.. • Constructors are not inherited, even though they have public visibility • Yet we often want to use the parent's constructor to set up the "parent's part" of the object • The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor http://improvejava.blogspot.in
  • 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 http://improvejava.blogspot.in
  • 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 http://improvejava.blogspot.in
  • 8. Example on super class constructor class Box{ private double width; private double height; private double depth; // construct clone of one object Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth; } http://improvejava.blogspot.in 8
  • 9. Example on using super reference //Constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box(){ http://improvejava.blogspot.in
  • 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 created Box(double len){ width = height = depth = len; } http://improvejava.blogspot.in
  • 11. Example on using super reference Contd.. // Compute and return volume double volume() { return width * height * depth; } } // BoxWeight now fully implements all constructors. class BoxWeight extends Box { double weight; // weight box // construct clone of an object BoxWeight( BoxWeight ob) { // pass object to constructor http://improvejava.blogspot.in
  • 12. Example on using super reference Contd.. super(ob); weight = ob.weight; } // constructor when all parameters are specified BoxWeight ( double w, double h, double d, double m) { super(w,h,d); //call super class constructor weight = m; } // default constructor BoxWeight() { super(); weight = -1; } http://improvejava.blogspot.in
  • 13. Example on using super reference Contd.. // constructor used when cube is created BoxWeight( double len, double m) { super(len); weight = m; } } http://improvejava.blogspot.in
  • 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(); http://improvejava.blogspot.in
  • 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(); http://improvejava.blogspot.in
  • 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(); } } http://improvejava.blogspot.in
  • 17. Output of program Volume of mybox1 is 3000.0 Weight of mybox1 is 34.3 Volume of mybox2 is 24.0 Weight of mybox2 is 0.076 Volume of mybox3 is -1.0 Weight of mybox3 is -1.0 Volume of myclone is 3000.0 Weight of myclone is 34.3 Volume of mycube is 27.0 Weight of mycube is 2.0 http://improvejava.blogspot.in 17
  • 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.member here 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. http://improvejava.blogspot.in
  • 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 } http://improvejava.blogspot.in
  • 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); Subob.show(); Out put of the program is } i in superclass : 1 } i in subclass : 2 http://improvejava.blogspot.in
  • 21. Summary In 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 http://improvejava.blogspot.in
  • 22. Quiz 1. Whenever a sub class needs to refer to its immediate super class it can do so by using keyword _______ a) super b) this c) new d) All the above http://improvejava.blogspot.in
  • 23. Frequently Asked Questions • What are the uses of super keyword ? • Explain two general methods of super with suitable examples. http://improvejava.blogspot.in 23