Classes, Objects and References Flashcards
What is Object Oriented Programming?
-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
A class
***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.
An object
-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.
A reference variable
-**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.
Example
Animal someVar = new Animal();
- The class/type of the reference variable
- The name of the reference variable
- 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.
Inheritance
- 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
When a Class is inherited from is called
base, super, or parent class
When a Class that inherits is called
derived, sub or child class
Object Polymorphism
means many forms and is The ability for An object to be considered an instance of every class in its inheritance chain*
Method Overriding
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
Method Overloading
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
C# Inheritance
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
C# Inheritance- multiple inheritance is not supported by C sharp
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
Polymorphism
is the ability for some code structures in an OOP language to be treated as different structures at runtime.
Casting
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