Objects, Classes and Constructors Flashcards
How many classes in a file can be a public class?
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.
Constructors are a special kind of method. What are their three peculiarities?
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.)
___ represents an entity in the real world that can be distinctly identified
A class
__ is a construct that defines objects of the same type
A class
An object is an instance of a __
class
__ is invoked to create an object
A constructor
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
x contains a reference to a Circle object
The default value for data field of a boolean type, numeric type, object type is ____, _____, ______ respectively
false, 0, null
what is the syntax for creating an object (workerOne) from the class Workers using a default constructor
Workers workerOne = new Workers();
what are the 3 types of constructors?
Default Constructor
No-Arg (no-argument) Constructor
Parameterized Constructor
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
Workers workerOne = new Workers(“Bob”, 35);
what does overloading mean?
That you can have more than one method (including constructors) with the same name as long as the method is unique.
what are the criteria that can make a method(including constructors) unique?
Different number or order of arguments/parameters
AND / OR
Different parameter/arguments types
What is dot notation used for?
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.
what are the members of a class?
variables (aka: attributes or fields),
methods,
constructors.
what are the 4 types of access modifiers?
public
private
protected
default
What are access modifiers used for?
to specify the visibility of a class, its data members, and methods to other parts of the code.
What is an abstract data type(ADT)?
Another name for the data type you’ve created when you define a class.
What type of naming convention is used for classes?
Classes are in pascal case which starts with a capital letter and then squeezes words together which all begin with upper case.
What is a “use case”?
Use cases describe what a system is supposed to do.
What is “composition” when referring to Java classes?
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.
What is a Unified Modeling Language(UML) class diagram?
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.
What is the syntax to check if two object (objectOne and objectTwo) are of the same class and output the Boolean result?
System.out.println(objectOne.getClass() == object.Two.getClass());
What is the @Override annotation and why can it be useful?
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.
what’s one way to check if an object o is a member of the Person class and return false if it’s not
if (!(o instanceof Person)) return false;
Explain the (Person) part of this line from a method that overrides the .equals() for the class Person: Person other = (Person) o;
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.