class HelloWorld {
public static void main(String[] args) {
A a = new B(4);
System.out.println(a.getClass());
a.run();
}
}
class A{
A(int speed){
}
public void run(){
System.out.println("Runs slow");
}
}
class B extends A{
B(float speed){
}
public void run(){
System.out.println("Runs");
}
}```
#Inheritence
1 messages · Page 1 of 1 (latest)
Detected code, here are some useful tools:
Formatted code
class HelloWorld {
public static void main(String[] args) {
A a = new B(4);
System.out.println(a.getClass());
a.run();
}
}
class A {
A(int speed) {
}
public void run() {
System.out.println("Runs slow");
}
}
class B extends A {
B(float speed) {
}
public void run() {
System.out.println("Runs");
}
}
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
I am getting an error, due to constructors i think
B(float speed){
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length
u have to understand that when u want to create a Dog, the first thing java has to do is creating its Animal "base"
so when u write new Dog(), java first internally has to do sth like new Animal
and then it can put the dog "on top" of that
so far so good?
I see
now, u specified that ur A requires a parameter to be constructed
A (int speed){ ... }
so when java now wants to create ur B, its like "dude, i have to construct A first, but i have no idea what to give it for speed"
hence the error
it helps to understand that when u write a constructor, java always implicitly has to invoke the super-constructor
i.e. ur code:
B (float speed) {
}
is actually:
B (float speed) {
super();
}
java implicitly adds this
super() means sth similar to new A()
but that call is invalid, as its missing the speed
so u have to do:
B (float speed) {
super(123);
}
now it would work and be created with speed 123
but u probably want to use that parameter from B
so
B (float speed) {
super(speed);
}
but now it will complain that float is not an int
not sure if u intended it to be a float though
I am just testing out
then u get the "default constructor" automatically:
A() {}
...
B() {}
thats always the case
u have to differentiate variables from the instances they refer/point to
A a = ... creates a variable that can refer to anything that is A
but its not an instance
the instance is what u created with new
new B()
getClass() gives u the type of the instance
now this makes much more sense,
which is B
rather than creating an object called a, I create an object and assign it to a
exactly
and when u call a method on it, such as animal.makeNoise(), the variable is resolved and the actual instance behind it is asked to please execute makeNoise()
so ur asking the dog/cat to execute the method
not animal
hence they bark or meow
slightly different question
why B b = a; now returns an error?
because java cant be sure that the thing behind a is actually a B
more interestingly, when I did B b = (B)a; between println and method call, it removed the newline character
like, the dog might as well be a cat now, who knows
not sure what u mean by that
the cast wont mess with any prints
but a.getClass() returns B 
yeah but at runtime
the type system is compile-time safe
java cant ensure during compile-time that the thing is a B
it is a B, but it cant know it for sure
after all, u could have messed with a in between
Animal a = new Dog();
a = new Cat(); // messing around
Dog dog = a;
now, u could say that its easy for the compiler to detect that messing-around
in this case it is
but what if
Animal a = new Dog();
a = someMethod(); // messing around
Dog dog = a;
now it would need to check what the method does
and what if
if (currentDay() == Day.MONDAY) {
a = new Dog();
} else {
a = new Cat();
}
now the program is safe only on mondays
but crashes on other days
its getting complex really quick
and the compiler just cant be 100% sure anymore that ur a variable still refers to a B - at compile-time
hence cast exists. which says "yo, just trust me"
I understand 
but when I do this
System.out.println(a.getClass());
B b = (B)a;
a.run();
output is:
class BRuns
its always BRuns
cause the instance behind the variable is and will always be B
methods are executed on the instances
not the variables
but what happened to newline character
a.run() resolves the variable and then asks the instance to execute the method
not sure what u mean by that. there is a newline character
isn't output ought to be class B Runs
it is class BRuns?
not for me
maybe its ur console. share a screenshot please
or u edited the code and wrote print(...) instead of println(...)
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class HelloWorld {
public static void main(String[] args) {
A a = new B(4);
System.out.println(a.getClass());
B b = (B)a;
a.run();
}
}
class A{
A(int speed){
}
public void run(){
System.out.println("Runs slow");
}
}
class B extends A{
B(float speed){
super((int)speed);
}
public void run(){
System.out.println("Runs");
}
}
class C extends B{
C(double speed){
super((float)speed);
}
}```
I see
maybe also a mismatch between \n and \r\n
on that online IDE
java will determine the correct line-terminator to use by checking the OS
using System.lineSeparator()
maybe that online-IDE said its a linux system, i.e. \n should be used
but then the console is maybe windows cmd
which expectes \r\n
so java maybe printed \n but the console only displays line-breaks with \r\n
🤷♂️
Well since it's IDEs fault it doesn't bothers me so much hence 