#Difference between Shape circleObj = new Circle(); and Circle circleObj = new Circle();?

33 messages · Page 1 of 1 (latest)

tough marten
#

I tried both and achieving the same output/result. So, why need to use 2-statements instead of 1.

summer folioBOT
#

This post has been reserved for your question.

Hey @tough marten! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tough marten
#

Difference between Shape circleObj = new Circle(); and Circle circleObj = new Circle();?

pseudo pawn
#

You don't "need" those two statements. That there is more thzn one way to get the same result should not baffle you

#

We accept 1+1 and 1 + 1 out of convenience

#

The difference between your two lines is a compile-time difference, when you're writing the program. It doesn't change what the program does when you run it

#

The difference is the type of the variable. Probably a Circle class has methods that a Shape class does not have. So if your variable is of type Circle you can call getRadius() on it, and with a Shape variable you can't because not all shapes have a radius

#

That also means that if you make a variable of type Circle when that's not necessary, you won't be able to give it any other kind of shapes, so you're making your life difficult. Don't use specialized variable types when you don't need them specialized. Do use specizlized variable types when you do need them dpecialized

stable cloak
#

the difference is semantic. when you have a broader type, it says that the given variable could be a different type within that broader constraint

tough marten
#

@pseudo pawn , I understand what you try to say.

It's also for the abstration and interfaces?

pseudo pawn
#

Abstraction isn't a keyword

#

It doesn't matter whether the supertype is a class or an interface. It works the same

tough marten
#

In simple interface,

Movable car = new Car();

OR

In multiple interface,

Animal animal = new Animal();

Why, not doing the same as simple interface?

pseudo pawn
#

These examples don't seem related to the subject of multiple interfaces. Simple interface isn't a thing

stable cloak
#

what do you mean by "multiple interface" here even?

pseudo pawn
#

You cannot give names to stuff without showing them. That should be obvious with no need to mention

#

These two code samples try to demonstrate completely different things, so they do things differently

tough marten
#

It's implemented by Animal subclass so?

pseudo pawn
#

It makes no sense trying to compare them

tough marten
#

Ok

pseudo pawn
#

"It"?

tough marten
#

I think so

pseudo pawn
#

Dude, what is "it"?

tough marten
#

interface movable and eatable implements by Animal subclass.

pseudo pawn
#

Okay?

tough marten
#

What I did wrong is I mixed two things

tough marten
pseudo pawn
#

It is. Abstraction is not

tough marten
#

hehe.