OOP Fundementals

Ace your homework & exams now with Quizwiz!

Upcasting

Assigning an object of a derived class to a variable of an ancestor class The process of storing a more specific value in a less specific type example (Java): To continue using Automobiles and Sedans, this is an example of upcasting: Automobile car = new Sedan();

Why is the Distinction Between Relationships (i.e inheritance vs composition vs aggregation) Important?

Besides being a common and basic interview question, understanding the differences between these relationships can inform how you design your code. If you are writing code to simulate the operation of a car. Does a car have an engine, or is an engine a subtype of car? When the answer is obvious, it can prevent you from making mistakes with your design. If a Car has-an Engine, a FuelTank, and other such structures, are these structures subtypes of some concept? For example, are an Engine object and a FuelTank object each more specific examples of a generic CarPart structure? But sometimes, the answer to these types of questions isn't obvious, or is in fact driven by a business decision. Imagine you are writing software for a a social media platform: Does a post have comments, or are comments a type of post? Depending on how you answer this question, you wind up with Facebook or Twitter. Two different platforms that answered the same question in different ways.

casting (polymorphism)

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. Consider the Sedan : Automobile example for inheritance. The moveAutomobile method accepts any reference to an object that has an IS-A relationship to Automobile. The method treats the passed-in reference as if it were an Automobile type reference, i.e. it stores the reference in an Automobile reference variable to use within the method. But the underlying object is still actually a Sedan object, it is only being access through an Automobile type. This is called casting

Inheritance (example)

Consider a class "Chef". We can extend this class to create a class called "Italian Chef" as follows: class Italian Chef : Chef This new class "inherits" the functionality from the Chef class.

Specialization

Creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created. example: In the previous example for Generalization, we saw that the classes Piece of Luggage and Piece of Cargo shared similar properties that were placed in a superclass called Freight. When it comes to Specialization, if there is a property that is only applicable to a specific subclass, such as Degree of Hazardousness, that property is placed in the class Piece of Cargo where-in this class also has all the properties of the Freight class with the concept of Generalization.

access modifier

Defines the circumstances under which a method or class can be accessed; public access is the most liberal type of access.

reference variable (example)

Dog myDog = new Dog(); This creates a new Dog object, storing its address in a reference variable named myDog with type Dog. recall the "mask" analogy

Downcasting

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. For example: Automobile car = new Sedan(); Sedan otherCar = (Sedan) car;

What will happen when a method in the derived class has the same signature as a method in the base class?

It will override the implementation given in the base class. That is when we create an instance (object) of the derived class and invoke this method for that object, the method as defined in the derived class will be called rather than the base class.

Encapsulation

Keeping details (like data and procedures) together in one part of a program so that programmers working on other parts of the program don't need to know about them. We can create a fully encapsulated class by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

Advantages of encapsulation

Maintainability Flexibility Extensibility Data hiding and security

Are Composition and Inheritance mutually exclusive?

No, consider this segment in the Java syntax: class Dog {} class Labrador extends Dog {} public class Person { Dog myDog = new Labrador(); } This is verging on an example of polymorphism, but recall that a reference variable is allowed to store the reference of an object that IS-AN instance of the reference variable's type. We say that a Person HAS-A Dog object, but that Dog-type reference variable refers to a Labrador object. This is okay, because a Labrador object IS-A Dog object too.

read only and write only

Read and Write Properties: When property contains both get and set methods. Read-Only Properties: When property contains only get method. Write Only Properties: When property contains only set method. Auto Implemented Properties: When there is no additional logic in the property accessors and it introduce in C# 3.0.

Polymorphism (objects)

Refers to the Is-A relationship created by two objects through inheritance. Polymorphism refers to database structures that send the same command to different child objects that can produce different results depending on their family hierarchical tree structure.

Constructors

Special methods used for the creation of an Object. Note that the constructor name must match the class name, and it cannot have a return type (like void or int). Also note that the constructor is called when the object is created. All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields. Example:

reference variable

Stores (points to) the memory address of an object as opposed to a value. Reference Variables use a class as a type.

Polymorphism (example)

Suppose a "Sedan" inherits from the class "Automobile", then since a Sedan object is also an Automobile object, the moveAutomobile() method in the Automobile class can be used for the Sedan subclass.

Composition (HAS-A) Example

Take a look at the following snippet: (Java) public class Dog {} public class Person { Dog myDog = new Dog(); } Based on the above, you'll see that Person has an reference variable myDog that points to an object that is an instance of class Dog. Considering this, we say that class Person HAS-A class Dog. Thus a Reference variable creates a HAS-A relationship

Composition vs Inheritance

Technically, if Object B cannot exist without Object A, (if our program design indicates that a Dog object can never be created independent of or outside of a Person object) it is an example of Composition. If a Person object has-a Dog object, but our application also creates independent Dog objects elsewhere (like wild Dogs), then this relationship is called Aggregation. Generally, you would be forgiven for referring to both as Composition, but there are times when clarity is important.

Inheritance

The process of extending a broader class (i.e. base/parent class) to a more specific class.

Generalization

The process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes, associations, or methods. Example: The classes Piece of Luggage and Piece of Cargo partially share the same attributes. From a domain perspective, the two classes are also very similar. During generalization, the shared characteristics are combined and used to create a new superclass Freight . Piece of Luggage and Piece of Cargo become subclasses of the class Freight. Therefore the properties that are common to the classes Piece of Luggage and Piece of Cargo are placed in the superclass Freight - Identification, Weight and ID-Number are those properties.

Abstraction

The process of hiding the implementation details and showing only functionality to the user. That is showing only essential things to the user and hiding the internal details. Abstraction lets you focus on what the object does instead of how it does it. example: sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.

namespace

The set of variable and function names (defined by classes) that have been reserved by the compiler/interpreter. This allows you to organize code and create unique class names.

method signature

The signature is how methods are differentiated by one another in most OOP languages. It is a combination of the method's name, and the types, number and order of parameters that are passed in. This system of differentiation allows us to override and overload methods.

using System;

The using keyword imports a namespace, and a namespace is a collection of classes.

Method Overloading

This happens 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. We do this because if we have to perform only one operation (see example), having same name of the methods increases the readability of the program. Example: Consider a simple calculator that adds two numbers. Rather than name our methods differently (i.e. PlusDouble, PlusInt, etc.) we can give them the same name but with different types of parameters. C# Allows is to do this.

Console.ReadKey()

This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

Method Overriding

When a child class overwrites a method inherited from a parent class. example: Suppose class "Animal" contains a method Animal.eats() which returns string "I eat food". We can modify this method in the child class "Dog" so it can return string "I eat kibble" when we call method Dog.eats().

Main method

Where execution starts in a C# program. public static void main(String[] args)

What happens when you create an object in memory with the "new" keyword?

You store the reference to that object in a reference variable that uses that object's class as a type. That is objects are stored in heap memory, and their addresses are stored in reference variables. Consider: dog myDog = new dog ( ) Here we created a dog object by using the new keyword to invoke the constructor of the dog class--and we are storing the reference in a variable of type dog named myDog.

object

a particular instance of a class

Method overriding is sometimes called:

runtime polymorphism, because the implementation of the method that will be used is determined by the type of the object being referenced at runtime.

class

A blueprint for creating objects, providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). The user-defined objects are created using the class keyword.

abstract class

A class that only exists in a model so subclasses can inherit from it--it can therefore never be instantiated. Example: Consider a bookstore where every book is of some type (novel, magazine, comic, etc.) For organizational purposes it is useful to have a Book class even though it cannot be instantiated. Some points to remember : An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method.

abstract method

A method that is specified but not implemented in an abstract class. The subclasses must implement this method. Thus an abstract method has no body as it is only provided in the subclass. Example: Recall our Book class example. An example of an abstract method for this class would be "identifier". (We would public void as a type?)

Ways to achieve abstraction in Java

Abstract class (0 to 100%) Interface (100%)

4 pillars of OOP

Abstraction Polymorphism Inheritance Encapsulation

Abstraction & Encapsulation

Abstraction and Encapsulation are two more core concepts of Object Oriented Programming. Abstraction is the principle of "using simple things to represent complex things." We often shorthand this as "data hiding," because the principle is the same: when you break your code into objects, the user (whether it's an actual user or another developer using your code to write their own programs) doesn't need to know how something works, they just need to know what it does, and what the inputs and outputs are. Encapsulation is how you go about restricting access to your abstracted code, by using access modifiers. You've seen these around in multiple examples, and most languages have at least these three: public: the code is visible to all other classes protected: the code is only visible within the current class, or any derived/child classes private: the code is visible only within the current class.

abstraction vs encapsulation

Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it's inside view, where the behavior of the abstraction is implemented.


Related study sets

H. 17B Final Multiple Choice: Cold War

View Set

Chapter 40: Musculoskeletal Care Modalities

View Set

Chapter 1 understanding Our Environmental

View Set

Topic 9: Management Accounting and Cost Concepts

View Set

Principles of Management Practice Exam

View Set

SOCY 101 Exam 1 (Travis Williams VCU)

View Set