[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Can someone explain why this method prints 2,2,2

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 86
Thread images: 6

Makes no fucking sense. Also what the fuck is a "virtual" method of covariance?
>>
>>58860358
If you can read the definition of a virtual method and understand it, you'll understand why the method prints 2 when called.
>>
use a gentoo method instead
>>
the m() in class C overrides the m() functions of A and B
>>
>>58860358
It uses the method defined by the actual type of object, even if reference is covariant.
>>
>>58860358
c_obj is a instance of C
If you refer to it as a class of type B, which it is because C extends B, it is still a instance of C.
>>
>>58860378
professor never defines it, he just introduces the term in that fucking slide. what is a virtual method
>>
File: 1.png (196KB, 1522x1131px) Image search: [Google]
1.png
196KB, 1522x1131px
>>58860415
>>58860472

What is the difference between OP pic and this? I don't see whats going on or the differences.
>>
>>58860358
bump
>>
>>58860564
The difference is first pic is dynamically dispatched virtual methods and second pic is not. In the first pic, you can see that C's m method gets called each time despite the type of the object not being C. In the second pic, the m for each type gets called.
>>
>>58860358
>Java
ABSOLUTELY DISGUSTING
>>
>>58860779
can you put that in english? I dont even know what a virtual method is because he doesn't fucking define anything
>>
>>58860779
please break it down for me because i literally see NOTHING different from pic 1 to pic 2. he is a shit lecturer and i am totally confused
>>
>>58860813
seems like almost every university uses java heavily
>>
>>58860831
>>58860853
That the two pictures have the same code is the point. The first pic shows different results than the second because of virtual methods. The difference is how the code is being run. Stop and think, how can this code be interpreted that would result in 2,2,2 and how can it be interpreted that would result in 2,1,0?
>>
>>58860890
can you fucking define what a virtual method is? the only thing i could think of is it something made each method refer to c_obj.m call for each b_obj.m and a_obj.m call (in other words c,obj.m overrides the other method call) but i dont fucking know how this would happen or work
>>
>>58860853

The first slide is in regards to virtual methods and the second is not. The slides suck because normally you'd indicate "virtual" in the method signature.
>>
>>58860938
You're getting there, where did a_obj and b_obj come from?
>>
>>58860952
can you define what a virtual method is? because the term is being thrown around with no definition. it could be called hockey stick for all i know, i still have no idea what it is.

>>58860956
a_obj comes from class A and b_obj comes from class B and C extends both B and A

so B,A are parent classes of C and C inherents A,B's methods.
>>
>>58860985
You can figure out the definition. I mean in the code, how did you get instances of these classes?
>>
>>58860985

Sorry, I fucked up. Java methods are virtual by default, so disregard what I said about "virtual" in the signature, that was something I should have remembered.
>>
>>58861001

i honestly dont think i can figure the definition out, can you just explain this? im about to say fuck it and just give up. i tried for hours and got no where
>>
File: th6BO09QMG.jpg (10KB, 282x300px) Image search: [Google]
th6BO09QMG.jpg
10KB, 282x300px
>>58860358
Don't the methods need to be static to do casting like that?
>>
>>58860358

Dude I don't know how to program and this shit is obvious, you're a retard. Basically C is 2, therefore c_obj is 2 as well, and since b_obj and a_obj are c_obj then they have the power of C in them, making them all = 2.
>>
File: 1476627566857.jpg (66KB, 655x527px) Image search: [Google]
1476627566857.jpg
66KB, 655x527px
>>58861048
wait nvm
>>
>>58861050
Slide >>58860564
is different so explain the difference

im not a retard this is an intro programming class and i never seen this shit

the code is the EXACT FUCKING SAME

so what is the difference
>>
Ok, OP's confusion comes from the fact that the professor was too retarded to define the methods correctly. The professor indicates via hints (slide title, description at bottom) that the methods in one are virtual and the methods in the other are non-virtual, when in reality he should've defined the method signatures correctly.

From StackOverflow:

"All functions in Java are virtual by default.

You have to go out of your way to write non-virtual functions by adding the "final" keyword.

This is the opposite of the C++/C# default. Class functions are non-virtual by default; you make them so by adding the "virtual" modifier."
>>
>>58861050
Also loosk like C extends B, A

so C is-a B and C is-a A but B, A is not a C so doesnt make sense your explanation
>>
>>58861025
Don't give up, you are very close. I'm not going to tell you but I'll try to help you figure it out for yourself.
c_obj is set to a newly instantiated object of type C.
Then the other two are set up as casted pointers to that same object. See if that gets you closer to figuring out why the first prints all 2 and second prints different things.

>>58861048
Don't let the fact that the code looks a lot like java fool you, first pic is a language that looks like java and has does virtual method dispatch and second is a similar language that only differs in that it doesn't do virtual method dispatch.
>>
I've only taken a few CS classes, but from some googling and thinking I think this is right?

Per Wikipedia:

In object-oriented programming, when a derived class inherits from a base class, an object of the derived class may be referred to via a pointer or reference of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound either 'early' (by the compiler), according to the declared type of the pointer or reference, or 'late' (i.e., by the runtime system of the language), according to the actual type of the object referred to.

So you're instantiating b_obj as a copy of c_obj cast as a B, but virtual methods mean that the derived class' method is called, not the base class. This is what the slide means when it says "the actual type of object", since it's actually a C being cast to B (or A, in the case of the other one).
>>
>>58861082

It doesn't matter what they extend because if they both have a power of the same kind, the native one makes the extended one null. I don't know what words you programmers use but you're probably too stupid to understand anyway.
>>
>>58861099
I dont fucking get it, maybe you see the connection because you already learned this. professor didn't explain shit and im just pissed off no one is helping me understand this.
>>
>>58861136

See

>>58861072

I suspect you're looking for differences in the actual code, when there are none. As another poster pointed out, while this appears to be Java, it's really only some lookalike for the purpose of the example. You know the functions/methods are virtual or non-virtual respectively based on slide title.
>>
Does it help if I say the reason the two pics give different output is because the interpreter the code is being run in works differently?

The difference is that one of them interprets all methods as virtual, the other doesn't.

What are virtual methods?

>>58861108
Wikipedia is pretty clear on this I think. The code can be called such that the method is bound to the declared type, or the actual type. It's virtual if it's the actual type.

-----------------

Also, an aside. The reason other people aren't just giving you "the answer" is that people learn WAY better when they work out the solution with help than when they're given the solution in its entirety.
>>
>>58861025
>virtual method
You can redefine them
>non-virtual
you can't redefine them
It's retarded to see non-virtual methods without the final keyword.
Messing with the vm is meaningless for a beginner, your teacher is a retard or clearly don't give a shit

Don't bother with complex defintions or explanations you can see all over the places.
Just remember what I just said until you know more about polymorphism in java
>>
>>58861108
all i see is two slides with the same code and one acts different than the other and no one explains why. it's infuriating you are trying to give me bread crumbles when i am obviously very stuck and can't move forward

>>58861178
i dont know what a virtual function is by looking at those slides and i dont understand his comment.

ypure completely unhelpful to me.

IF you could say

DEFINITION:

VIRTUAL FUNCTION IS DEFINED AS....

THIS MEANS,

CODE ONE DOES......

AS OPPOSED TO CODE 2 WHICH DOES...

I'd get it.


You aren;t helping a bit dude. I told you everything I know

THIS IS ALL I SEEL

SLIDE 1: CODE, OUTPUT

SLIDE 2: SAME FUCKING CODE, DIFFERENT OUTPUT

Doesn't make any SENSE WHY
>>
>>58861136
Do you know what
extends
does? How it works?
>>
Fuck this thread and fuck you guys

I give up

Dont bother replyimg you guys are fucking assholes
>>
>>58861216

I literally just explained why the code isn't different and I was sympathetic towardds your frustration, but now you must be trolling or you're a lost cause since you can't fucking read.
>>
>>58861067
virtual pic is probably lacking the virtual keyword
>>
I think OP's real problem is not that he doesn't understand this concept in programming, but that his reading comprehension is very poor.

????

Anyways good job everyone if OP wasn't retarded he would have learned a lot.
>>
>>58861248
Is your comment >>58861206? If so, i didn't see it until now.

If a virtual method means you can override another method then where is it defined? There is no method overloading i see that makes b and a object become c object

the way the code works i might as well copy paste the side 10 times and have each output be

333
444
555
666
777
888
etc without any explanation because thats what i see
>>
>>58861280
it's easy for you to read these comments and say "yes if op was smart he;d understand" when you already know the material.

but instead of calling the person stupid for not understanding try explaining and answering my questions because literally there is no difference between pic 2 and pic 1 and no one explained why
>>
>>58861298
I didn't already know the material, I taught it to myself so I could help the OP.

Took like 5 minutes.

>there is no difference between pic 2 and pic 1 and no one explained why

I made a post specifically addressing this actually.
>>
>>58861319
Where? Because if it's an explanation I read it didn't make sense.


Your comment

>>58861025
>virtual method
You can redefine them
>non-virtual
you can't redefine them
It's retarded to see non-virtual methods without the final keyword.
Messing with the vm is meaningless for a beginner, your teacher is a retard or clearly don't give a shit

Don't bother with complex defintions or explanations you can see all over the places.
Just remember what I just said until you know more about polymorphism in java

doesnt help me see why pic 2 and pic 1 are different
>>
>>58861099

I read this
>c_obj is set to a newly instantiated object of type C.
Then the other two are set up as casted pointers to that same object. See if that gets you closer to figuring out why the first prints all 2 and second prints different things.

Many times. It gets me no closer.

The code from pic 2 is no different than pic 1. How is your comment suppose to help?
>>
>>58861339
that's not my explanation.

when you have some class, B, which extends another class, A.

and B has a method which overrides a method of A.

you could instantiate an instance of A and then cast it to B. let's call this Q.

now when you call the method of Q, which method should be called? should you call the method defined by the type you cast to (which is B) or the method that the actual type of the object defines (A)?

that decision, which method to call, is what you're defining when you declare something as a virtual method.

************ "but where in the code does it define a virtual method" ************

nowhere.

it's just given as a fact in the example that these methods are virtual or not virtual.

normally you would know how your language works, so you can know if your language defaults to virtual or not. and what keyword is used to distinguish this, but for the sake of examples your prof left that out.
>>
>>58861409
oh woops I mixed up A and B here.

sorry, should be:

when you have some class, B, which extends another class, A.

and B has a method which overrides a method of A.

you could instantiate an instance of B and then cast it to A. let's call this Q.

now when you call the method of Q, which method should be called? should you call the method defined by the type you cast to (which is A) or the method that the actual type of the object defines (B)?
>>
>>58861409
In this example then C is casted to B and A. And the virtual method calls C instead of B and A and hence its prints 2 2 2 for each method call.

Is this right?

If so, then would the virtual method be defined somewhere in code in an actual program (ignoring my professors shit slides)?
>>
File: 1486602568729.png (183KB, 1473x1070px) Image search: [Google]
1486602568729.png
183KB, 1473x1070px
>>58861072
>>58861178
>>58861248

Since OP hasn't bothered to comprehend a single one of the posts I made, I made something maybe fuck nuts can understand.
>>
>>58861449
Oops just saw your >>58861449

if so then does that mean C is overriding B and A methods call? If so how, because C is a B and C is an A but not the reverse

and where is the virtual function defined in code
>>
Does Java not force you to declare virtual methods in the signature? That seems ambiguous as shit
>>
File: 1486603661512.png (174KB, 1522x1131px) Image search: [Google]
1486603661512.png
174KB, 1522x1131px
>>
>>58861449
In Java all methods are assumed to be virtual, if they're not then you use the final keyword to say so.

It's different in other languages.

So if your prof had done it like that, then you'd see "final" in the method declarations in the slide with non-virtual methods, this one >>58860564
>>
>>58861216
>all i see is two slides with the same code and one acts different than the other and no one explains why.

It's kind of unclear, but whatever do the exercises.

That said the basic idea is that if you look in the box on the bottom left, notice that new is only used once. Remember, also, that every method has two components, its name and its code.

When you extend an object, you create a new object that is essentially a superset of the original object, that also means inheriting that original object's methods. A VIRTUAL method is one that can be OVERRIDDEN whenever the class gets extended. This means that the extending class has a function with the exact name, but different code attached to it. A NON-VIRTUAL method is a method that once it is defined, cannot have its code redefined.

Now, every time you call new, you are taking a classand instantiating it in an object. When you create an object, in any non-meme language the first thing you do is run that object's [spoiler]constructor[/spoiler]; furthermore, when you create an object of a class that [spoiler]extends[/spoiler] another class, that simply means that in the extending class's [spoiler]constructor[/spoiler], the first thing you do is create an instance of the extended class and build off that.

Now, In Solution 2 the slide is saying that these methods are
virtual
, so that when C is created, and in effect A and B's constructors are run, there is a function m that gets initialized THREE(3) times and because it is
virtual
, the function definition simply gets overwritten, as if the name of the function gets defined once and the code it points to gets defined three times. And in Solution 1, because the functions are defined to be NON-VIRTUAL, what happens INSTEAD is that the definition of [spoiler]m[/spoiler] that happens in each type's constructor results in both the code AND the name of that code to be (CONT)
>>
>>58861489
correct, it should be kind of obvious that methods will be virtual by default if you understand the design goals of java though.

>>58861519
this is a good explanation
>>
>>58861519
defined three separate times. Remember that the object is still only created once (with new, but the definitions of m for type A and B still occur when instantiating type C, so you can access any object as any of its sub types(remembering, again, that extended classes are supersets of the classes they extend) which is a way of
casting
the object. What happens is that because the functions are non virtual, you can then access the method as it was defined by that type the object has been casted as(whereas with Virtual, the type definition effectively no longer exists).

I didn't proofread but that's the gist of it
>>
>>58861339
So, in java and only in java:
If you have virtual methods on then the jvm init the child BEFORE the parent by making an implicit call to the parent without checking if its initalized yet.
You have a similar behavior in C with header files:
the compiler know that a function exist but does not need the actual code of the said function.
In your example you create a C object which inherite from B which inherite from A. As I said before the jvm knows the inheritance reliationships
then you create variables A and B BUT you DON'T create a new objects on them. So what you actually do is considering the C object AS A and B.
You manage the C object memory space and in this one the method m is redefine and return 2.
This behavior is legal because the child is init BEFORE the parents, this is where the trick is.

In a non-virtual environment what you'd actually have would be different as the jvm would had initialized THREE DIFFERENTS object with DIFFERENT references ie different memory spaces -> different methods -> different result.

You WON'T EVER see a jvm without virtual-methods on so don't bother trying the non-virtual. If you want to specify a non-virtual method you put the final keyword.
>>
>>58861601
>>58861519
OP here, are you saying if it's a if it's virtual then it stays that way even if you typecast it to a parent class ??
>>
>>58861618
That's the end effect, yeah.
>>
>>58860358
Because it's how polymorphism works.
>>
>>58861642
Thank you for fucking explaining this.

>>58861617
Thanks for this explanation too.

So if you have a virtual function (I know it' been said before) do you need to include the world virtual like >>58861474 or in java are methods virtual by default?

If you want to make a method non-virtual how does that look?

Class A {

}

Class B final Extends A {

}

Class C final extends B {

}

?
>>
OP here again

Sorry for being so angry earlier, thanks for your patience and for giving me excellent responses.
>>
>>58861661
in java, everything is virtual by default. If you want to prevent the child from redefining the method, you put the keyword final in the parent
Class A {
public final int m(){ return 0;}
}

Class B final Extends A {
public int m(){ return 1;} // won't compile as you're not allowed to redefine m
}

you can still call the final method in the child and in the child of the child and so on.
>>
>>58861718
fuck, I quote your text.
remove the final in the class.
you can set a class final and preventing inheritance but that not the point here but it's like:

public final Class A {
public final int m(){ return 0;}
}
>>
>>58861748
im confused


you mean the below to define non-virtual method?


Class final A {
public int m() {return 0;}
}


Class final B extends A {
public int m() {return 1;}
}


Class final C extends B {
public int m() {return 2;}
}

>>
>>58861136
virtual/dynamic method invocation: the type of the object decides what code is run.

non-virtual/static method invocation: the type of the pointer to the object decides what code is run.

in c++ non-virtual methods are faster because the compiler knows exactly which location to jump to at compile time, and that code lives with the class and has fuck all to do with the objects made from that class. virtual methods on the other hand have to be resolved by looking at the object in question, as the method is called.
>>
>System.out.println();

absolutely disgusting
>>
>>58861840
Nope
public Class A {
public final int m() {return 0;}
}


Class B extends A {
public int m() {return 1;}
}


Class C extends B {
public int m() {return 2;}
}

Check the final in class A, this is how you define a non-virtual method.
This code won't even let you compile as m is now non-virtual and can't be redefined.

You can define a final class so you can't do inheritance on it but the syntax is:
public final Class A 

This is off-topic but good to know its possible
>>
>>58861930
this sounds like a contradiction to me. if you can't compile because it's a non-virtual function then how did the professor get the answers in his non-virtual example?
>>
>>58862086
he didn't use final
>>
>>58862158
then how is his method non virtual without final?
>>
>>58862185
Final doesn't actually mean non-virtual, just that it cannot be redefined once it's been initialized.
>>
>>58862216
ok

so then questions

1: How do you define a method as virtual
2: How do you define method as non virtual
>>
>>58862242
In java all functions are virtual unless specified static.

A function can be specified static by either declaring it final or if it's a private method.

The prof is likely not referencing java directly though. But I may also have been confusing earlier, final doesn't mean non-virtual, but using it does cause the function to become non-virtual as a result.
>>
>>58862242
exactly as told before...
you CAN'T write java code like your professor did.
>1: How do you define a method as virtual
everything is virtual by default
>2: How do you define method as non virtual
use final in most case, sometime static but its too soon to care about that

Just stop trying to seek similarities with what your professor wrote. Stick to the java doc.
>>
>>58862332
>>58862319
How should he have rewrote >>58860564

then?
>>
>>58860779
>In the first pic, you can see that C's m method gets called each time despite the type of the object not being C.
The type of the object is C in every case. The type of the variable holding the reference to it is not, but why would it matter? Java is full of bad design, but this is something they got right when designing it.

>>58862242
>How do you define method as non virtual
you don't. use final or static.
>>
>>58862389
What is the proper way to rewrite the non-virtual method as posed in this comment >>58862376
for his code?

I need to see it re-written for his bad example using his example but in the corerect way
>>
>>58862376
public class A {
public final int m() { return 0; } // m is final
}
public class B extends A {
public int n(){ return 1; } // new method cuz can't use m as a name
}
public class C extends B {
public int l() { return 2; } // same
}
public class Main {
public static void main(String[] args) {

C c_obj = new C();
B b_obj = (B) c_obj;
A a_obj = (A) c_obj;
System.out.println(c_obj.l()); // new call
System.out.println(b_obj.n()); // new call
System.out.println(a_obj.m());
}
}

This is not "exactly" the same as only m is non-virtual and not the whole environment but that's impossible in java.
>>
>>58862492
Thanks, so does this mean my professors example is essentially java code that wouldn't compile (rewritten with your source code)?
>>
Only one object is created, c_obj. The b_obj and a_obj variables are just pointers to c_obj, cast to a different type. When the m() method is invoked on any of these objects, the virtual method table of c_obj is used in each case, which points to the method that returns 2.

A virtual method is a method that can be overridden by subclasses. The easiest way to describe it tends to be with animals. We can think of an animal as some fuzzy creature that knows how to talk. We'll give it a method called talk(), which by default will do fuck all. We can think of a dog as a type of an animal, so it's a class that inherits from animal. For a dog, the talk() method should involve barking, so we'll just print "bark" at the screen or some shit. Now let's say we make a new object, and we make that object a dog. If we call talk() on that thing, it's going to bark. Let's say we further decide to make a reference to that, and that reference is just of the animal type, rather than the dog type. It's a dog that is merely acting as an animal. But it's pointing to the same object in memory. If you call talk(), it's gonna bark, instead of doing fucking nothing.

Anyways, go bug your professor during their office hours or some shit.
>>
>>58862599
>>58862492
thanks for the explanations.
>>
>>58862526
His example works in an environment where you can specify to globally use or not virtual methods. In java you can't as you can't turn off polymorphism (which is way more than in your example).
I wouldn't be surprise if he is more of a c++ or c# guy than java.
This is a correct explanation when see from a natively non-virtual environment.
>>
>>58862436
static way
class A {public static int m(){return 0;}}
class B {public static int m(){return 1;}}
class C {public static int m(){return 2;}}

[...]

System.out.println(A.m());
System.out.println(B.m());
System.out.println(C.m());


enum way
public enum M {
A {@Override int m(){return 0;}},
B {@Override int m(){return 1;}},
C {@Override int m(){return 2;}};
abstract int m();
}

[...]

M a_obj = M.A;
M b_obj = M.B;
M c_obj = M.C;
System.out.println(a_obj.m());
System.out.println(b_obj.m());
System.out.println(c_obj.m());


Also tell your prof that using misleading variable names, like a_obj and b_obj holding a reference to some object of type C, is bad practice and discouraged.
>>
>>58862683
>>58862679
thanks
Thread posts: 86
Thread images: 6


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.