Friday, December 29, 2017

Session 8 - GSLC

Session 8
Saturday, 25 November 2017

Today is GSLC session. Miss Yanfi gave us some questions to answer on the binusmaya's forum.
Here are the questions:

1. What are the similarities of and the differences between Java Packages and C++ Namespace?
2. Explain the dangers of C's approach to encapsulation.

And these are my answers that i wrote on the forum:

1. Similarity : 
- Java uses packages to group classes. C++ uses namespace to group related function and classes. 
- Java package and c++ namespace both make the code cleaner and less redundant. 
- Java package and c++ namespace both provide breakup for class names. 
- Java package and c++ namespace are both abstract container for classes. 

Difference : 
- Java package cant be nested. c++ namespace can be nested. 
- C++ namespace are just used for partitioning names to avoid collisions. java package are used for modules, the naming aspect is just a part of it. 
- In java you can apply various access specifiers to classes which will have impact on your package. these type of access specifiers are not applicable to namespace in C++. 

2. The main problem is that the biggest part of encapsulation is done via hiding, rather than protection. This is achieved through definition hiding: a header file is preprocessed (which is a synonym for copy-pasted) into the implementation file. Anyone with this header file will be able to access any method or public variable of a the client related to the header, left apart any "static" method / variable. The documentation of the dependence of the client program on the library (and its header file) is lost. The author of the library could change the header file and the implementation file, but the client could attempt to use the new implementation file (not realizing it had changed) but with the old header file, which the user had copied into his or her client program.

No comments:

Post a Comment