Object Oriented Programming - Java Flashcards
The . Operator
The . operator gives you access to all members of a given class. Including access to the variables that the object holds
What is a class?
A class is a user-defined blueprint from where objects are created
What are members of a class?
Members of a class are all of its fields, properties, methods, and everything that defines it
How do you create a Class?
- Define Properties
- Give values to the Properties
- Create an Instance of the Object(class)
What is a namespace?
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
Define Constructors
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.
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); } }
- we created a new variable with a data type, Point
- new Point(), is a new object with data type, Point
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); } }
- When you use the new keyword you are creating a new instance of the given class(object), this is called instantiation
Explain what the “this” keyword does for a class
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.
What is the protection level on a field and how do you access them?
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.
What is the purpose of using a get and set inside of a property instead of just making a field public?
- 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
explain get{ }
get returns the value of the field that we are using or referencing inside of the property
explain set{ }
sets the value, meaning for example it sets or gives the value of the x property to the x field
What do different versions of get and set control?
- we can control if a given Property is Read-Only, Write-Only or Read-Write.
What is a getter and setter in read-only and write-only terms?
Getter = Read Setter = Write