Can you access private methods outside of the class it was made in?
No, obviously not stupid
How to tell the difference between static and non-static methods?
An object is used for a non-static method.
expression obj = new expression(4, 2)
Call: expression.displayInfo(x, y)
No object is needed for a static method:
private static void greet(x,y) {}
Call: greet(text, text2)
List 3 return types of a method?
What is the name of the method that Java automatically calls when running a program?
main method
What method type does not return any value?
What access modifier allows the method to be used by its sub class.
Public
What type of method belongs to the class itself rather than instances?
static method
What data type should myMethod's return data type so as to not result in an error?
public _____ myMethod(int x){
return x + 6;
}
int
What will this method call print?
System.out.println(myMethod(4));
public double myMethod(int x){
return (x / 8 + 2) * 4
}
10.0
What happens when you run this method?
public void subtract(int a, int b){
return a - b;
}
It gives an error for using a return statement in a void method?
If a void method is labeled with this keyword, it can only be accessed by other methods in the class, often used for "helper" tasks that the user shouldn't see.
What is Private
What is wrong with this static method?
public int static add(int a, int b){
return a + b;
}
The static and int need to be switched.
Will this program return an error?
public void myMethod(int x){
return x + 7;
}
Yes
These are values that are passed inside of a methods parameters when it is called, allowing the method to perform actions on specific data.
What is an argument
Does this method change the variable x in the calculator class?
public class Calculator {
int x = 10;
public void subtract(){
x--;
}
}
Yes it does decrease x by 1
What are the two properties of the protected modifier?
Is able to be inherited by sub-class.
Is not able to be accessed outside of the class/sub-classes.
What is wrong with the call of this non-static method from the Calculator class?
System.print.ln(Calculator.multiply(3,4));
The Calculator class needs to be initialized as an object.
If a method returns an int, but a double is returned instead, what will happen?
compilation error
This term describes having multiple methods with the same name but different parameters. (e.g. math(int x, int y) VS math(int x, String s))
What is Method Overloading.
What happens when you try to assign a variable to a void method?
Example: String str = assignGreeting()
Error, it can't assign int to a void method.
There is no data value at the end of a void methods' run
Basically, the computer sees this:
String str = (Nothing)
What idea of Object-Oriented Programing (OOP) focus's on the ability to keep data safe using the protected access modifier.
What is encapsulation?
(Encapsulation is why someone can't walk up to your bank account and write yourAccount.balance = 0;)
This method keyword allows you to inference a method without creating an instance of the class. Sharing members across the entire class (Including variables, methods, and objects)
What is a static method.
What will this program print?
public class MyProgram {
public static void main(String[] args)
{
System.out.println(timmy("5", 4, 'w'));
}
public static String timmy(String s, int i, char c){
if(Integer.parseInt(s) * i > 20 ){
s = s + c;
i = i + s.length();
return "" + i;
} else if ((s + c).length() < 5){
s = s + i;
c = 'd';
s = s + c;
return s;
} else {
return "" + c;
}
}
}
54d
What is the action of providing a specific implementation of an already existing method defined in its parent class. (Hint uses @Override)
What is Method Overriding.
Example:
class Animal {
class Dog extends Animal{
@Override void move(){
Can you overload a previously used void method to be able to return an int?
Yes, if you change the parameters as needed to properly overload a method.