Classes and stuff? Flashcards

1
Q

What are three modifiers for accessing privileges?

A

Public,
Private,
Protected

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

What are the 2 ways to create objects?

A

Copy and adjust existing ones.

Instantiate a template

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

What way does Java create objects?

A

By instantiating a template

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

What is a class used for in Object-Orientated Programming?

A

A class is a template for buliding objects

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

What three things does a class have when being used in Object-Orientated Programming?

A

Attributes
Constructors
Methods

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

How do you create a new object in Java?

A

ObjectType name = new ObjectConstructor(args);

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

If an object bob has a private attribute age, can this attribute be accessed using bob.age;?

A

No, you need to use a getter method inside the class

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

If an object bob has a public attribute age, can this be accessed using bob.age;?

A

Yes, but this is bad practice to do, attributes should be private or protected

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

What is the difference between private and protected?

A

Private can only be used by that class, protected acts like private from a user’s perspective but can be used by subclasses

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

What 2 things happen when you instantiate an object with the keyword new?

A

The JVM allocates memory to store the new object.

A constructor method is called to initialise it.

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

What is the syntax for creating a constructor for a class called XClass?

A
public XClass(parameters) {
    //attribute assingments
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you call one constructor from within another?

A

You use the keyword ‘this’.

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

What does the keyword static do?

A
This means the variable belongs to the class rather than the instance and thus is the same for all objects of this class.
Static methods can only access static variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some of the advantages of using inheritance in OOP?

A
  • Introduces more abstraction
  • Enhances code re-usability
  • Improves code readability
  • Can reduce software maintenance costs
  • Allows multiple people to program subclasses simultaneously
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does inheritance work in OOP?

A

Subclasses inherit methods and attributes from the superclass and extend them (for instance to add more specific things)

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

How is a class declared as a subclass of a superclass?

A
modifiers class SubClass extends SuperClass {
//class body}
17
Q

Can you call a superclass constructor from within the subclass?

A

Yes, you use the keyword ‘super’. It must be the first thing in the constructor method.

18
Q

What is overwriting/overriding?

A

When you redeclare a method from the superclass in the subclass. The version in the subclass is the version that will be used.

19
Q

What types of methods/attributes will a subclass inherit (private/public/protected)?

A

Subclasses will only inherit public and protected attributes and methods.
Anything private are not inherited

20
Q

What is an abstract class?

A

An abstract class is a class which can be extended to a subclass but not instantiated by itself.

21
Q

How do you declare an abstract class?

A
public abstract class ClassName {
//class body}
22
Q

What is the naming convention for interfaces?

A

____able

23
Q

Do abstract methods have the method body written when declared?

A

No, the method body is created when the subclass overrides the method (which it must do for abstract methods)

24
Q

What does an interface contain?

A

Method signatures.
The bodies are written in clases implementing the interfaces.

They can also contain attributes for constants.

25
Q

How would interfaces be used in instantiation?

A
Interface name = new Constructor();
The interface is put as a type for the object and the object is created with its usual constructor.
26
Q

How does a class use an interface?

A
public class ClassName implements InterfaceName1, InterfaceName2, etc {
//Class Body}
27
Q

How many interfaces can one class implement?

A

As many as it wants to

28
Q

If a class implements an interface what must that class contain?

A

The method bodies for methods declared in the interface.