Categories


Authors

Java & C++ Guide

Java & C++ Guide

Java-and-CPP-Guide.png

Overview

  • Classes

  • Inheritance

  • Collections & Containers

  • Multithreading

  • Miscellaneous

 

 

 

 

OOP-Classes.jpg

Classes

Static Members

Static members have class scope as opposed to object scope

 

Classes Defined within Other Constructs

Nested-and-Local-Classes.png

Pass-by-value & Pass-by-reference

Many argue that Java is ONLY pass-by-value, but I'd argue that it's much more nuanced than that.
Best explained by the following examples:

Inheritance

Inheritance vs Composition
 

Inheritance-vs-Composition.png

Overriding vs Overloading

  • An overridden method has the same name and arguments as its base method.

  • An overloaded method has the same name but different arguments and does not rely on inheritance.

  • Two methods with the same name and arguments but different return type are illegal.

Polymorphism

  • The ability for objects of different classes related by inheritance to respond differently to the same method call

  • Enabled by C++ virtual methods. Java methods are virtual by default.

Example:
+ base class Shape with area as an abstract method
+ two derived classes, Square and Circle, implement area methods
+ Shape reference points to Square and area is invoked

Outcast Downcasting

  • Beware of using "downcasting" - Downcasting is casting down the inheritance hierarchy from a base class to a subclass (i.e. opposite of polymorphism)

  • Java Example:
    if(mySubClass instanceof SubClass) {
    SubClass mySubClass = (SubClass)someBaseClass;
    mySubClass.nonInheritedMethod();
    }

  • Use polymorphism & overriding instead of instanceof & downcasting

Order of Construction/Destruction

Order-of-Construction.png

Object Cleanup

In C++, it's a good idea to declare a destructor as virtual to ensure that the subclass' destructor will be called if the base-class pointer is deleted

In Java, a finalize method is similar a destructor in C++; however, finalizers are unpredictable (they rely on GC).  Best practice - use a "close" method to explicitly cleanup 

protected void close() {
try {
// do subclass cleanup
}
finally {
isClosed = true;
super.close();
}
}

protected void finalize() {
try {
if(!isClosed) close();
}
finally {
super.finalize();
}
}

Abstract Methods & Classes

Abstract-Methods-and-Classes.png

Accessibility Modifiers

OOP-Accessibility-Modifiers.png

The Dreaded Diamond Problem

The-Dreaded-Diamond-Problem.png

an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?[*]

 

 

java.lang.Object class

All classes inherit, either implicitly or explicitly, from the Object class.  Any reference can be cast to the Object type.

Java Collections & C++ Containers

Flowcharts

Multithreading

Semaphore

Brilliant explanation from Stackoverflow:

Think of semaphores as bouncers at a nightclub. There are a dedicated number of people that are allowed in the club at once. If the club is full no one is allowed to enter, but as soon as one person leaves another person might enter.
It's simply a way to limit the number of consumers for a specific resource. For example, to limit the number of simultaneous calls to a database in an application.

Mutex

  • A mutex is a semaphore of 1 (i.e. only one thread at a time)

  • Using the nightclub metaphor, think of a mutex in terms of a bathroom stall in the nightclub. Only one occupant allowed at a time… well, in theory anyway.

Mutex Example

 

Deadlock

Deadlock-Example.png

How to Avoid Deadlocks

  • If using multiple locks, always require locks in the same order

  • Minimize the use of locks

  • Minimize code between lock and unlock

Miscellaneous

Integer Types

* Win64 API is only 32 bit     Lots more C++ types

* Win64 API is only 32 bit

Lots more C++ types

vtable

Compiler creates a vtable for classes that contain virtual functions.  At runtime, the vtable is used to invoke the appropriate virtual function implementation.

Permutations

Permutations

Cord Cutting

Cord Cutting