#Super Classes
149 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @dapper sphinx! Please use
/closeor theClose Postbutton 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.
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".
Superinterfaces is between interfaces only. There is a second term, the Superclass. A normal class having two interfaces is very good practice.
There are two kind of super. The constructor-super and the method-super.
What do you mean?
Can u explain both?
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"
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?
In line 1 you see the extend. In line 3 you see the super. The only thing left is the implement.
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?
Yes. But be aware of constructors without a parameter. They behave compleatly different!
oh
Constructors without arguments are called "default constructors". They do must not be called via "super", they call themselves automatically.
I dont know the reason. They just do it this way.
oh
if you were to do smth like this
how would you call jframe
or runnable
like
javax.swing.jframe?
First things first. The name "MyThread" is bad because the class is not a Thread. You better call it "MyRunnable" or "MyRunnableJFrame"
oh so if you make an object that has inherited classes and that inherited class is default that just runs
yeah thats true
i just chose a random name cus i was lazy
ik its bad practice tho
I would say "if you make an object that has inherited classes and that inherited class has default-constructor that just runs"
alr
thats makes sense
Your "MyRunnableJFrame" is aswell a JFrame AND a Runnable.
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
Just like the "Prince" is aswell a King-son AND a Queen-son.
ok
Ready for the super in methods?
sure
1 second
alr
public void shout() {
super.shout();
}
}
public class Cat {
public void shout() {
System.out.println("Meow");
}
}
cat.shout?
oh
So in line 3 you see the super again but having a dot and then the reference to the Cat's shout-method.
yep
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();
alr
oh ye for this if the super class were to have a parameter in the constructor how would you fix the error the unresolved compilation error
"Unresolved compilation error" can mean thousand things. Its like the "computer sais noooo".
when you extend this ig
how would you fix this from happening
or does it just normally not happen at all
"Something" have a constructor. Since there is the parameter "n" it is not the default-constructor.
true but is this just something that normally you shouldnt put a constructor into a parent class
Try to add public MyThreads() {super("");} to line 4 of "MyThreads"
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.
oh so this is a way of specifying the super class in the class
Classname() {super(some parameter)};
oh thats interesting
also one last question
i understand what you mean
but how would you call the king specifically
or the queen
if you just wanted one of the other
also is there a specific name for this
non-default-constructor-overwrite.
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.
You can not do that. You can neither ignore that the Lion is a Cat nor the Mercedes is a Car nor the MyThreads is a Something.
oh
but is there a way to
sorta just call ones method
like a king can have its own methods
You might be able to hide the concrete class using interfaces.
and a queen can have its own
and you can call upon a specific method from each
or is that not possible
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!
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
Give me a second
ok
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.
}```
i mean in this case would super.shout(); not work?
No, super is a class thing, not an interface-thing.
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.
what
You can not write ```
Shoutable tomcat = new Cat();
tomcat.purr();
also what is a mycoon
I fixed the example, sorry for that
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.
why can't this be written tho?
You can google mycoon:
oh its a type of cat
Exactly
oh because the type is shoutable
you can only acces shoutable's methods
compared to
if you made the type Cat
Because the first Word in the Code is "Shoutable" and this defines what tomcat is allowed to be used.
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
It should. It is good practice, it is called coding-against-interfaces (aka "program to interfaces, not implementations").
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
Yes. You can reuse good-practice-code much better!
Oh ic
also is there any resource you recommend to use for someone who's tryna start java
Well, this is a question someone else should answer, this Posts are for concrete programming problems and questions only.
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.
it was very informative
At your service!