Variable in overridden method does not existed in the parent class [duplicate] - java

This question already has answers here:
Java order of Initialization and Instantiation
(2 answers)
What's wrong with overridable method calls in constructors?
(8 answers)
Closed 2 years ago.
class Main {
public static void main(String[] args) {
new B();
}
public static class A {
String f1 = "300";
public A(){
init();
}
protected void init(){}
}
public static class B extends A {
private String f1 = "3";
public B(){
super();
init();
}
protected void init(){
System.out.println(f1);
}
}
}
Output:
null
3
Can anyone explain to me the first line of the output? If I delete the line String f1 = "300";, it still has the same output also.
I have tried that if I declare some variables in B and invoke it in init() and all of it will be null in the constructor of A. In other words, my question is I want to know the reason for those variables being null at that point.

Related

Calling a method defined within an interface implementation from another class in java [duplicate]

This question already has answers here:
What is the difference between a variable, object, and reference? [duplicate]
(5 answers)
why can't I call a subclass method using a reference of a parent type that refers to an instance of a sub-type?
(6 answers)
Closed 3 years ago.
I was wondering if it's possible to define a method within the implementation of an interface class and call that method from another class?
for example:
public interface MyInterface
{
void method1();
}
public class MyClass1 implements MyInterface
{
public void method1()
{
System.out.println("This is already defined in MyInterface")
}
public void method2()
{
System.out.println("This is not already defined in MyInterface")
}
}
public class MyClass2
{
public static void main(String[] args) {
{
MyInterface interface = new MyClass1()
interface.method2
}
}

Should static method Override in java? [duplicate]

This question already has answers here:
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 6 years ago.
Is this method type hiding? How static method gets area in memory?
public class Demo {
public static final void main(String args[]) {
try{
A a = new B();
a.display();
}catch(Exception e){
e.printStackTrace();
}
}
}
class A{
static void display(){
System.out.println("A");
}
}
class B extends A{
static void display(){
System.out.println("B");
}
}
Static methods dont belong to a instance but to a class, overriding some static method makes no sense and java will not let you to do that

Call subclass's static method from parent class's non-static method [duplicate]

This question already has answers here:
Are static methods inherited in Java?
(15 answers)
Why doesn't Java allow overriding of static methods?
(22 answers)
Closed 6 years ago.
Suppose I have the following simple code:
class A {
protected int someMethod() {
return staticMethod();
}
private static int staticMethod() {
return 10;
}
}
class B extends A {
protected int someMethod() {
return super.someMethod();
}
public static int staticMethod() {
return 20;
}
}
class TestX {
private void main() {
B b = new B();
int finalValue =b.someMethod();
}
}
In the above code, when b.someMethod() is executed, it ends up calling A's version of staticMethod(). But I thought static methods are called based on the type of reference of the main object. Since the object on which someMethod() is called is of type B, shouldn't B's version of staticMethod() be called even when A's someMethod() is called (because of the line return super.someMethod();) ?
Given the above code, is there a way I can make B's staticMethod() get executed so that 20 is returned instead of 10 ?
I found a similar question but the idea suggested in it isn't relevant to me I think:
Calling subclass's static method from parent class

Java:Overriding a static function [duplicate]

This question already has answers here:
Static methods and their overriding
(5 answers)
Closed 8 years ago.
What should be printed when we execute this code?
I picked the question from here where the answer is provided but I kinda believe is wrong. First the call to a static function has to be done in static way, second when we override a static function the previous one is no longer accessible ( no new memory is assigned)
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public static void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
static method belongs to class itself and overriding works for instance methods only. If you define the same method again in sub-class then it's called re-defining hiding of the static method.
static method looks for class to invoke it and that can be verified by below same code:
Base b = null;
b.show(); // Base::show() called
It doesn't make any sense to call static method via object.

java inheritance dealing with fields [duplicate]

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".

Categories

Resources