answersLogoWhite

0

The super variable is not a reference to a subclass. It is a reference to the superclass.

class MyClass {

void printType() {

System.out.println("This is a MyClass");

}

// MySubClass is a subclass of MyClass. Within this class, the super keyword

// refers to MyClass.

static class MySubClass extends MyClass {

void printType() {

// Tell Java we also want to call the printType method of the super class

super.printType();

System.out.println("This is a MySubClass");

}

}

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

Can a super class varialble can reference a subclass object in java?

No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.


Is super keyword is analogous to this keyword in java?

No. The keyword super is used to refer to the parent class instance while the keyword this is used to refer to the current class instance. You need to learn about Inheritance and Object creation using constructors to learn more about these keywords and their use


What class is the super class for ALL Java classes?

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class


How constructor called?

You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.


Why we use super in java?

The keyword super is used to refer to the parent class instance of the current class. Lets say we have a class class A extends B { ... public void getName(){ } ... } Lets assume the parent class B also has a method getName() Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super if we say super.getName() then the parent class instance of the method would be called.

Related Questions

Can a super class varialble can reference a subclass object in java?

No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.


What is object class in java?

object class is a super class for all other class...


Is super keyword is anologous to this keyword in java?

No. The keyword super is used to refer to the parent class instance while the keyword this is used to refer to the current class instance. You need to learn about Inheritance and Object creation using constructors to learn more about these keywords and their use


Is super keyword is analogous to this keyword in java?

No. The keyword super is used to refer to the parent class instance while the keyword this is used to refer to the current class instance. You need to learn about Inheritance and Object creation using constructors to learn more about these keywords and their use


What are the applications of super and this keyword?

The super and this keywords are mainly used in case of inheritance. this - refers to the object of the current class instance super - refers to the object of the instance of the parent class of the current class.


What is this and super keyword in java?

"this" represents current instance of a class whereas to access constructor, member variable or method of the super class "super" keyword is used. For ex. public class A { A() {} hello() {}; } public class B { private int x; B() { super(); // call A's constructor } hello(int x) { super.hello(); //calls A's hello() this.x = x; // this.x represents member variable of the class B whereas "x" represents local variable. } }


What class is the super class for ALL Java classes?

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class


How can you access the super class variables with out any object creation?

Variables of the super class can be accessed using the super keyword. Here is an example. class A { int a; } class B extends A { int a; public B() { super.a = 5; } }


Why Object is Super Most Class In Java?

It's part of the language specification that all objects in Java must inherit from Object. Java defines a strict class hierarchy, and enforcing a "super most class" ensures that this ordering is maintained.


Which object-oriented concept defines the relationship between a subclass and a super class?

Inheritance


How constructor called?

You cannot invoke a constructor explicitly. It will get invoked implicitly when you call the new keyword on the class to create an object of the class. Ex: private ClassExample obj = new ClassExample(); here this new keyword usage on the ClassExample class will invoke the constructor of this class and create an object of that class.


Why we use super in java?

The keyword super is used to refer to the parent class instance of the current class. Lets say we have a class class A extends B { ... public void getName(){ } ... } Lets assume the parent class B also has a method getName() Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super if we say super.getName() then the parent class instance of the method would be called.