Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9e89c1

Browse files
committed
polymorphism chapter: discussion
1 parent 72d2ea3 commit e9e89c1

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
lines changed

‎book/copyright.tex‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,18 @@ \chapter*{Thanks}
110110
Editing:
111111
\begin{itemize}
112112
\item \LaTeX: a document preparation system
113+
\item Atom: Source code editor.
113114
\item TeXstudio: Text editor for Latex
114115
\item KDE Neon: Linux system based on Ubuntu
115116
\end{itemize}
116117

118+
Hosting and version control:
119+
\begin{itemize}
120+
\item Git: a version-control system
121+
\item Github: a web-based hosting service for version control using Git.
122+
\end{itemize}
123+
124+
And, of course, all the readers.
117125

118126
% ===================================================
119127
% VERSIONS

‎book/polymorphism.tex‎

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,158 @@ \subsection{Ruby}
620620

621621
\lstinputlisting[language={[KB]Ruby}, linerange={3-18}, style=codeStyle]{../codes/ruby/polymorphism/overriding.rb}
622622

623-
623+
\begin{discussion}
624+
625+
Polymorphism allows us to process objects differently depending on their data type or class.
626+
Based on type compatibility and equivalence, three type systems can be determined:
627+
\begin{itemize}
628+
\item \textbf{Nominal type system}: The compatibility and equivalence of data types is is determined by explicit declarations or name.
629+
Example: C++, C\#, Java.
630+
\item \textbf{Structural type system}: The compatibility and equivalence of data types is is determined by their structures or definitions: if they have the same definition then they are equivalent.
631+
Example: OCaml, Scala, Go.
632+
\item \textbf{Duck typing}: The compatibility is determined by the presence of certain methods and fields.
633+
``\textit{If it walks like a duck and talks like a duck, it must be a duck}".
634+
Example: Javascript, Python, Ruby.
635+
\end{itemize}
636+
637+
Table~\ref{tab-polymorphism} represents a comparison between some OOP languages based on their polymorphism capacities:
638+
\begin{itemize}
639+
\item \textbf{Type compatibility}: Nominal, structural or duck.
640+
\item \textbf{Get object type}: How to get the type of a given object.
641+
\item \textbf{Is instance of}: Verify if an object is generated from a class/prototype.
642+
\item \textbf{Members existence}: Check if a member (field or method) exists in a given object.
643+
\item \textbf{Type casts}: Downcasting is the act of changing a reference of a base class to one of its derived class.
644+
\item \textbf{Overloading}: Having methods overloading or not.
645+
\end{itemize}
646+
As for overriding, no need to compare since all of our languages afford this property.
647+
648+
\begin{landscape}
649+
\extrarowsep = 0pt
650+
651+
% \doublerawsep = 1.5pt
652+
\begin{longtabu} to \linewidth %
653+
{
654+
X[1,p]|[5pt white]
655+
X[0.75,p]|[5pt white]
656+
X[2,p]|[5pt white]
657+
X[2,p]|[5pt white]
658+
X[2,p]|[5pt white]
659+
X[2,p]|[5pt white]
660+
X[0.5,p]|[5pt white]
661+
} %{llllllll}
662+
% \begin{longtabu} {p{1cm}p{1cm}p{5cm}p{1cm}p{1cm}p{2cm}p{2cm}p{1cm}}
663+
\caption{Polymorphism comparison}%
664+
\label{tab-polymorphism}\\
665+
666+
% \hline\hline
667+
\rowcolor{indigo}
668+
\rowfont{\bfseries\color{white}}
669+
{Language} &
670+
{Type compatibility} &
671+
{Object's type} &
672+
{Instance of} &
673+
{members existence} &
674+
{cast} &
675+
{overloading}\\
676+
% \hline\hline
677+
&&&&&&\\
678+
\endfirsthead
679+
680+
% \hline\hline
681+
\rowcolor{indigo}
682+
\rowfont{\bfseries\color{white}}
683+
{Language} &
684+
{Type system} &
685+
{Object's type} &
686+
{Instance of} &
687+
{members existence} &
688+
{cast} &
689+
{overloading}\\
690+
% \hline\hline
691+
&&&&&&\\
692+
\endhead
693+
694+
\taburowcolors{indigo!20!white .. black!10!white}
695+
696+
{\bfseries\color{indigo}C++} & %Language
697+
Nominal& %type system
698+
typeid(obj)& %object's type
699+
Using cast& %Instance of
700+
/ & %members existance
701+
(Cls) obj; \newline
702+
static\_cast<Cls*> (ptr) \newline
703+
dynamic\_cast<Cls*> (ptr) & %cast
704+
Yes\\%overloading
705+
% \hline
706+
707+
{\bfseries\color{indigo}Java} & %Language
708+
Nominal& %Type system
709+
obj.getClass()& %object's type
710+
obj instanceof Cls& %Instance of
711+
using reflection & %member existance
712+
(Cls) obj & %cast
713+
Yes\\%overloading
714+
% \hline
715+
716+
{\bfseries\color{indigo}Javascript} & %Language
717+
Duck& %Type system
718+
Type: typeof obj \newline Class: obj.constructor.name& %object's type
719+
obj instanceof Cls& %instance of
720+
obj.hasOwnProperty ("property")& %member existance
721+
/& %cast
722+
/\\%overloading
723+
% \hline
724+
725+
{\bfseries\color{indigo}Lua} & %Language
726+
Duck& %Type system
727+
Type: type(obj) \newline Class: /& %object's type
728+
/& %Instance of
729+
access and verify type& %Member existance
730+
/& %cast
731+
/\\%overloading
732+
% \hline
733+
734+
{\bfseries\color{indigo}Perl} & %Language
735+
& %Type system
736+
ref(obj)& %Object's type
737+
obj->isa(Cls)& % instance of
738+
obj->can("method")& %member existance
739+
/& % cast
740+
/\\%overloading
741+
% \hline
742+
743+
{\bfseries\color{indigo}PHP} & %Language
744+
Duck?& %Type system
745+
Type: gettype(obj) \newline Class: get\_class(obj) & %object's type
746+
obj instanceof Cls \newline is\_a(obj, "Cls") & % Instance of
747+
method\_exists(obj, "method") \newline property\_exists(obj, "field") & % member existance
748+
/ & %cast
749+
/ \\%overloading
750+
\hline
751+
752+
{\bfseries\color{indigo}Python} & %Language
753+
Duck& %Type system
754+
type(obj) \newline obj.\_\_class\_\_& %Object's type
755+
isinstance(obj, Cls)& % instance of
756+
hasattr(obj, "property")& %member existance
757+
/& %cast
758+
/\\%oveloading
759+
% \hline
760+
761+
{\bfseries\color{indigo}Ruby} & %Language
762+
Duck& %Type system
763+
obj.class& %Object's type
764+
obj.is\_a?(Cls) \newline instance\_of?(Cls) \newline kind\_of?(Cls)& %instance of
765+
obj.class.method\_defined? :method & %member existance
766+
/& %cast
767+
/\\%overloading
768+
% \hline
769+
770+
% \hline\hline
771+
\end{longtabu}
772+
\end{landscape}
773+
774+
\end{discussion}
624775

625776
%=====================================================================
626777
\ifx\wholebook\relax\else

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /