Objects, Classes and Constructors Flashcards

1
Q

How many classes in a file can be a public class?

A

In Java, only one class per file can be public.

If a class is declared public, the file name must match the class name, with the extension .java. For example, if you have a public class named MyClass, the file containing the class must be named MyClass.java.

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

Constructors are a special kind of method. What are their three peculiarities?

A

A constructor must have the same name as the class itself.
Constructors do not have a return type, not even void.
Constructors are invoked using the “new” operator when an object is created. (Constructors play the role of initializing objects.)

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

___ represents an entity in the real world that can be distinctly identified

A

A class

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

__ is a construct that defines objects of the same type

A

A class

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

An object is an instance of a __

A

class

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

__ is invoked to create an object

A

A constructor

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

Given the declaration Circle x = new Circle(), which of the following statement is most accurate
x contains an int value
x contains an object of the Circle type
x contains a reference to a Circle object
you can assign an int value to x

A

x contains a reference to a Circle object

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

The default value for data field of a boolean type, numeric type, object type is ____, _____, ______ respectively

A

false, 0, null

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

what is the syntax for creating an object (workerOne) from the class Workers using a default constructor

A

Workers workerOne = new Workers();

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

what are the 3 types of constructors?

A

Default Constructor
No-Arg (no-argument) Constructor
Parameterized Constructor

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

what is the syntax for creating an object (workerOne) from the class Workers using a parameterized constructor which needs name (“Bob”) and age (“35”) arguments

A

Workers workerOne = new Workers(“Bob”, 35);

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

what does overloading mean?

A

That you can have more than one method (including constructors) with the same name as long as the method is unique.

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

what are the criteria that can make a method(including constructors) unique?

A

Different number or order of arguments/parameters
AND / OR
Different parameter/arguments types

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

What is dot notation used for?

A

to access the instance data and methods of an object once it is created. commonly:
Update a class member value.
Access a class member value.
Call a method.

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

what are the members of a class?

A

variables (aka: attributes or fields),
methods,
constructors.

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

what are the 4 types of access modifiers?

A

public
private
protected
default

17
Q

What are access modifiers used for?

A

to specify the visibility of a class, its data members, and methods to other parts of the code.

18
Q

What is an abstract data type(ADT)?

A

Another name for the data type you’ve created when you define a class.

19
Q

What type of naming convention is used for classes?

A

Classes are in pascal case which starts with a capital letter and then squeezes words together which all begin with upper case.

20
Q

What is a “use case”?

A

Use cases describe what a system is supposed to do.

21
Q

What is “composition” when referring to Java classes?

A

A relationship expressed as “has a” or “belongs to” is called composition. Virtually all Java programs use composition to combine classes together to accomplish their tasks.

22
Q

What is a Unified Modeling Language(UML) class diagram?

A

UML class diagrams are a visual way to show what attributes and methods are included in a class. You can translate a class diagram into Java code or you can use a tool to generate class diagrams from existing Java code.

23
Q

What is the syntax to check if two object (objectOne and objectTwo) are of the same class and output the Boolean result?

A

System.out.println(objectOne.getClass() == object.Two.getClass());

24
Q

What is the @Override annotation and why can it be useful?

A

The @Override annotation indicates that the child class method is over-writing its base class method. The @Override annotation can be useful for two reasons. It extracts a warning from the compiler if the annotated method doesn’t actually override anything. It can improve the readability of the source code.

25
Q

what’s one way to check if an object o is a member of the Person class and return false if it’s not

A

if (!(o instanceof Person)) return false;

26
Q

Explain the (Person) part of this line from a method that overrides the .equals() for the class Person: Person other = (Person) o;

A

The (Person) part of the line is called an explicit cast, which is a way of telling the compiler that we know o is actually a Person object (or a subclass of Person).

If o were not a Person object or a subclass of Person, then a ClassCastException would be thrown at runtime. However, we check this earlier in the code with the instanceof keyword, so we know that o is a Person object (or a subclass of Person) before we cast it to a Person object in this line.