#Inheritence

1 messages · Page 1 of 1 (latest)

wooden ermine
#
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");
    }
}```
unreal knollBOT
# wooden ermine ```java class HelloWorld { public static void main(String[] args) { ...

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.

unreal knollBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

wooden ermine
#

I am getting an error, due to constructors i think

normal jacinth
#

can u share the error please? 🙂

#

ah nvm

#

i see it

wooden ermine
#

B(float speed){
^
required: int
found: no arguments
reason: actual and formal argument lists differ in length

normal jacinth
#

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?

wooden ermine
#

I see

normal jacinth
#

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

wooden ermine
#

I am just testing out

normal jacinth
#

okay

#

🙂

wooden ermine
#

when I remove both constructors

#

it returns a as a type B

normal jacinth
#

then u get the "default constructor" automatically:

A() {}
...
B() {}
normal jacinth
#

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

wooden ermine
#

now this makes much more sense,

normal jacinth
#

which is B

wooden ermine
#

rather than creating an object called a, I create an object and assign it to a

normal jacinth
#

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

wooden ermine
#

why B b = a; now returns an error?

normal jacinth
#

because java cant be sure that the thing behind a is actually a B

wooden ermine
normal jacinth
#

like, the dog might as well be a cat now, who knows

normal jacinth
#

the cast wont mess with any prints

wooden ermine
normal jacinth
#

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"

wooden ermine
#

I understand thinkowo

#

but when I do this

#

System.out.println(a.getClass());
B b = (B)a;
a.run();

#

output is:

#

class BRuns

normal jacinth
#

its always BRuns

#

cause the instance behind the variable is and will always be B

#

methods are executed on the instances

#

not the variables

wooden ermine
#

but what happened to newline character

normal jacinth
#

a.run() resolves the variable and then asks the instance to execute the method

normal jacinth
wooden ermine
#

isn't output ought to be class B Runs

normal jacinth
#

yes. and it is

#

why do u think its not?

wooden ermine
#

it is class BRuns?

normal jacinth
#

not for me

#

maybe its ur console. share a screenshot please

#

or u edited the code and wrote print(...) instead of println(...)

wooden ermine
#

using online compiler, myb

normal jacinth
#

can u share ur code again please

#

since u likely did modify it from the original

wooden ermine
#
// 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);
    }
}```
normal jacinth
#

it has a newline for me

#

it looks as expected

#

its probably ur online IDE

wooden ermine
#

I see

normal jacinth
#

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

#

🤷‍♂️

wooden ermine
#

Well since it's IDEs fault it doesn't bothers me so much hence kappa

normal jacinth
#

i checked on jshell, cause it was the fastest thing i can have at hand for trying it out quickly

#

im using intellij as ide

wooden ermine
#

same here

#

thanks for the help