Classes, Objects and References Flashcards

1
Q

What is Object Oriented Programming?

A

-it means breaking normally procedural code into structures called objects

By creating relationships between objects, we can write code that applies to families or collections of related types of objects. Ultimately, this allows us to write less code by writing more generic code

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

A class

A
***a class is a template used to instantiate objects. It is called type in some circumstances such as when used with a reference variable.
determines what states and behaviors an object will possess*****

A class used to instantiate an object determines what states and behaviors an object will possess - as a template, the object will possess individual copies of the states and behaviors defined in the class. A class used as the type for a reference variable determines what behaviors of the referenced object can be invoked.

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

An object

A

-an object is an instance of a class in memory. You never interact with them directly, but interact through their reference instead, (which is the memory address used by the JVM to find a particular object).**

Other OOP languages do allow you to manipulate objects directly, but even these still use references most of the time.

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

A reference variable

A

-**a variable that stores the reference to an object in memory.**

Just like the type of a primitive variable determines the range of values that a primitive variable can store, the type of a reference variable determines what types of objects a reference variable can store a reference to. When a class is used as the type of a reference variable, that reference can only be used to invoke behaviors of the object that are declared in the class/type.

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

Example

A

Animal someVar = new Animal();

  1. The class/type of the reference variable
  2. The name of the reference variable
  3. The instantiation of a new object using the “new” keyword to invoke a constructor
The someVar reference variable does not contain an Animal object, it contains a reference that points to it in memory
The Animal type means that someVar can only store a reference to an object that is an instance of the Animal class (directly or through inheritance)
The Animal type means that someVar can only be used to invoke methods or access public variables present in the Animal class (whether defined in Animal or inherited from a superclass)
The "new Animal()" expression creates an object, it is not the object itself. You can never access the object directly. 
Understanding these concepts and their implications is key to properly understanding OOP languages and concepts.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Inheritance

A
  • allows you to organize classes into a hierarchy
  • properties and behaviors from the top of the hierarchy will be present in the classes lower in the hierarchy

When one class inherits from another, an object that is an instance of the first class will also contain the states and behaviors of the second class.

-creates an “is-a(n)” relationship between the child and parent class

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

When a Class is inherited from is called

A

base, super, or parent class

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

When a Class that inherits is called

A

derived, sub or child class

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

Object Polymorphism

A

means many forms and is The ability for An object to be considered an instance of every class in its inheritance chain*

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

Method Overriding

A

When a method in a child class has the same signature as a method in the parent class, it will hide (the implementation given in the base class) or the inherited implementation behind its own implementation.

  • Since child classes inherit public and protected items from a parent class
  • the child class can also declare its own method with the same name and parameters (method signature), “hiding” the inherited implementation behind its own

-overriding allows developers to write methods that can be called interchangeably but have individual tasks

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

Method Overloading

A

when two methods share the same name, but have a different signature - that is, when they share the same name, but the type/order/number of parameters they accept are different.*

same methods with different parameter types and number

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

C# Inheritance

A

virtual: identifies a method in the parent class that can be overridden by the child class
override: identifies a method as an override

base: 
used to access superclass member 

sealed: class cannot be inherited

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

C# Inheritance- multiple inheritance is not supported by C sharp

A

virtual: identifies a method in the parent class that can be overridden by the child class
override: identifies a method as an override

base: 
used to access superclass member 

sealed: class cannot be inherited

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

Polymorphism

A

is the ability for some code structures in an OOP language to be treated as different structures at runtime.

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

Casting

A

is the process of storing a reference to an object in memory inside a reference variable of a type different than the object itself, or the last type of reference variable used

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

Upcasting

A

upcast a reference by storing a reference to an object in a base-class-typed reference variable.

Automobile car = new Sedan();

17
Q

Downcasting

A

downcasting is when we take a previously upcast reference, and store that reference in a reference variable that is a type closer to that of the actual object being referenced.

Automobile car = new Sedan();
Sedan otherCar = (sedan) car;