| Start | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Static Class Members Static members have class scope as opposed to object scope C++ Example (opens new window)Java Example (opens new window) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Classes Defined within Other Constructs Defined within Another ClassC++Nested Class[ref] (opens new window) (needs a reference to enclosing class) Java[non-static] Nested Class (aka Inner Class or Member Class) Statically Defined within Another ClassC++Static Nested Class JavaStatic Nested Class (aka Static Member Class)[ref] (opens new window) Defined within a Method(e.g. event handling) C++Local Class[ref] (opens new window) JavaLocal Class[ref] (opens new window) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pass-by-value & Pass-by-reference Many argue that Java is ONLY pass-by-value, but it's more nuanced than that. Compare the following C++ and Java examples to see the many flavors of pass-by-value (aka copy) and pass-by-reference (aka alias). C++ Example (complete code) (opens new window)Java Example (complete code) (opens new window) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Inheritance vs Composition C++ & Java are both object-oriented languages, thus the following diagram applies to both. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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). In general, use polymorphism & overriding instead of instanceof & downcasting. C++ ExampleJava Example |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Abstract Methods & Classes Abstract Methoddeclared without an implementation C++pure virtual method Javaabstract method Abstract Classcannot be instantiated C++cannot be instantiated; has at least 1 pure virtual method Javacannot be instantiated; can have non-abstract methods Interfaceno instance fields C++nothing comparable to Java Javavery similar to abstract class, but 1) supports multiple inheritance; 2) no instance fields RemarksClasses Defined within Other Constructs#Defined within Another ClassC++Nested Class[ref] (opens new window) (needs a reference to enclosing class) Java[non-static] Nested Class (aka Inner Class or Member Class) Statically Defined within Another ClassC++Static Nested Class JavaStatic Nested Class (aka Static Member Class)[ref] (opens new window) Defined within a Method(e.g. event handling) C++Local Class[ref] (opens new window) See also Lambda expressions (opens new window) JavaLocal Class[ref] (opens new window) Overriding vs OverloadingThe following Overriding vs Overloading points apply to both C++ and Java: PolymorphismPolymorphism is the ability for objects of different classes related by inheritance to respond differently to the same method call. Here's an example:
In C++, polymorphism is enabled by virtual methods. In Java, methods are virtual by default. Order of Construction/DestructionObject CleanupIn 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. Abstract Methods & Classes
Accessibility Modifiers
C++ Friend ExampleThe Dreaded Diamond ProblemThe diamond problem is 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? (from [Wikipedia](https://en.wikipedia.org/wiki/Multiple_inheritance)) While C++ has always been susceptible to the diamond problem, Java was susceptible until Java 8. Originally, Java didn't support multiple inheritance, but with the advent of default interface methods, Java classes can not inherit "implementation" from more than one class. java.lang.Object ClassIn Java all classes inherit, either implicitly or explicitly, from the Object class. Any Java reference can be cast to the Object type. C++ doesn't have a comparable "Object" class. Java Collections & C++ ContainersJava Collections are symonymous with C++ Containers. Java Collections FlowchartC++ Containers FlowchartInteger Types
|
Read Article
https://develop.horje.com/learn/1434/reference