Difference Between Java SE and Java EE

Since the long time, Java developers have relied on both of these platforms to do the desired tasks through the language. Many newbie developers find it hard to understand the difference between Java SE and Java EE, and often end up considering them an entirely different programming languages… perhaps because they’ve learned a different language in the past which had no funny sounding distinctions within the language. If you’ve been reading my blog, then you know that I’ve been writing on Java, thus it makes sense to cover the commonly asked topic.

In this article, I will try to highlight the difference between Java SE and Java EE.

What is Java SE?

It is the name of the Java platform which provides the core tools.When Java was released, its first version was simply called “Java”, but when the 4th version was released in 1998, it was renamed as J2SE 1.2, wherein “J2” represents the Java 2, which then further replaced with Java SE as the previous name caused the confusion as to why J2SE had versions like 1.x, for instance, people assumed that J2SE 1.6 is an entirely different version. In Java SE, you are provided with the necessary tools to compile the Java programs with the help of different APIs, which is then run through the JVM (Java Virtual Machine). Apart from simple building tools, it also has GUI libraries / framework for building sophisticated GUI powered applications.

In the Java community, Java SE is also known as “Core Java” unofficially. According to the official site: “When most people think of the Java programming language, they think of the Java SE API. Java SE’s API provides the core functionality of the Java programming language. It defines everything from the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.”. Its latest version is Java 8.

What is Java EE?

It is the super-set of Java SE, meaning it has all the features of Java EE. It is the Enterprise Edition, which basically means that it has all the features that are specifically added for the development of giant Enterprise applications. The cool thing about Java EE is that you do not need to compile your Java code to a stand-alone executable, as the platforms provide the built-in environment to compile / run the code without the need of a stand-alone executable. This results in extremely fast software development cycle. Java EE has a web framework based upon Servlets (i.e., a class used to extend the capabilities of servers). Apart from it, JSP (Java Server Pages) is provided which is a template language that compiles from JSP to a Java Servlets where it can be run by the container (the environment of Java EE). Other features include JPA, JDBC, etc.

Through Java EE, you can develop web server applications, although you need Java EE compatible servers like GlassFish, JBoss, WildFly, etc., so Java EE is more of a “platform” than simple tools. According to the official site: “The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.”

In Java SE, the applications that you write can run as stand-alone Java programs or as Applets. In Java EE, you can still run the program as stand-alone, but it provides the facility to run the program within the Java EE container.

…And what about Java ME and JavaFX?

Java ME was designed for applications which run on resource constrained devices (small scale devices) like cell phones, for example games.
JavaFX is a platform for building robust GUI applications. It is actually intended to replace Swing as the standard GUI library for Java SE, however, both will be included for the foreseeable future.

The 5 Best IDE for Java

When we learn a programming language, we are tempted to start wondering the tools (including the best IDE) we are going to use for writing the code. Some programmers download the text editors which provide enough satisfaction to them due to their simplicity plus often text editors are specifically designed for writing code, while the other newbie programmers download the IDE (Integrated Development Environment) which provides all the tools required for compiling the code for a single or many programming languages.

As Java is a language with huge libraries, it makes sense to write programs through an IDE. Several vendors in the past have provided many IDEs. Some of them became popular, others remain in the shadows. Following are the carefully selected IDEs that I think are one of the best, if not the best available for writing programs in Java:

1) Eclipse

Eclipse has been the part of the Java community for a very long-time, and it’s probably considered the most extensive IDE for Java. Thanks to its plugin system, you can easily integrate new functionalities with just a click. It also provides auto-update system, which notifies you when new updates are available. Though Eclipse, you can create applications for several platforms, including desktop, web, mobile and enterprise. Along with tools to compile / run Java code, it also supports various other features including the integration with Maven, a nice-looking XML editor, Git client, CVS client, and most importantly: Windows Builder.

Windows Builder supports designing the GUI through the different tools, which can help you in designing the powerful GUI for your software. It is essentially a SWT and Swing Designer. The cool thing about it is that you do not need to use it to compile the code, as the code that it generates can be compiled with normal Java libraries.

2) NetBeans

It is an open-source IDE for Java which is written in Java itself. It provides the development of Java SE, JavaFX, Java ME, web, EJB and mobile application out-of-the-box. Like Eclipse, it is modular in design and provide the plugins feature, you can extend the functionality easily through third-party plugins. Although, NetBeans was developed for building Java code, it now also supports C/C++, PHP, HTML, etc. natively, and any language can be added through plugin. Thus, this IDE is recommended for those Java developers who might want to learn other programming languages as well, so they won’t need to change the IDE (in case they love the IDE!).

3) IntelliJ IDEA Community Edition

This IDE was designed for Android app development, Scala, Groovy, Java SE and Java programming. It is extremely lightweight and provided only minimalist features, including JUnit testing, TestNG, debugging, code inspections, code completion, support for multiple refactoring. But, as it’s a Community edition, meaning you need to buy the full version to unlock the remaining features.

4) Enide Studio 2015

It is a tool suite which is available from the Eclipse plugin store and from the main website. It is a component of Nodeclipse which is a set of third-party developer solutions for Eclipse for programming in JavaScript, CoffeeScript with focus on Node.js. You can either install the plugin if you have Eclipse already installed, or you can download the Enide Studio directly from the website.

5) Android Studio

This is platform from Google which was originally designed to develop Android applications, however, it can be used for other programming languages as well. Before its arrival, Eclipse was considered a standard IDE for Android / Java development. With its arrival, it is now regarded as the best platform for Android / Java development due to the support from Google which updates its features regularly.

Conclusion

There is no such thing as the “best” IDE, as the choice depends on the needs of the user. For most, a simple text editor is enough, as they don’t need to code completion and other bloating features which slow down the development process.

The History of ‘Hello, World!’ program

If you ever stumbled on any programming language tutorial regardless of your background, you certainly have already seen the “Hello, World!” written somewhere in the tutorials. It is considered a standard program for introducing a programming language to the students, wherein it simply prints “Hello, World!” to the console screen. It was first introduced in the introductory book on C: The C Programming Language which was written by Brian Kernighan and Dennis Ritchie. It was written as “hello, world” (without capital letters or exclamation mark) in the book. As the book was considered the best introduction to the programming language (C; which was hugely popular in the era), since then it is used as the first program to introduce the basic details of a programming language whenever a programming language is being taught to students.

The above program was written for the original version of C, which was translated from a program written in A Tutorial Introduction to the Language B By Kernighan for illustrating external variables:

In modern C, the program can be written as follows:

Due to the rise in popularity of the “Hello, World!” program, many modern programming languages incorporate the program in a similar fashion, but with the addition of the sophistication or the style of the language. For instance, in Python you can write the program in a single line:

Overall, the “Hello, World!” program introduced a topic of interest to the programming enthusiasts who like to figure “everything” out.

Compiled vs Interpreted Languages – Which ones are better?

Imagine a guy who just started programming. He searched the google for the articles on his “favorite” programming — the choice he made subconsciously, because his friends told him so. He found people talking about the programming, but he misunderstood things, including “compiled”, “interpreted”, etc., because his private language (the language he speaks) has different meanings of the words written there. To reduce the complexity in another newbie programmer, I am writing a short tutorial to highlight the difference between compiled and interpreted languages.

Compiled vs Interpreted Languages

Compiled languages are those in which the code is compiled line-by-line, which means that a compiler tries to understand every line of code in a program (including different files within a single project), and then translates it into the machine language of the architecture, which often happens after it’s translated to assembly instructions in .obj files – also known as object files, which are then linked together to create an executable file which a user can run on a target machine.

Interpreted languages, on the hand, are the ones that translates the code to the bytecode (i.e., the code format that is understood by the virtual machines) line-by-line on the fly, which means that it will start interpreting the code from the start and then execute the results through virtual machines, which could be seen through the console (in the form of logs or actual results). Virtual machines are provided by the vendors of the language to convert the bytecode to the machine language of the target machine.

Many programming languages are both interpreted and compiled, including Java, C#, Python, etc. These are called “bytecompiled” languages. In these languages, the code is compiled by the compiler to the bytecode of the language which can be run on the fly through the Virtual Machines (VMs) that are specifically developed for translating the code to platform dependent machine code on the fly. So, for instance, if you have different .class files which are basically Java bytecode that are independent of the architecture, then the JVM (which should be installed on the target machine) can convert the .class files in a real-time to the machine language of the computer architecture on which the program is running (i.e., x86, x64).

Which ones are faster?

As compiled languages need to only compile for once, the compiler make sure to add every kinds of optimization for the particular machine on which the program is compiling. This results in the faster execution of program on the average with the cost of slow compile-build-run process, as you would need to compile the whole program when you change a single-line of code, though modern compilers are getting better in recognizing the areas that need to be recompiled through carefully investigating the object files (.obj).
Interpreted languages, on the hand, require a two-step process: first the compiler compiles the whole program into the bytecode. Now, a compiler doesn’t know anything about the machine on which the program will run, so the bytecode which is produced by the compiler is platform neutral or independent. Once the bytecode is available, it is then run by the virtual machines installed on the target machine. At this stage, a virtual machine can optimize the bytecode as per the machine specifications, but the optimization isn’t very good as compared to the compiler in the compiled languages.

Which ones are better?

I don’t think there is a conclusive answer to the question. These days interpreted languages are quite in-demand due to the fact that virtual machines have been greatly optimized since the past few years (due to the addition of JIT techniques) plus the average users are using the modern computers that can run the code through virtual machine in almost the same speed as if it is written in a compiled language.
Most interpreted languages can be used interactively where you can run the code on the fly through the shell line-by-line (REPL). For instance, Python provides an interactive shell through which you can type the function or statement to do something (such as, printing the number, adding the numbers, etc.), and it will work instantly and give the results to the console, even on the website. There are few compiled languages that can run interactively, such as, Clojure (Lisp), Haskell, etc. Interpreted languages can also be very convenient for dynamic typed languages.
Please note that compilation or interpretation is a matter of implementation, not the language itself. Most often than not, many languages considered “interpreted” also provides a compiler to translate the code to machine language. But, as newbie programmers tend to search “compiled vs interpreted languages”, we were forced to use such title for our tutorial.
If you have any question, then you can ask in the comment section below!

Procedural Programming vs Object-Oriented Programming – Which would win the battle?

Programming languages comes in a different flavors through different programming paradigms which provide a particular style to write the code. The style could include syntax, features, indenting, etc. Two of the most used programming paradigms are procedural and object-oriented. In the recent development of programming languages, the object-oriented style pretty much took over the old procedural style that was dominating in the early programming days.

In this tutorial, I am going to show you the difference between these two paradigms, and the pros / cons of these styles. Let’s get started!

Procedural Programming

Procedural programming is the part of imperative programming. In imperative programming, we tell the computer “how to do” rather than “what to do”. In procedural programming, we use statements to change the state of the program. Statements include the lines of code which are instructions telling the machine “how” something should be done. As we would need to write hundreds of code to perform the tasks, sometimes it becomes necessary to carefully select those lines of code or statements that are used excessively within a same program and then copy it somewhere and then paste on those places where they are required. Thankfully, procedural programming provides procedures, also knows as subroutines or functions, which can be used to hold the commonly repeated lines of code, and then use it whenever you require it in your programs.

What are procedures?

Procedures are routines / functions that holds several lines of code, which can be placed whenever you need to do something instead of writing the same lines of code again and again. Thus, it’s a way to organize the code to reduce duplication. Procedures can be called in other procedures, and even in themselves. In procedural programming, we create / define different procedures (i.e., printName, changeColor, etc.) and call them whenever we like to perform a required task.

Following is the example of the program in C, which is enough to show the procedural programming style:


Pros

  • Easier to learn the programming language
  • Encourages coding in a straight-forward way
  • Can be a good start for the newbie programmers

Cons

  • Hard to organize code
  • Looks outdated
  • Hard to add the support of new features
  • Lack flexibility

Object-oriented Programming

Object-oriented programming came later in the programming community. It is often contrasted with procedural programming, because here we deal with the objects that are created through a class — a sort of a blueprint which holds different methods / attributes — which then are manipulated and used with each other to change the state of the program. It emphasis objects rather than “actions” and data rather than “logic”.

What are objects?

When we say “object-oriented programming”, we mean that our programming revolve around the objects. An object is an entity that is created through the use of a class, so we need to first create a class for a particular object (i.e., apple, rectangle, car, etc.). It is sort of a variable or an identifier in other programming languages, however, the difference here is that it holds properties of a class (through which it is created) and the methods (also called “fields”; “functions” in procedural programming) of a class to manipulate the other objects or itself. Thus, through manipulation of the different objects, we change the state of the program.

Following is the example of a program written in Java, which can be considered a beginner example of OOP:

Here, we’ve created two classes: PrintNumber and Main. In Java, the creation of at least one class is necessary to do anything, therefore, we used the Main class as the start point of the program. Then, we’ve created PrintNumber class through which we’ve created an object named “num”. Then, we called a method “print” through “num” object, thus manipulating it.

Apart from simple classes to manipulate objects, OOP also provides several features to build relationship between objects, and to further facilitate the organization of code. These features include inheritance, polymorphism, templates, etc.

Pros

  • Encourages organization of code
  • Easier for other developers to build based on previous code
  • Very flexible to create modules

Cons

  • Slow initial startup; need to write boilerplate code even for small tasks.
  • Takes more lines of code for simple programs; need to create classes for everything
  • Often feels bloated in features

Conclusion

In this tutorial, I have highlighted the difference between procedural and object-oriented programming. I would argue that it’s not good to debate about the importance of either of these styles, as they both provide enough features and rules for building certain kinds of applications. Procedural programming proponents use this style for simplicity (because they love to simplify the code!) and the fact that they don’t like bloated (or extra) features that comes with object-oriented programming, so they resort to programming in the old ways. Object-oriented programming proponents use this style for the better code management (in case they want other people to work on their projects!) and their expectation of the growth of their applications force them to think in a long-run.
If you have any question that you think I’ve missed (unfortunately) in the article, then you ask in the comment section below!

Beginner Introduction to Java — the “Programming Language of 2015”

 In the last article, I have highlighted the TIOBE Index’s positions, where Java got the 1st position. Believing the importance of the aforementioned measure of the programming languages popularity and the fact that I promised the readers to focus more on the Java this year, I have decided to write an introductory tutorial on the Java programming language. The whole purpose of this tutorial is to have a friendly easy-to-read guide that can be later referenced by the experienced programmers to their friends who have recently started programming, so they would be reading this guide without resorting to a complex introduction.

Java was developed specifically for the object-oriented programming language. To enforce this paradigm, the Java developers made sure to not support any feature that usually resides in other programming paradigms (i.e., procedural, functional, etc.), thus discouraging the use of other programming styles. It was originally developed by James Gosling when he worked at Sun Microsystems (which is now owned by the Oracle Corporation). It was released as the core component of the Java platform. Before its release, C++ was very popular due its then recently introduced object-oriented features (i.e., classes, inheritance, etc.). Java was specifically designed for running on a virtual-machine, because the developers thought that the code written in it should be compatible to every platform and not just a specific platform where the code is originally written. In C++, when you compile a code, then the executable file can only be run in a particular format where it was compiled. Now, let’s talk about the Java Virtual Machine (JVM).

What is Java Virtual Machine? (JVM)

JVM is a virtual machine that was designed to run the Java bytecode — the instructions that are translated from the Java code by the Java compiler — in the platform where it is installed on the drive. The motivation for its construction is to support the cross-compatibility between different platforms (i.e., Windows, Linux, MacOS, etc.). The arrival of JVM made the language both a compiled language and an interpreted language, because: 1) The Java code (mostly written in .java format) is first “compiled” by the Java compiler into the Java bytecode (mostly written in .class format), then 2) The JVM interprets the bytecode (by translating it to the machine instructions of the platform) to run the program. Thus, this is a two-step process when you desire to compile / run a program written in Java.

Installing the Java Platform

Before getting your hands dirty, you need to first install the Java Platform (JDK + JRE) in order to compile / run the program written in Java.

Under Windows

 By following the instructions below, you would be able to install the Java platform on Windows:
  1. Go to the site to download Java SDK.
  2. Install the SDK once the download is completed.
  3. Go to the “Properties” of “Computer” or “My Computer”, then go to the “Environmental Variables” and then “Advanced System Settings”
  4. Edit the “Path” by adding the destination of the folder where the SDK is installed to the variable value (see the image below).
  5. Open the “cmd” and type “java” to test the installation.
downloading-the-sdk
Downloading the SDK
environmental-variable
Environmental Variables
testing-installation
Testing the installation through “cmd”

Under Linux

Most Linux distributions come with Java pre-installed, so you simply need to open the terminal of your distribution, and write “java” to test the installation. In case it isn’t installed, then you can install by running the following command:
Now, let’s write a “Hello world” program in Java.

Hello Java!

Ok, I cheated: We are going to write a “Hello Java!” program instead of the traditional “Hello World” program! In Java, we need to define a class in order to do anything. Following is the code of the Java program that simply prints “Hello Java!” to the console:

Here, I defined a (public) class “Main” which contains “main” method which has the code to print “Hello Java!”. This code should be saved in a file “Main.java”, because the name of the public class and the file name (excluding .java) should be same according to the syntax rules.Once you have saved the code in “Main.java”, then you need to open command line from the directory of the source file (or change the directory of your command line to the required directory) and then run the following command to compile the code:

Here, the “javac” is a tool to compile the source code. Once the source code is compiled, it creates a Java bytecode file (with .class format) which is called a class of its source file. To run the class file, you use “java” tool (note that I omitted the .class). This will print “Hello Java!” to the console screen:

Beyond Hello Java!

Now that you’ve learned the basic “Hello Java!” program, you can read the other tutorials to learn more about this awesome language! If you find it cumbersome to compile / run Java code through command-line, then you can use the IDE (Integrated Development Environment) for Java.

Summary

This article was an introductory tutorial on the Java programming language. Consider it a good start into the complex field of Java programming language where you need to learn / study at every stages!

If you have any question, then you can ask in the comments section below!

Java popularity rises to the top – Awarded “Programming Language of 2015” by TIOBE Index

Java programming language has been around since 20 years, when it first Beta version was released in 1995, which then followed by the official release of JDK 1.0 in 1996. Due to popularity of newly emerged languages (in its era) providing the similar features yet vast in feature-set — often fixing the bugs of old languages, programmers speculated long-ago that Java would lose its popularity and will soon be remembered by the people through the reading of history on programming languages. But, behold: the famous programming language popularity measure TIOBE programming community index has confirmed (through their findings of different programming languages searched by users on different search engines, such as, Google, Yahoo!, Wikipedia, etc.) that Java has risen to the top and beaten every programming language, including the languages of similar flavors (i.e., OOP languages like C#) and other languages (C, Python, PHP, etc.), proving that not only the language is less likely to die sooner, but provide enough applications for other purpose (web, desktop, etc.) due to its massive improvement in the feature-set… thanks to the recent release of JDK 8.0 (including its updates) in 2014-2015.

In January 2015, Java’s position was 2nd, but due to its rapid increase in the ratings within a year, it reached the 1st position, thus get awarded as “Programming Language of 2015”. TIOBE has a long history of awarding programming languages every year based on its rise in popularity with how much margin. As Java got a rapid rise in ratings, it makes perfect sense to call it “Programming Language of 2015”, thus enforcing the likelihood of a increase in its usage in the current year for different kinds of applications.

It would be intriguing for you to see C programming language at the 2nd position, which remained in the 1st position for the long-time despite the fact that there are countless other programming languages. The reason for C high popularity is due to the fact that it is still used as a beginning language in schools (thus students search it on search engines) and its use can be extensively found in developing other programming languages / libraries, including the programming in embedded systems and even the operating systems (i.e., Linux) that are written entirely in C.

What can we learn from it?

Even though, the TIOBE Index may have flaws in its measure of popularity of programming languages, it nevertheless shows the greater focus on the development of Java, its strong user-base and huge collection of libraries, which tell us to focus on it and learn more about it, which would probably help us in getting more jobs in programming. As our motto of this website is to make you happy (and smile!) when you do programming / coding, we will therefore focus extensively on Java tutorials from now on, assuring our readers that by visiting / reading our blog, they can have a good understanding / insight on the languages to make them capable to face every challenges in software niche.

Generics In C# – Its Usage And How It Differs From C++ Templates And Java Generics

Generics allow a type or method to operate on objects of various types while providing compile-time type safety. It can be used to achieve parametric polymorphism – a way to make a language more expressive, while still maintaining full static type-safety. Through Generics, you can generically define a member/method in a class that is valid for all the related types, or you can pass any data type to a function whose parameter has any template. Templates are commonly considered as a compile-time feature. Unlike polymorphism, through interfaces (and inheritance), there is virtually no overhead (more on that later) in using templates, because they are normally turned into equivalent source code during compilation.
In this detailed tutorial, I will tell you how you can implement them, the benefits of this programming model and its difference to Java and C#. Let’s start the journey!

How To Implement Generics?

In my previous tutorial on operator-overloading, I gave you the example of “Vector3f”. Let us continue from where we left. Suppose we want a class for holding three-point vector values (x, y and z), that is generic and accepts any data type including integer, float and double. In order to do that, we need to create different classes for different data types. Fortunately, through generics feature, we can define only a single class with generic semantics that implements the same methods for all the data types as long as the operation on them makes sense. Let’s call our generic vector class as “Vector3“. We can define such class as follows:

The formula to declare our generic class is as follows:

Having that in mind, we can use our class in the program as follows:

As you can see, we can easily create an object of “Vector3“ of any data type that we want. This greatly reduces the need to create separate classes for different types.The above example is for generic class. Note, however, is that you can not use operators on generic data type, because at a time of compilation, a compiler doesn’t know whether those operations make sense. That is a limitation of generics in C# (which I will explain later). However, you can nevertheless overload operators in a class if you are sure that a programmer will use only those types that make sense.

Generic Method

Consider a simple method for printing what a programmer passed to a method to console. Now, for printing different data types (int, float, string etc.) you need to define different methods of the same name (function overloading).

You can see that the implementation is same for all the methods, but as C# is strongly-typed language, you can’t pass double in the parameter for “int”, so we had to define different methods. Fortunately, through generics, you need to only define a single method.

Generic Interface

An interface can be generic too. Consider the following example:

How Generics Work?

As I previously mentioned, Generics is the compile-time feature – meaning it works during compilation of a program. It transforms the class, interface, function etc. that has generic type, into equivalent source code with different class, interface, function etc. of data types that is used when creating an object.

During initialization of an object, compiler will transforms above class into equivalent classes. This will be transformed into the following:

However, note that in C#,  the types are substituted during compilation, so there may be a slight overhead.

Difference to Java Generics

In Java, there is a concept of generics too, which, although looks similar in look, have quite different implementation. Generics in Java provide compile-time safety for type-correctness, but is partially considered as a run-time feature and it is somewhat similar to inheritance-polymorphism in practice. In Java, there is a process called type erasure, through which, type information is removed during compilation and there is no way to tell what was the type of a generic when it was instantiated during run-time. This is considered as a restriction and many programmers are dissatisfied with it. So, any algorithm that requires to know the original type cannot be implemented through generics in Java. You can’t do things like:

Because there is no way to tell during run-time what was the type of “T”. As I said earlier, Java has type erasure, it actually has run-time cost because behind the scene Java’s compiler actually casts the type, something like the following:

See this for more information.

Difference to C++ Templates

In C++, templates are a pure compile-time feature, due to which it greatly increases the compilation speed. The resulting program is bigger in size as well due to the boilerplate code templates in C++ create during compilation. Templates in C++ are more powerful than C# and Java, but are very complex too. It gives a terrible compilation speed during source code transformation. Both provide support for parameterized types. C++ provide substitutions of types during compile time. In C#, however, substitutions are performed at runtime.

In C#, you can not call arithmetic operators on generic type, though you can call user defined operators. In C++, the following code is valid:

C# neither support explicit specialization nor partial specialization which makes Generics not useful in some cases. See this for more information.

Conclusion

Generics is the most useful feature, especially in libraries. Its usefulness can not be dismissed when creating a library for other programmers, considering the fact that is implemented in collections in C#. I find it easy to achieve polymorphism through generics than through the interface

If you have any question, then please ask in the comments section below!

Understanding Abstraction in C#

In Object-Oriented Programming (OOP), abstraction is one of three fundamental principles (along with encapsulation and inheritance) that defines the paradigm. Through the use of abstraction, you can hide the unwanted details of the underlying data about an object in order to reduce complexity and increase efficiency (in some cases, which I will mention later). If you have some background in the art, you will realize that it works in the same way. For instance, if you are sketching an apple, it is not desirable to sketch the unwanted details around the apple; the details of an apple is the only thing that really matters to you. In this way, an object that is created through a class represents the original, without unwanted details. You can refer the resulting object itself as an abstraction, i.e., a named entity that is made up of selected attributes and behavior that is specific to a particular usage of the originating entity.

Abstraction is related to both encapsulation and data hiding. Through this design, you can separate interface from implementation. It is often considered the most useful technique in data hiding. It is not only useful in the program for which the design is originally implemented, but in other programs too; if you are creating some library that is going to be used by other programmers, then it is a very useful design for an API (Application Programming Interface) as it completely hides the implementation from a programmer. The users of your library won’t be distracted with the details of methods that does the magic; they would simply use them in the way how you like them to be used

In this tutorial, I will make you understand the concepts of Abstraction and how this design can be implemented in your C# programs. Even though the source code examples that I will provide may be valid for Java (or C++) too, I’m just focusing on C#. So, let’s get started!

Interfaces On The Rescue

Suppose you want to create some graphics library whose purpose is to simply draw a circle or a rectangle to a window. When designing the API with this design in mind, our concern is not how the methods should be implemented, but how a programmer is supposed to use it. Through interface, you can separate the common methods of different classes; this assures that any class that inherits from the given interface must implement the methods defined in the interface else the compiler will give you the errors. Let’s consider an interface “Drawable” with a method “Draw” that needs to implemented in the classes that inherits this interface.

Now, every class that inherit from “Drawable” must implement “Draw”.

In this case, a programmer do not need to worry about the implementation of “Draw”  in every class that implements the interface “Drawable”, they just use those methods in the way they are supposed to be used without worrying about the implementation of different “Draw” methods. Here, you also try to ensure that the entity is named in a manner (“Drawable” in this case should have an obvious “Draw” method) that makes sense and it has all the relevant aspects.

As abstraction provides a programmer a generalized view of the classes, a user of your library can easily examine the interface without reading the implementation, thus it enhances readability. Recall that I have mentioned that abstractness can increase the efficiency in some cases. To unpack what I said: Suppose you have several methods that have similar functionality of “Draw”, but you name them differently, let’s say “Draw” and “DrawOnce”, for drawing in a loop or drawing once respectively. Now the programmer can easily see how those methods are supposed to be used. Conversely, if you use a boolean member in a class, let’s call it “drawOnce”, to flag whether the method “Draw” should draw in a loop or draw once, there is a chance that a programmer will miss the detail and end up using “Draw” without flag of “drawOnce” set to “true” when he/she only wants to draw once, thus the resulting program will not be very efficient.

At this point, I should add that interfaces are linked with the concept of Polymorphism in a sense that it is considered the application of interfaces.

Understanding Polymorphism

Through Polymorphism, a programmer can use the derived class in the place of a base class. It is a way to generically use common methods. Polymorphism can be considered the applications of several concepts such as inheritance, generics etc. I won’t go into the details of Polymorphism and its every possible application. I will focus only on its application of abstraction.

In our example, any class can be passed to a method “Draw” of an instance of “Window” that implements the interface “Drawable”, resulting in expressive code and reduced complexity.

Now, a programmer who likes to draw different shapes other than rectangle and circle can create their own user-defined classes that inherit from “Drawable” and then simply implement the “Draw” method. In this way, the instance of a user-defined class can easily be passed into the method “Draw” of an instance of “Window”. Through the process, we obtained what we wanted in the first place, i.e., data hiding, thus we can conclude that abstraction is indeed a great design for libraries.

Conclusion

In this tutorial, I have showed you the concepts of Abstraction, how it is normally understood in the OOP, and how you can use its amazing power to harness your program. If you have any questions, then you can ask in the comments section below!

Functions / Methods in Java and C#

When we hear a term “Function” our mind create the image of an option or technology in a program that do something, so when we use the function, a program does a particular job. A function is simply a combination of several tasks written beautifully within a single label through a proper organization rules. In programming languages, a function is used to hold several lines of code that does something, such as, adding the two integers together, displaying a text on a console screen, etc. Through the use of function, you can cut the multiple lines of codes and paste into a function with a name (i.e., a name through which you will remember it) and then call (use) it whenever you need the functionality in your program without duplicating the same code throughout the program, thus FUNCTIONS REDUCES THE SIZE OF THE CODE.

In OOP languages (i.e., C# or Java), a function is implemented as a static method, which means that you need to declare a class first in order to use it, unlike in non-OOP programming languages (like C) where a function exists by itself. In C# and Java, a function is a part of an object and is defined in a class and every executed instruction is performed in the context of a method. So, for example, when we call “System.out.prinln()” (in Java) or “Console.WriteLine()” (in C#), we are actually calling a function which simply creates a line to the console, thus a function is named as “method” in OOP.

In this tutorial, I will teach you how you can implement functions in C# and Java to make your programming life more joyful!

Static Methods

As I said earlier, In OOP languages, we define functions as static methods. They are those methods which doesn’t change the state of a class (thus they are named ‘static’). It make sense that functions are implemented as static methods, because in functions we are interested in executing some arbitrary and we are not interested in changing the object’s state.

The syntax for defining a static method in a class is same for both C# and Java. It is as follows:

Considering this syntax, we can very easily define our function. Let’s define a function for adding two numbers, we can do so as follows:

You can see that it’s trivial to define a function, but then the task that our function does is trivial as well. Let’s define another function for calculating factorial:

We can use our functions in a program as follows:

C#

Java

Static Classes

Static classes are those classes whose only one instance is available to a program. These classes don’t need to be instantiated. It is useful feature in those cases where you know that an instance of a class that doesn’t need to be passed to other classes, but can be easily used without instantiating. A static class is a class which can only contain static members, and therefore cannot be instantiated. One of its examples is “GameSettings” for storing different settings in our game. We can define such class as follows:

The following is the syntax for using static method in a class:

Now we can use our static class in a program as follows:

C#

Java

Note that you can not use non-static members in static methods.

Conclusion

As you can see, Static Class can be used as a convenient container for a sets of methods that only run on input parameters and doesn’t modify the state of an object. In the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations like ‘cos’, ‘sin’ etc., without any requirement to store or retrieve data that is unique to a particular instance of the Math class, thus they don’t need instantiation to operate.

Functions are best suited for trivial tasks, especially when some task is duplicated several times in your program; hence you can simply create a static method in a class and call it whenever you need it. In which cases are you going to use functions?