Chapter 5--creating Classes & Methods Flashcards
(21 cards)
Variables are considered instance variables if _____
They are declared outside a method definition but are NOT modified by the static keyword.
How do you create a class variable?
static int VERN;
static final int YAR=10;
How do you create a new class?
with the extends keyword. e.g.,
class Buttknocker extends Fartknocker {
int Vern;
float Farts;
}
What is overloading methods? What makes it work or not work?
It is creating methods that have the same name. They must differ in the number and type of arguments. The names of the arguments do not matter.
Can a constructor with no arguments be called in a class that already has a constructor that accepts one or more arguments?
Only if a constructor with no arguments has been defined in that class
What are the basic differences between methods and constructors?
Constructors
- Always have the same name as the class
- Don’t have a return type
- Can’t return a value in the method by using the return statement
What is a method’s signature?
It’s name and argument list
How do you override a method?
Create a method in a subclass that has the same signature as a method in a superclass.
If a source code file contains more than one main(), what happens?
Netbeans asks which one should be run
How do you call a superclass method that has already been overridden?
with the super keyword, i.e.,
super.methodname()
Can constructors be overridden? Why or why not?
No, because they always have the same name as the current class.
How do you call a constructor from the superclass instead of the one from the current class? What are the rules for this?
super()
it must be the first statement in the constructor definition.
Also, there must be a constructor with that signature in the immediate superclass.
Is it better to create your own constructors, or to pass them up the hierarchy? Why?
Pass them. You don’t always know everything a constructor is doing re: initializing.
Use the extends keyword in an example. What does it do?
Creates a subclass
class Vernyverns extends Verns{ //body of the class
Vernyverns is the subclass, Verns sic the superclass
Create a class with examples of instance variables
class Buttnuggets extends ButtEffluvia{ String smell; String texture; float viscosity; }
How do you create a method that does not have a return value?
Use the void keyword
How do you refer to the object itself in the body of a method definition?
With the this keyword
What are the possible return types in a method definition?
Void, a class, or a primitive data type
What do the parameters in a method signature become?
Local variables in the body of the method definition. They receive their values when the method is called
What is required with a value-returning method?
The return keyword inside the method definition
Can you use the this keyword for a class method?
No