#Super Classes

149 messages · Page 1 of 1 (latest)

dapper sphinx
#

This might just be a dumb question cus I recently started Java so please bare with me. But if i were to have multiple interfaces in one class what would the Super be?

lost jungleBOT
#

This post has been reserved for your question.

Hey @dapper sphinx! Please use /close or the Close Post button above when your problem is solved. 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.

raven garnet
#

Superinterface is the description of an inheritance relation between two interfaces. If interface Car extends interface Vehicle, then the relation between Vehicle and Car is: "Vehicle is a superinterface of Car".

dapper sphinx
#

What if you have a normal clas with two interfaces

#

Or is that bad practice

raven garnet
#

Superinterfaces is between interfaces only. There is a second term, the Superclass. A normal class having two interfaces is very good practice.

dapper sphinx
#

Oh

#

Then if you were to call super in the class what would it call?

raven garnet
#

There are two kind of super. The constructor-super and the method-super.

#

What do you mean?

dapper sphinx
#

Can u explain both?

raven garnet
#

Sure, first the constructor super. Give me a secodn.

#
    public class Mercedes extends Car {
        public Mercedes(String designatedLicensePlate) {
            super(designatedLicensePlate);
        }
    }

    public class Car {
        private String licensePlate;

        public Car(String designatedLicensePlate) {
            this.licensePlate = designatedLicensePlate;
        }
        
        public String getLicensePlate() {
            return this.licensePlate;
        }
    }```
#

In line 3 you are reusing the constructor of the class Car.

#

Throu "super"

dapper sphinx
#

oh

#

so your doing

#

car(parameter); pretty much

#

but what would happen if you were to have

#

an extend

#

and an implement

#

and call super?

raven garnet
#

In line 1 you see the extend. In line 3 you see the super. The only thing left is the implement.

dapper sphinx
#

oh super(); is to get the constructor and super.method is to get the method of the super class in case of overrides and others?

raven garnet
#

Yes. But be aware of constructors without a parameter. They behave compleatly different!

dapper sphinx
#

oh

raven garnet
#

Constructors without arguments are called "default constructors". They do must not be called via "super", they call themselves automatically.

dapper sphinx
#

automatically?

#

how so

raven garnet
#

I dont know the reason. They just do it this way.

dapper sphinx
#

oh

#

if you were to do smth like this

#

how would you call jframe

#

or runnable

#

like

#

javax.swing.jframe?

raven garnet
#

First things first. The name "MyThread" is bad because the class is not a Thread. You better call it "MyRunnable" or "MyRunnableJFrame"

dapper sphinx
dapper sphinx
#

i just chose a random name cus i was lazy

#

ik its bad practice tho

raven garnet
raven garnet
#

Your "MyRunnableJFrame" is aswell a JFrame AND a Runnable.

dapper sphinx
#

ye i just used it as an example of how would you call one or antoher

#

like how would u call the jframe

#

or the runnable

raven garnet
#

Just like the "Prince" is aswell a King-son AND a Queen-son.

dapper sphinx
#

ok

raven garnet
#

Ready for the super in methods?

dapper sphinx
#

sure

raven garnet
#

1 second

dapper sphinx
#

alr

raven garnet
#
        public void shout() {
            super.shout();
        }
    }
    public class Cat {
        public void shout() {
            System.out.println("Meow");
        }
    }
dapper sphinx
#

cat.shout?

raven garnet
#

idk

#

yes

dapper sphinx
#

oh

raven garnet
#

So in line 3 you see the super again but having a dot and then the reference to the Cat's shout-method.

dapper sphinx
#

yep

raven garnet
#

Not every cat sais "Meow". A lion is also a cat but a Lion should implement its very own version of "shout". "Roar" for example. So you will decide to not call the super.shout();

dapper sphinx
#

alr

dapper sphinx
raven garnet
#

"Unresolved compilation error" can mean thousand things. Its like the "computer sais noooo".

dapper sphinx
#

when you extend this ig

#

how would you fix this from happening

#

or does it just normally not happen at all

raven garnet
#

"Something" have a constructor. Since there is the parameter "n" it is not the default-constructor.

dapper sphinx
#

true but is this just something that normally you shouldnt put a constructor into a parent class

raven garnet
#

Try to add public MyThreads() {super("");} to line 4 of "MyThreads"

dapper sphinx
#

oh it worked

#

how

raven garnet
#

MyThreads is a Something. And Something have no default-constructor. So the only way a Something can be constructed is by specifying the parameter "n". So the only way how "MyThreads" can be constructed is by specifying the parameter "n" aswell.

dapper sphinx
#

oh so this is a way of specifying the super class in the class

#

Classname() {super(some parameter)};

#

oh thats interesting

dapper sphinx
#

i understand what you mean

#

but how would you call the king specifically

#

or the queen

#

if you just wanted one of the other

dapper sphinx
raven garnet
dapper sphinx
#

alright thank you

lost jungleBOT
# dapper sphinx alright thank you

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

raven garnet
dapper sphinx
#

oh

#

but is there a way to

#

sorta just call ones method

#

like a king can have its own methods

raven garnet
#

You might be able to hide the concrete class using interfaces.

dapper sphinx
#

and a queen can have its own

#

and you can call upon a specific method from each

#

or is that not possible

raven garnet
#

You could hide methods using interfaces. But be aware that every instance extends from java.lang.Object. This is a thing you absolutly can not hide!

dapper sphinx
#

how would you call the methods in the interfaces

#

like lets say you overrode a method

#

and wanted the parents version of a method

#

but you have two interfaces

raven garnet
#

Give me a second

dapper sphinx
#

ok

raven garnet
#
        public void shout() {
            System.out.println("Meow");
        }
        public void purr(){
            System.out.println("Purrr");
        }
    }
    public interface Shoutable {
        void shout(); //"public" is added for all interfaces automatically by the compiler.
    }```
dapper sphinx
#

i mean in this case would super.shout(); not work?

raven garnet
dapper sphinx
#

oh

#

thats interesting

raven garnet
#

You could then create the cat using Shoutable tomcat = new Cat(); The type of the variable tomcat is "Shoutable".

#

Even if the Cat might have much more methods, you can not reference those methods throu the variable "tomcat" because "tomcat" is konw to be of type Shoutable only.

dapper sphinx
#

what

raven garnet
#

You can not write ```
Shoutable tomcat = new Cat();
tomcat.purr();

dapper sphinx
#

is cat an interface in this example

#

or smth

dapper sphinx
raven garnet
#

I fixed the example, sorry for that

dapper sphinx
#

oh thanks

lost jungleBOT
# dapper sphinx oh thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

dapper sphinx
raven garnet
#

You can google mycoon:

dapper sphinx
raven garnet
dapper sphinx
#

you can only acces shoutable's methods

#

compared to

#

if you made the type Cat

raven garnet
dapper sphinx
#

oh

#

how come both of them would end with

#

new cat(); tho

#

does the end always reference the childclass?

#

and the beginning is what methods it can access

raven garnet
dapper sphinx
#

so coding against interfaces is how you access things within a class that implements an interface?

#

lik ehow you create an object

#

and how you access methods of its interfaces

raven garnet
#

Yes. You can reuse good-practice-code much better!

dapper sphinx
#

Oh ic

#

also is there any resource you recommend to use for someone who's tryna start java

raven garnet
#

Well, this is a question someone else should answer, this Posts are for concrete programming problems and questions only.

dapper sphinx
#

Oh alright

#

Well thank you for your time

lost jungleBOT
# dapper sphinx Well thank you for your time

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

dapper sphinx
#

it was very informative

raven garnet
#

At your service!