This question already has answers here:
Does polymorphism apply on class attributes in Java?
(8 answers)
Closed 4 years ago.
I have a following code
public class Main {
public static void main(String[] args) {
Parent upCasted = new Child();
System.out.println("String: " + upCasted.getString());
System.out.println("Int: " + upCasted.a);
}
}
class Parent {
int a = 1;
public String getString() {
return "Parent";
}
}
class Child extends Parent {
int a = 2;
#Override
public String getString() {
return "Child";
}
}
When I run it I see this in console
String: Child
Int: 1
Why during the upcasting the int value was taken from the Parent and the String value from the Child?
It's because instance fields are not overridden when re-declared in child classes.
Instead, when upCasted.a is evaluated, Java looks at the static/declared type of upCasted to know which class's field to read (not at the runtime class of the object that upCasted points to).
This is why one should not use the same name for fields in superclasses and subclasses.
Related
This question already has answers here:
Purpose of a constructor in Java?
(12 answers)
Closed 2 years ago.
When we can use methods instead of constructor for any operation then what is the use of constructor in java or c++.
//Program of Division using constructor:-
class Hey {
Hey() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main ( String[] args ) {
Hey ob = new Hey();
}
}
//Program of division using method:-
class Hey {
public void disp() {
int i = 10;
System.out.println("Division of 10/2 is " + i/2);
}
}
public class HelloWorld extends Hey {
public static void main( String[] args ) {
Hey ob = new Hey();
ob.disp();
}
}
As, we can see that both will have same output. So, now I am bit confuse that when to use constructor.
Constructor is used to initialize objects in java. Even if you don't provide constructor in your code, java compiler will automatically add a default constructor.
Whereas Methods are used to exhibits functionalities to object. You will have to invoke methods explicitly in your code.
In the example you shared, you are creating object of Hey class Hey ob=new Hey() in order to call its method disp. So if you want to define object in your class, you will use constructors, and if you want to write some functionality of object, you can use Methods.
This question already has answers here:
A superclass method is called instead of the subclass method
(3 answers)
Java inheritance (method overriding and overloading)
(4 answers)
Inheritance and Overloading methods with different argument data types in Java
(2 answers)
Closed 4 years ago.
So in class, we are going over-loading and over-ridding though for some reason my professor could not explain why when we have two classes (a and b; as well as b extends to a) when calling a method it prefers the method in the parent class even though the parameters meet those of the child class. Yet java uses the parent one.
class B extends A {
public int m(int x) {
return 20;
}
}
class A {
public int m(double x) {
return 10;
}
}
public class Tester {
public static void main(String[] args) {
A x = new B();
System.out.println(x.m(5));
}
}
Output: 10
This question already has answers here:
Java: Calling a super method which calls an overridden method
(13 answers)
Closed 5 years ago.
Question: The result of the following code is "5 A" and "10 B". How is it possible that for b.print(), this.num is a reference to a Class A object and this.getClass() is a reference to a Class B Object?
Superclass A
public class A {
private int num;
public A(int num) {
this.num = num;
}
public void print() {
System.out.println(this.num + " " + this.getClass().getName());
}
}
Subclass B
public class B extends A {
public B(int num) {
super(num);
}
}
Main Method
A a = new A(5);
B b = new B(10);
a.print();
b.print();
These are two distinct things.
this.getClass() or more simply getClass() (as this is implied as not specified) will always refer to the actual runtime class that may be the class that uses it or a child of it. If you call it from A, it will return the A class but if you call it from a child of it, it will return this child class.
While this.num refers to a specific instance field declared in the A class. When you code refer to this field, it relies necessarily on the class that declares this specific field.
Whenever you call this in an instance, you are going to be calling the actual instance, and not the "class". For example, in you case, if you override the method print in your class B, like this;
public class B extends A {
public B(int num) {
super(num);
}
public void print() {
System.out.println("PRINTING B");
}
}
Then when you call the print method it will call this one and not the parent one, even if you use this.print() inside any method of the class A
If you really want to explicitly print the A class, then you need to reference it like this:
public void print() {
System.out.println(this.num + " " + A.class.getName());
}
This question already has answers here:
Why doesn't the compiler complain when I try to override a static method?
(9 answers)
Closed 6 years ago.
I'm new at learning Java can anyone explain me the execution flow of the following code? I'm quite confused with the output.
This is the code:
public class MainClass {
public static void main(String[] args) {
car c = new car();
vehicle v = c;
/* I am unable to understand what's happening while printing the values using the objects of the different classes*/
System.out.println("|" + v.getModelName()
+ "|" + c.getModelName()
+ "|" + v.getRegNo() + "|" + c.getRegNo() + "|");
}
}
class vehicle {
public static String getModelName() {
return "Volvo";
}
public long getRegNo() {
return 12345;
}
}
class car extends vehicle {
public static String getModelName() {
return "Toyota";
}
#Override
public long getRegNo() {
return 54321;
}
}
Object creation
You are creating car instance ( new car())
Add new object pointer to variable c
Copy content of variable c to variable vehicle ( which point to car object)
Method call flow
When you are call static function on object it will not apply inheritance rules, so in call to v.getModelName() Java Virtual Machine call method in class vehicle.
But when you are call car() object with vehicle pointer (v variable) getRegNo method of class vehicle will call and also when you are using car pointer (c variable) getRegNo method of class vehicle will call.
edite suggestion form comment:
This ability called "Polymorphism": here you can find good tutorial. "Polymorphism" is definitely as important a concept as "inheritance" and "encapsulation'.
This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 7 years ago.
Code is
public class ctorsandobjs {
private int a;
public int b;
public ctorsandobjs(String arg)
{
System.out.println("I got " + arg);
}
public void add(int a,int b)
{
System.out.println("Addition is " + String.valueOf(a+b));
}
public static void main(String args[])
{
ctorsandobjs c = new ctorsandobjs("You");
c.a = 12;
c.b = 15;
add(c.a,c.b); //compiler shows error here
}
}
I am using Eclipse Luna IDE and JDK 8 ...
can you tell me why compiler is showing error here.....
"Cannot make a static reference to a non static method add(int,int) from the type ctorsandobjs"
I am new to JAVA...
and if possible suggest a solution
add is a non-static method and so you have to invoke it from the object of a class
You have to do:
c.add(c.a, c.b);
You cannot reference non-static members (private int a; public int b) from within a static function.
The add method is not a static method, so you need to call it on an instance of the class ctorsandobjs, for example like this:
c.add(c.a,c.b);