Object Oriented Programming - Java Flashcards

1
Q

The . Operator

A

The . operator gives you access to all members of a given class. Including access to the variables that the object holds

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

What is a class?

A

A class is a user-defined blueprint from where objects are created

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

What are members of a class?

A

Members of a class are all of its fields, properties, methods, and everything that defines it

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

How do you create a Class?

A
  • Define Properties
  • Give values to the Properties
  • Create an Instance of the Object(class)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a namespace?

A

The namespace lets you organize your classes and files. The class name should match the name of the .cs file and the namespace name should match the name of the folder the .cs file is in.

  • add ( using FileName; ) at the top of .cs file to import a class from another folder.
  • namespaces can have subnamespaces
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define Constructors

A
Constructor is the method that constructs your class.
It has the name of the class and no data type and it may or may not take in arguments.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Explain the line of code: "Point point = new Point();"
     class Point
    {
        public int x;
        public int y;
    } 
     class EntryPoint
    {
        static void Main()
        {
            Point point = new Point();
            point.x = 5;
            point.y = 3;
            Console.WriteLine(point.x);
            Console.WriteLine(point.y);
        }
    }
A
  • we created a new variable with a data type, Point

- new Point(), is a new object with data type, Point

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Explain what the new keyword means below:
     class Point
    {
        public int x;
        public int y;
    } 
     class EntryPoint
    {
        static void Main()
        {
            Point point = new Point();
            point.x = 5;
            point.y = 3;
            Console.WriteLine(point.x);
            Console.WriteLine(point.y);
        }
    }
A
  • When you use the new keyword you are creating a new instance of the given class(object), this is called instantiation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain what the “this” keyword does for a class

A
this, keyword gives you access to members of your class and lets you distinguish between two variables that have the same name.
- Use the this keyword to show that you are referencing a member from your class if there is a variable naming conflict.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the protection level on a field and how do you access them?

A

Fields are almost always private, they are not directly exposed to the “outside world”. Their values can be accessed only by a Property or some other logic in your Class.

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

What is the purpose of using a get and set inside of a property instead of just making a field public?

A
  • You can implement validation logic inside the scopes of get and set. Ex, you may want to set the value to x only if its between 5 and 10.
  • Between the get and set { } you can perform validation using if statements or loops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

explain get{ }

A

get returns the value of the field that we are using or referencing inside of the property

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

explain set{ }

A

sets the value, meaning for example it sets or gives the value of the x property to the x field

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

What do different versions of get and set control?

A
  • we can control if a given Property is Read-Only, Write-Only or Read-Write.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a getter and setter in read-only and write-only terms?

A
Getter = Read
Setter = Write
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to access static fields?

A

Static Fields(Static Members in general) are not dependant on the instance of a given class. They can be accessed directly from the class without instantiating it.

17
Q

What is the value of the static field dependent on?

A

The value of a Static Field is NOT dependent on a Class instance. The static field is “shared” amongst all instances of the Class and they all can modify or access it internally.

18
Q

How can static fields be shared?

A
  • You can use a public static field to share a variable between all instances of your class.
  • Each class can access and change the variable of that field.
19
Q

What are the two different kinds of constants?

A
  1. const keyword
  2. readonly keyword

Note. Constants are written in all Caps and separated with an underscore. ex.1) ID ex.2) THE_NAME_OF_THE_CONST

20
Q

What is the const keyword? How do you access the field with this constant?

A

const = value hardcoded in the code.

  • when creating the const you have to assign the value to the const.
    • const constants are static by default. This means they are accessed the same as static fields through the class. ex.) User.HEIGHT;
21
Q

What is the readonly keyword? How do you access the field with this constant?

A

readonly = value assigned later on in code logic

  • get their value from inside of the constructor
  • the readonly is accessed through the variable assigned to the new object()
22
Q

Why do constants exist for fields?

A

We should not be able to directly modify any public fields after instantiating a class.
- Constants of any kind can not change their value once it is set

23
Q

What is Enumeration?

A

Enumeration is used to prevent possible mistakes by a User, by giving them a set of values from which they can choose from.

24
Q

Can static classes have constructors?

A

No, Static classes can not have constructors as they can NOT be instantiated so they do not need one.

25
Q

Public - what is it in the main method?

A

Public is an access modifier. An access modifier determines the accessibility of the class, variable, or method.

26
Q

name the eight primitive data types in Java

A

boolean, byte, char, short, int, long, float, double

27
Q

Method Overloading

A

It is the ability to create more than one method with the same name, as long as it has different parameters.

28
Q

public/private static final

A

this is how to create a constant value in Java. The final keyword determines if the variable is a constant

29
Q

Java switch statement

A

Can only be used with byte, short, char and int data types