Answers for "ways to use super() java"

1

java super.var

class Superclass
{
   int num = 100;
}
class Subclass extends Superclass
{
   int num = 110;
   void printNumber(){
	/* Note that instead of writing num we are
	 * writing super.num in the print statement
	 * this refers to the num variable of Superclass
	 */
	System.out.println(super.num);
   }
   public static void main(String args[]){
	Subclass obj= new Subclass();
	obj.printNumber();	
   }
}
Posted by: Guest on October-03-2021
9

super keyword in java

Variables and methods of super class can
be overridden in subclass. 
In case of overriding , a subclass
object call its own variables and methods.
Subclass cannot  access the variables and
methods of superclass because the overridden
variables or methods hides the 
methods and variables of super class.
But still java provides a way to access 
super class members even if 
its members are overridden. Super is
used to access superclass variables, methods, constructors.
Super can be used in two forms :
1) First form is for calling super class constructor.
2) Second one is to call super class variables,methods.
Super if present must be the first statement.
Posted by: Guest on January-05-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language