Java OOP Flashcards

1
Q

Object-oriented programming has several advantages over procedural programming:

A

OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Classes and Objects?

A

So, a class is a template for objects,/like an object constructor and an object is an instance of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you create an object (of Main)?

A

To create an object of Main, specify the class name, followed by the object name, and use the keyword new:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can you access an object of a class in another class?

A

Yes!

This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is another name for a variable ?

A

an attribute of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is another term for class attributes?

A

Another term for class attributes is fields.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we access attributes of a class?

A

You can access attributes by creating an object of the class, and by using the dot syntax (.):

The following example will create an object of the Main class, with the name myObj. We use the x attribute on the object to print its value:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you modify an attribute?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If you don’t want the ability to override existing values, what would you do?

A

declare the attribute as final:

The final keyword is useful when you want a variable to always store the same value, like PI (3.14159…).

The final keyword is called a “modifier”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If you create multiple objects of one class, how would changing the attribute value in one object affect the other?

A

you can change the attribute values in one object, without affecting the attribute values in the other:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are methods in Java and why use them?

A

A method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

Methods are used to perform certain actions, and they are also known as functions.

Why use methods? To reuse code: define the code once, and use it many times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a method?

A

A method must be declared within a class. It is defined with the name of the method, followed by parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also create your own methods to perform certain actions:

myMethod() is the name of the method
static means that the method belongs to the Main class and not an object of the Main class. You will learn more about objects and how to access methods through objects later in this tutorial.
void means that this method does not have a return value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you Call a Method?

A

To call a method in Java, write the method’s name followed by two parentheses () and a semicolon;

In the following example, myMethod() is used to print a text (the action), when it is called:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is void and what can you replace it with?

A

The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is method overloading?

A

With method overloading, multiple methods can have the same name with different parameters:

Note: Multiple methods can have the same name as long as the number and/or type of parameters are different.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Java scope?

A

In Java, variables are only accessible inside the region they are created. This is called scope.

Variables declared directly inside a method are available anywhere in the method following the line of code in which they were declared:

17
Q

What is a block scope?

A

A block of code refers to all of the code between curly braces {}.

Variables declared inside blocks of code are only accessible by the code between the curly braces, which follows the line in which the variable was declared:

18
Q

difference between static and public?

A

You will often see Java programs that have either static or public attributes and methods.

In the example above, we created a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects:

19
Q

What is a constructor?

A

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes:

Note that the constructor name must match the class name, and it cannot have a return type (like void).

Also note that the constructor is called when the object is created.