The Artima Developer Community
Sponsored Link

Java Buzz Forum
Super keyword interview questions java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Super keyword interview questions java Posted: Mar 2, 2016 10:07 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Super keyword interview questions java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Super Keyword:


  • The functionality of super keyword is only to point the immediate super class object of the current object.
  • super keyword is applicable only in the non static methods and super keyword not applicable in the static methods.
  • super keyword used to access the members of the super class object.
  • super.member;
  • It is used to store super class non static members memory reference through current sub class object for separating super class members from subclass members.
  • We can call super class constructor in sub class using super() call.
  • We can access super class methods and variables in sub class using super.variable_name, super.method();

Uses of super : 

 
1. By using super keyword we can access super class variables in sub class.

  • Using super keyword we can access super class variables from sub class.
  • super.variable_name.
  1. package com.superkeywordinjava;
  2.  public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. }

  1. package com.superkeywordinjava;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7. super.a=10;
  8. super.b=20;
  9.  
  10.  
  11. }
  12.  
  13. public static void main (String args[]) {
  14.  Subdemo obj= new Subdemo();
  15.  
  16. obj.a=1;
  17. obj.b=2;
  18.  
  19. obj.disply();
  20.  

  21.  
  22. }
  23. }

Output:

  1. 1
  2. 10
  3. 20

2. By using super keyword we can access super class methods in sub class.
  • Using super keyword we can call super class methods from sub class.
  • super.method();.

  1. package com.instanceofjavaforus;
  2.  public Class SuperDemo{ 
  3. int a,b;
  4.  
  5. public void show() {

  6. System.out.println(a);
  7. System.out.println(b);
  8.  
  9. }
  10. }

  1. package com.instanceofjavaforus;
  2. public Class Subdemo extends SuperDemo{ 
  3. int a,b;
  4. void disply(){

  5. System.out.println(a);
  6. System.out.println(b);
  7.  
  8. super.a=10;
  9. super.b=20;
  10.  
  11.  super.show();
  12.  
  13. }
  14.  
  15. public static void main (String args[]) {
  16.  
  17.  Subdemo obj= new Subdemo();
  18.  
  19. obj.a=1;
  20. obj.b=2;
  21.  
  22. obj.disply();

  23.  
  24. }
  25. }

Output:

  1. 1
  2. 10
  3. 20
 
3. We can call super class constructor from class constructor:
  •  By using super keyword we can able to call super class constructor from sub class constructor.
  • Using super(); 
  • For  the super(); call must be first statement in sub class constructor.  

  1. package com.superinterviewprograms;
  2. public Class SuperDemo{ 
  3.  
  4. int a,b;
  5.  
  6. SuperDemo(int x, int y){
  7.  a=x;
  8. b=y
  9. System.out.println("Super class constructor called ");
  10.  
  11.  
  12. }

  1. package com.superinterviewprograms;
  2.  
  3. public Class Subdemo extends SuperDemo{ 
  4.  
  5. int a,b;
  6.  
  7. SubDemo(int x, int y){
  8. super(10,20);
  9.  a=x;
  10. b=y
  11. System.out.println("Sub class constructor called ");
  12. }
  13.  
  14. public static void main (String args[]) {
  15.  Subdemo obj= new Subdemo(1,2);

  16.  
  17. }
  18. }

Output:
  1. Super class constructor called
  2. Sub class constructor called 

key points:

  • Super(); call must be first statement inside constructor.
  • By default  in every class constructor JVM adds super(); call in side the constructor as first statement which calls default constructor of super class, if we are not not calling it.
  • If we want to call explicitly we can call at that time default call wont be there.

4.What will happen if  we are calling super() in constructor but our class does not extending any class?

  •  if our class not extending any class Yes still we can use super(); call in our class 
  • Because in java every class will extend Object class by default this will be added by JVM.
  • But make sure we are using only super(); default call we can not place parameterized super call because Object class does not have any parameterized constructor.

  1. package com.superinterviewprograms;
  2.  
  3. public Class Sample{ 
  4.  
  5. Sample(){
  6. super();
  7. System.out.println("Sample class constructor called "); 
  8.  
  9. }
  10.  
  11. public static void main (String args[]) {
  12.  
  13.  Sample obj= new Sample();
  14.  
  15. }
  16. }

Output:

  1. Sample class constructor called


5.What if there is a chain of extended classes and 'super' keyword is used


  1. package com.superinterviewprograms;
  2. public Class A{ 
  3.  
  4. A(){
  5.  System.out.println("A class constructor called ");
  6.  
  7.  
  8. }

  1. package com.superinterviewprograms;
  2. public Class B extends A{ 
  3.  
  4. B(){
  5.  
  6.  System.out.println("B class constructor called ");
  7.  
  8.  
  9.  
  10. }

  1. package com.superinterviewprograms;
  2. public Class C extends B{ 
  3.  
  4. C(){
  5.  System.out.println("C class constructor called ");
  6.  
  7.  
  8. public static void main (String args[]) {
  9.  C obj= new C();

  10.  
  11. }
  12. }

Output:

  1. A class constructor called
  2. B class constructor called
  3. C class constructor called

6. Can we call super class methods from static methods of sub class?

  • No we can not use super in static methods of sub class Because super belongs to object level so we can not use super in static methods. 
  • If we try to use in sub class static methods compile time error will come.

super keyword in java interview example program frehser

Read: Super keyword interview questions java

Topic: DIY Annotations Previous Topic   Next Topic Topic: Spring Data Couchbase Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use