Instance variable access by using Inheritance concept [duplicate] - java

This question already has answers here:
Overriding member variables in Java ( Variable Hiding)
(13 answers)
Closed 7 years ago.
I am using a Inheritance concept here. I have extended class A(Superclass) into class B(Subclass). And I have created an object of a subclass and by using that object I have called add() method. But it is printing a value 5(Super Class).
Why it didn't take subclass's value which is 10 ?
class A{
int a=5;
public void add(){
System.out.println(a);
}
}
class B extends A{
int a=10;
}
public class InheritExample {
public static void main(String args[]){
B b1=new B();
b1.add();
}
}
Help Appreciated.
Thanks.

There's no overriding of instance variables. Overriding only works on instance methods. Therefore, the method add of class A has access only to the a variable declared in A, even if you call that method for an instance of class B.

Related

Why does the code can instantiate a java interface [duplicate]

This question already has answers here:
Java: Interface with new keyword how is that possible?
(9 answers)
Closed 8 months ago.
I understood that java interface cannot be instantiated. However, the below code looks like it instantiated an interface ? I'm trying to understand the code.
interface I{
int a();
int b();
}
public class C {
public static void main(String [] arg){
I i=(new I(){
public int a(){return 1;}
public int b(){return 2;}
});
assert i.a()==1 && i.b()==2;
}
}
An interface is like an all-abstract class, but that unambiguously supports multiple inheritance. You can instantiate any child of it that implements all the abstract methods.
The code you are asking about is creating an anonymous class that provides an implementation of all the abstract methods. Since the class is anonymous, the type of its instance can is most specifically given as I. Given that it's anonymous, that's also the only aspect of it you care about.

If constructor is not inherited in java then why it parent class constructor is called in subclass by default? [duplicate]

This question already has answers here:
Does a subclass inherit constructors from it super class?
(6 answers)
Java Constructor Inheritance
(10 answers)
Closed 5 years ago.
Lets say we have two classes:
public class A{
public A(){
System.out.println("A");
}
}
and its subclasas
public class B extends A{
public B(){
System.out.println("B");
}
}
The output below would be
A
A
B
public static main(String[] args){
A a = new A();
B b = new B();
}
WHY like this is constructor is not inherited?
SHould not we call super() in subclass constructor to call constructor of parent?
thanks
when you create object of child class then super() call automatic by jvm for non-parameterized constructor.
and if your constructor is parameterized then you have to call by passing parameters like super(parameters).

Why would you allow a static field exist twice? [duplicate]

This question already has answers here:
Overriding interface's variable?
(4 answers)
Closed 5 years ago.
Here we go with my question:
I have an interface with a static variable nr
public interface TestInterface {
public static int nr = 1;
}
And a class which implements that interface and has it's own static variable nr too.
public class TestClass implements TestInterface {
public static int nr = 2;
}
And I can actually do this without an error!
public static void main(String args[]) {
TestClass test = new TestClass();
System.out.println(TestClass.nr);
}`
Question: Why would this be allowed?
As far as I understand static fields should be universal through a class instances but what about the superclass - subclass relation?
It's explicitly allowed by the language.
If the class declares a field with a certain name,
then the declaration of that field is said to hide
any and all accessible declarations of fields with
the same name in superclasses, and
superinterfaces of the class.
http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.3
In your code, both of the static fields are actually defined/meant for separate types i.e., one for TestInterface interface type and other for TestClass class type.
One point you need to understand is that, when it comes to static, there is no overriding, so they will be treated as separate fields.
In superclass - subclass relation the super class's static variables is shadowed by sub class's static variables if they have same name. Because the static variables aren't polymorphic.

why is it allowed to call a static method with a reference to an instance of the class? [duplicate]

This question already has answers here:
Is it possible to override a static method in derived class?
(6 answers)
Closed 7 years ago.
lets assume that i have 2 classes.
ParentClass:
public class ParentClass {
public static void getInstance(){
System.out.println("Parent method");
}
}
ChildClass:
public class ChildClass extends ParentClass {
public static void getInstance(){
System.out.println("child method");
}
public static void main(String args[]){
ParentClass pc=new ChildClass();
pc.getInstance();
}
}
as you notice above both classes has a static method called getInstance() and in java and many other languages if there is an inherited method and you have the same method in the child class the method that get executed is one in the child class.
the question is: why pc.getInstance(); calls the method in the parent class? yeah there is no method overriding for static methods but could anyone please explain more the weird behavior of pc instance and why does it refer to the parent method even tho its pointing on the child class??
and why is it allowed to call a static method with a reference to an instance of the class ?
Thanks
There is no method overriding for static methods. The static type of the instance being used to call the method (ParentClass in your example) determines which method is called.
Besides that, it's bad practice to use an instance reference in order to call a static method. You should use ClassName.methodName() to execute a static method.

Java: Why does my class automatically inherits constructor from superclass? [duplicate]

This question already has answers here:
Inheritance in Java - creating an object of the subclass invokes also the constructor of the superclass. Why exactly?
(15 answers)
Default constructors and inheritance in Java
(11 answers)
order of constructor calls in multilevel inheritance in java [duplicate]
(4 answers)
Closed 8 years ago.
When I try the code on the bottom of this question, the output is:
a a b a b c
So this means that the constructor from B and C call the constructors from their superclasses. But why?
I thought that the superclass' constructor only get's called when used with the super() function like this:
public sportscar(String name, int weight, int topspeed){
super(name, weight);
this.setTopspeed(topspeed);
}
But if it automatically takes over the constructor from the classes they extend from, why would we use the super() function? I know that normal methods extend to there sub-classes automaticaly, but I thought that the constructor was different.
If anyone can clear this out for me, thanks a lot!
Code:
public class App {
public static void main(String[] args) {
new A();
new B();
new C();
}
}
public class A {
public A(){
System.out.println("a ");
}
}
public class B extends A {
public B(){
System.out.println("b ");
}
}
public class C extends B {
public C(){
System.out.println("c ");
}
}
This behaviour is mandated by the JLS (ยง8.8.7. Constructor Body):
If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

Categories

Resources