Java Classes Flashcards

1
Q

What is object oriented programming?

A

object-oriented programming is about creating objects that contain both data and methods

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

What are classes in Java?

A

A Class is like an object constructor, or a “blueprint” for creating objects.

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

What is an object?

A

An object is an instance of a class. You would instantiate an object which would call the constructor of the object and then depending on the parameters involved you would set the variables and then have access to the methods.

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

What are Java Class Attributes?

A

Attributes are variables within a class and can be any of the data types

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

How would you access attributes in an object?

A

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

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

How do you modify an attribute?

A

By using the assignment operator = after the object is created

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

How does the keyword final affect an attribute in a class?

A

It makes it so you can’t modify the value.

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

How would you call a static method in a class?

A

Would simply call the method myStaticMethod(); in the main method

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

How would you call a non-static method?

A

Would need to first create an object and then use the dot notation to access the method.

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

What is a constructor in Java?

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

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

What are constructor parameters?

A

Constructors can also take parameters, which is used to initialize attributes. Parameters are data that is passed into the object to set attributes or for method calls.

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

What are the two types of modifiers for methods in Java?

A

1- Access

2 - Non-access

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

What are the different access modifiers for classes, methods, and attributes?

A

For classes, you can use either public or default. For attributes, methods and constructors, you can use the one of the following

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

What are the different non-access modifiers for classes, methods, and attributes?

A

For classes, you can use either final or abstract. For attributes and methods, you can use the one of the following

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

What is a static method?

A

A static method means that it can be accessed without creating an object of the class, unlike public

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

What is an abstract method?

A

An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass

17
Q

What is encapsulation?

A

he meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must:

declare class variables/attributes as private
provide public get and set methods to access and update the value of a private variable
18
Q

What are getter and setter methods in java?

A

The get method returns the variable value, and the set method sets the value

19
Q

Why is encapsulation important?

A

Better control of class attributes and methods
Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
Flexible: the programmer can change one part of the code without affecting other parts
Increased security of data

20
Q

What is a package?

A

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories:

Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)

21
Q

What is the this keyword and how is it used?

A

It is used in the constructor to set the value of the variable in the class and to reference it.

22
Q

What is the super keyword and how is it used?

A

It is used to call the constructor of the parent class or reference a variable or method of the parent class

23
Q

What is the toString method and how is it created?

A

it is a method that is called to verify the contents of the variables defined in a class. It can be created automatically with most IDE’s