What is the keyword super? I don't quite understand what it does. I'm using it in a tutorial but I don't feel comfortable continuing without knowing what it is. Can someone help me out?
Also, what is the term casting?
Printable View
What is the keyword super? I don't quite understand what it does. I'm using it in a tutorial but I don't feel comfortable continuing without knowing what it is. Can someone help me out?
Also, what is the term casting?
I'm new to this forum, no one has instructed me about how to post headers on this forum.
With all due respect, if you would have liked to address me about it, it would've been more efficient to Private Message me, than to spam my post and not give me an answer to the original post.
Be specific from esr's smart question faq.
(which - I have just noticed is also linked to from the faq on this site: Forum Rules)
I've read that super has something to do with superclasses, but, that is where it gets confusing. I'm guessing it overrides the use of using either, a) a object for the class it corresponds with, or a b) private class/protected class it corresponds with.
First off, a super class is the immediate parent class of the child class (in an inheritance hierarchy), and the term "super" can be used in a child's constructor to call the super class's constructor of your choice. So if the super class has one constructor that takes no parameters and another that takes an int, you can tell the child's constructor to call the one that takes the int if you call super(myInt). Otherwise the parameterless constructor will be called by default. This has to be on the first line of the child's constructor to work.
The super key word can also be used to call any of the super class's methods.
As for casting -- you will find much on this (and on the super key word) in tutorials via Google.
Code:import javax.swing.JFrame;
public class Main{
public static void main( String [] args) {
JFrame frame = new JFrame("Star Blasters II");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(new GameFrame());
frame.setVisible(true);
}
}
When using these two classes, and I try to compile the GameFrame class, I get this error.Code:import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class GameFrame extends JPanel {
public GameFrame() {
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("test", 100 , 100);
}
}
http://gyazo.com/86d4481db4fada9d6e3ac33e243338f8.png
You've got an import problem. Can you figure out what is missing based on this hint? :)
I imported JPanel, and it compiles that correctly, but in my mainclass, I get this error when I compile, this tut is turning out worse and worse every time!Code:import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class GameFrame extends JPanel {
public GameFrame() {
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawString("test", 100 , 100);
}
}
http://gyazo.com/772f07e33ca9f816a6670799bd1dd307.png
Did you try to compile Main before successfully compiling GameFrame? Are they in the same directory/package?
I don't think that the tutorial is at fault, but rather that you're new to the language, and it will take many little steps to get things started, but once over the hump things will go much better. Expect to be thrown more speed bumps on this journey, but don't give up yet.Quote:
...this tut is turning out worse and worse every time!
I'm not sure I know how to make a package on Notepad++. Am I ready to use an IDE, Fubarable-Sensai?
No stay with Notepad++ for now Army-san.
How do you make a package on Notepad++ then, sensai?
For my money, I go to the source:
The Java Tutorials
What Is a Package?
Lesson: Packages
Wow, I am facepalming right now. I looked at what is a interface, and the class I'm taking now basically copied and pasted all that stuff to their online course.
In other news, I'm guessing you can... "declare" (excuse my terminology) a package by saying - package Something; at the top?
Here is some more detailed information on how to do command line compilation and running of java files:
javac - Java programming language compiler
java - the Java application launcher
Thanks for being so patient, you should see my tabs.
Anyway, even if import everything right on this step, the tutorial does not give me enough information later on, for he is using an IDE and he basically is clicking to make methods and imports. I cannot follow everything he is doing :/.