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

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaEdureka!
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaMOHIT AGARWAL
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sqlÑirmal Tatiwal
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance pptNivegeetha
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in javaHitesh Kumar
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaManish Sahu
 

La actualidad más candente (20)

What are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | EdurekaWhat are Abstract Classes in Java | Edureka
What are Abstract Classes in Java | Edureka
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Inheritance
InheritanceInheritance
Inheritance
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Object and class
Object and classObject and class
Object and class
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Abstract class
Abstract classAbstract class
Abstract class
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 

Destacado (7)

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
 
This keyword in java
This keyword in javaThis keyword in java
This keyword in java
 

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
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
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
 

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
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
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
 

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