This question already has answers here:
Are static variables inherited
(4 answers)
Closed 4 years ago.
I have a code in which I expect the output to be different from the actual output.. As static variables are reference based, I expect the output to be "superclass" but what I am getting is "subclass".. Code:
class TestClass {
public static void main(String args[] ) throws Exception {
A b = new B(); // Since the reference is A, "superclass" should be the output
b.test();
}
}
abstract class A{
static String a = "superclass";
abstract void test();
}
class B extends A{
static String a = "subclass";
void test(){
System.out.println(a); // Output subclass
}
}
Please tell me where am I wrong..
Static variables are not inherited in java. You varibale static String a is static which associates it to a class. Java inheritance doesn't work with static variables.
If you absolutely want the superclass variable you could use:
System.out.println(super.a);
Here is the inheritance what you probably wish to see:
abstract class A {
String a = "superclass";
abstract void test();
}
class B extends A {
void test() {
System.out.println(a); // Output superclass
}
}
I remove the static identifier and removed the subclass's implementation of variable a. If you run this you'll get superclass as output.
A b = new B();
First off, static variables are not inherited in Java. This means that when you create your object as a new B(), even though that class extends class A, it won't keep the definition of your String a.
static String a = "subclass";
Secondly, even if that were the case, you're immediately overriding the value of String a at the beginning of this class B. You specifically set it to be "subclass" just before printing its value, so of course you've overriden the original value with this new one.
Finally, it would be a wonderful idea to try and name things with a little more variety. There being a class A and a String a doesn't help much for readability, for you or the people answering your question.
public abstract class A
{ static int a = 1; }
public class B extends A
{ static int a = 2; public A() { } }
public static void main(String argv[])
{
A var1 = new B();
System.out.println(var1.a)
// Re-cast the variable to type "class B"
// This is the SAME VARIABLE
// It is occupying the SAME MEMORY SPACE
// This println will produce the same output...
System.out.println(((B) var1).a)
}
Related
When accessing a function from another class in c++,
we can write: classA::fct();
Is there an equivalent operator in java?
If not, how can we access a function from another class in java?
Well the ::-operator (Scope Resolution Operator) in C++ allows you to resolve ambiguous calls/references to identifiers. However, in java there are no independent functions as all functions are actually methods (members) of a class. As such there are no need for this operator, have a look here for differences between Java and C++ classes.
I am guessing you are attempting to access a member (possibly static) of a class, in which case you'd use the .-operator as exemplified in Mwesigye's answer or as follows:
public class AB {
public static void main(String[] args) {
B myB = new B();
myB.printA();
}
}
public class A {
public static int getInt() {
return 4;
}
}
public class B {
public void printA() {
System.out.println(A.getInt()); // output: 4
}
}
Here the .-operator is used to access printA() from the instantiated object myB (instantiated from class B). It is also used to access the static method getInt() whose implementation is tied to class A rather than any object of A. More info can be found here.
Take an example of a Class Student with methods what you call functions in c++
eg.
class Student{
//a non static method
public void getFees(){
//your logic
}
public static void deleteSubject(){
// your logic
}
}
class Club{
//create a new instance of student class
Student student = new Student();
public void printData(){
//access a non static method
student.getFees();
//accessing a static method
new Student().deleteSubject();
}
}
Hope this will help.
I'm new to java. Recently I saw some code which was similiar to this:
class A {
protected int myInt;
public static void main(String[] args) {
B b = new B();
b.myFunction();
}
}
class B extends A {
public void myFunction() {
this.myInt = 10;
}
}
As far as I know, when creating a subclass instance, an instance of its parent is created as well. All protected and public members of base class are accessible from the subclass.
If I override myInt there will be a difference between this.myInt to super.myInt because each class will have its own myInt (B will have access to both).
So, my question is: if I don't override myInt, which form is preferable, this.myInt or super.myInt?
You only need to use this or super when need to specify which scope are you using/referring to. In your case, I'll prefer to omit the this to simplify the readability.
super is used to represents the current instante of a parent class while this is used to represents the current class. You only need to used this or super if some variable or method overlaps (Have the same name) with one in a wide scope.
eg. If you have define a method parameter with the same name as class attribute, you need to use this to indicate that you are using the class attribute and not the method parameter.
public class A {
public int myInt = 1;
public static void main(String[] args) {
B b = new B();
b.myFunction(3);
}
}
class B extends A {
public int myInt = 2;
public void myFunction(int myInt){
System.out.println(myInt); // The parameter
System.out.println(this.myInt); // myInt from the current class (B)
System.out.println(super.myInt); // myInt from the parent class (A)
}
}
This example will print:
3
2
1
If you don't have this kind of collission, the use of this is optional:
public void myFunction2(){
System.out.println(myInt); // Both refers to the same
System.out.println(this.myInt); // variable myInt from class B
}
It's a matter of taste and the project's standards/guidelines more than anything else.
Personally, I wouldn't use either, and would just write myInt = 10.
Only one instance is created. If you instantiate a derived object, the parents constructor is called, but only one object is created. Also, the term this is more so used when there are different variables with the same name being referenced in a class.
For example a simple constructor:
class SupClass{
public int a = 1;
int incA(){
return ++a;
}
}
class MyClass extends SupClass {
public int a = 10;
public int b = 20;
MyClass() {};
MyClass(int a, int b){
this.a = a;
this.b = b;
}
int incA(){
return ++a;
}
public static void main(String args[])
{
SupClass d = new MyClass();
System.out.println(d.a); //1, members known of type SupClass at compile-time,
System.out.println(d.incA()); //11, methods are virtual, decided at run-time
}
}
Only use the super method when you want to explicitly use the value that is in the super class. To answer your question, only methods can be overwritten, member variables can not.
When a static method is called from a reference variable of type A, Java will find yourself where that method?
a) Class which objects are shown to belong
b) Class A
c) Starting from the class that the object is shown to belong, if not see, then look at the superclass.
d) sublayer of A
public class A {
public static void get(){
System.out.println();
}
}
public class B extends A {
public static void get(){
System.out.println("this is Get() method ");
}
}
public static void main(String args[]){
A a=new A();
A b=new B();
a.get();
b.get();
}
In this case the program will print two empty lines because both calls are directed to the static method of class A.
This is because static calls are evaluated not at runtime but a compile time and you can't use override functionality with them.
So in both cases the visible class is "A" thus the static method get() of A will be invoked.
interface abs{
int a=10;// by default final static
void callme();
}
class B implements abs{
int a =11;// reinitializing
void call()
{
System.out.println("No problem"+a);
}
public void callme()
{
System.out.println("Call me"+a);
}
}
class main{
public static void main(String args[]){
B m=new B();
m.call();
m.callme();
}
}
In Herbert Schildt book, I have read that the interface variables are default Final and static. Which implicitly means that it will act like a constant. but when I am assigning 11 in the variable a in my above mentioned code, it is not giving any error.
o/p
No problem11
Call me11
You're not re-initializing the variable, but you're hiding it.
In order to access abs's a member, you have to refer it with the interface name. Something like:
System.out.println("No problem" + abs.a);
Because you have declared variable again
int a =11;
so see the error you want
do this in your class
a=11;
in your case the a is an entire new variable belonging to class B
You are not modifying the value of abs.a when you initialize a in class B. The field B.a simply hides the inherited a from the interface. You can still get to abs.a from code in class B by using the fully qualified name abs.a.
No, You can't reinitialize Interface variable. The variable in your Implementation Class is not interface variable A, but B class' instance variable. If you want to access Interface variable you reference it as abs.A
What you are actually doing is hiding the variable in interface by declaring a variable with same name in class B. You can verify it by using this code:
public interface Abs {
int a=10;// by default final static
void callme();
default void printA() {
System.out.println("A in interface: " + a);
}
}
public class B implements Abs {
int a =11;// reinitializing
void call()
{
System.out.println("A in class: "+a);
}
public void callme()
{
printA();
}
}
Then we have:
public static void main(String[] args){
B m=new B();
m.call();
m.callme();
}
It prints:
A in class: 11
A in interface: 10
This question already has answers here:
Different behavior of overriding methods and fields
(2 answers)
Closed 8 years ago.
Say i have this code
public class A {
String name = "a";
public void one(){
System.out.println(name);
}
public void two(){
System.out.println(name);
}
public static void main(String[] args){
A a = new A();
B b = new B();
b.one();
}
}
class B extends A{
String name="b";
public void two(){
System.out.println(name);
}
}
I cant figure out why b.one() always produces "a". From what i know of inheritance, B will see that it doesnt have its own copy of one() so it will super.one(). super.one() will print out the value of the variable name(a) in that class. However wont B see that it too has a name variable which has value "b" so wont it go to that? Im confused cause i know this logic works for method calls. Can anyone clarify this?
Your own question partially answers itself. The moment you do super.one() you are no longer in 'B'. Thus, why would you expect anything else but "a" from the name. The name that you are referencing after you call the super class is the name of A. You could change the value of name in the subclass if name was public or protected and you did not re-declare it.
public class A
{
protected String name;
public void printName()
{
System.out.println(name);
}
}
public class B extends A
{
public B()
{
name = "b";
}
}
public class Main
{
public static void main(String [] args)
{
B b = new B();
b.printName();
}
}
The above code will print "B".