Basics of JavaDecember 28, 2022
Basics of Java
Java is still the most popular programming language according to PYPL - PopularitY of Programming Language Index. Knowing the basics of Java is crucial for advancing in Java programming and going beyond in software development. For this reason, I have decided to describe the basics of Java within this article, especially for the fresh starter to Java programming. I will try to cover most Java basics during this article without going into details in a very high-level and easy way in order to make crystal clear for every Java new beginner.
I will try to avoid giving complex code examples but only try to mention about the descriptions of Java basics with a text based narrations and simple code snipplets.
So, let’s start our Java 101 article.
What is Java?
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Java was developed by James Gosling at Sun Microsystems in 1991. It was formally named as OAK and and got the name Java in 1995. Sun Microsystems licensed most of its Java technologies under the GPL-2.0-only license in 2007. Oracle acquired Sun Microsystems in 2010 and Java moved under Oracle family with this agreement. As of September 2021, Java 8, 11 and 17 are supported as Long Term Support (LTS) versions and Java 20 is planned to be released on March 2023.
There were five primary goals in the creation of the Java language;
- It must be simple, object-oriented, and familiar.
- It must be robust and secure.
- It must be architecture-neutral and portable.
- It must execute with high performance.
- It must be interpreted, threaded, and dynamic.
Because of these goals; Java is now one of the most popular programming languages in use according to GitHub particularly for client-server web applications, with a reported over 9 million developers.
So, why it is so popular? It is a general-purpose programming language intended to let application developers “write once, run anywhere (WORA)”. WORA means that a compiled Java code can run on all platforms that support Java without the need for recompilation.
How it is possible to run a compiled Java code on all platforms? At this time Java Virtual Machine (JVM) comes our help. Java applications are typically compiled to bytecode that can run on any JVM regardless of the underlying computer architecture.
A java code that is compiled in and turned into byte code is ready to be run on each platform with the help of JVM.
After giving a brief explanation of Java, let’s switch to some basics of Java. I will not mention every fundamental but try to go over the important ones for fresh starters to Java programming.
Access modifiers are used to set level of access for classes, methods, variables, constructors in Java as can be understood from its name. There are four types of access modifiers available in Java;
- Default → When no access modifier is specified for a class, method, constructor or variable, it is having the default access modifier by default. The access level of a default modifier is only within the package. It cannot be accessed from outside the package.
- Private → The access level of a private modifier is only within its class. You cannot access from outside its class.
- Protected → The access level of a protected modifier is within its package and outside its package through its child classes.
- Public → The access level of a public modifier is everywhere. It can be accessed from within its class, outside its class, within its package and outside its package.
In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. It is one of the four principles of object-oriented programming.
To encapsulate/hide variables;
- Variables in a class must be declared as private.
- Public setter and getter methods are provided to modify and view the variables values of this class.
Please check the Gist to see how encapsulation is implemented in Java.
Attributes and methods can be inherited from one class to another in Java and that is called inheritance. The information (attributes & methods) can be used in a hierarchical order by using inheritance. It is also one of the four principles of object-oriented programming.
Inheritance is an IS-A relationship which means we can use inheritance if and only if there is an IS-A relationship between two classes. For instance, dog is an animal, car is a vehicle, strawberyy is a fruit.
The class which inherits the properties of the other class is known as subclass or child class and the class whose properties are inherited is known as superclass or parent class.
extend is the keyword in Java to inherit the properties of a superclass.
Superclass & subclass may have properties with the same names. super is the keyword that is used in the subclass in Java to differentiate that kind of cases.
Java does not support multiple inheritance which means and a subclass can only extend one superclass.
Please check the Gist to see how inheritance is implemented in Java.
Another principle of object-oriented programming is polymorphism which means the condition of occurring in several different forms. It occurs when we have many classes that are related to each other by inheritance. It uses inherited methods to perform different tasks. This allows us to perform a single action in different ways.
Now, let’s look method overriding & overloading.
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. It increases the readability of the program. There are two ways to overload methods in Java;
- By changing the number of arguments in the method signature
- By changing the data type of arguments in the method signature
Methods cannot be overloaded by changing the return types.
If subclass has the same method as declared in its superclass via inheritance, it is known as Method Overriding. It is used for runtime polymorphism by providing the specific implementation of a method which is already provided by its superclass. Some rules for method overriding in java are as below;
- The overridden method must have the same name as in its superclass.
- Final and static methods cannot be overridden.
- The overridden method must have the exactly same arguments as in its super class.
- The overridden method must have same return type with the method in its super class or subtype.
- Constructors cannot be overridden.
Abstraction is another principle of object-oriented programming and it is a process of hiding the implementation details from the user, only essential information/functionality will be provided to the user.
Abstraction is achieved using abstract classes or interfaces in Java.
abstract is the keyword in Java which is a non-access modifier & used for declaring abstract classes and methods.
Abstract class is a restricted class that cannot be used to create objects. In order to access an abstract class, it must be inherited from another class. Abstract classes may or may not contain abstract methods but if a class has at least one abstract method, then the class must be declared abstract.
Abstract method can only be used in an abstract class. An abstract method contains a method signature, but it has no method body. The body is provided by the subclass of its class.
Interface is another way to achieve abstraction in Java. An interface is an abstract class that is used to group related methods with empty bodies.
implement keyword is used to implement interface within a class.
Below are important points for interface in java;
- Interface methods do not have a body. Body is provided within the class that implements the interface.
- All methods must be overridden while implementing an interface.
- Interface methods are by default abstract and public.
- Interface attributes are by default public, static and final.
- An interface cannot contain a constructor.
Interface is used mainly for security reasons, to hide certain details and only show the important details of an object.
Java does not support multiple inheritance but it supports implementing multiple interfaces. So, multiple inheritance can be achieved by multiple interfaces.
Please check the Gist to see how interface example in Java.
Wrapper classes provide a way to use primitive data types as objects in Java. Wrapper classes are declared in java.lang package. Below is the mapping between primitive data types and wrapper classes.
For the below cases, wrapper classes are required;
- Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
- Serialization: Objects should be converted in to streams for serialization. Primitive values can be converted into object via wrapper classes for serialization.
- Synchronization: Java synchronization works with objects in Multithreading.
- java.util package: The java.util package provides the utility classes to deal with objects.
- Collection Framework: All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only. So, if we have a primitive value, it should be converted into an object via its corresponding wrapper class.
There are also two features related to wrapper classes & primitive types.
- Autoboxing: The automatic conversion of primitive data type into its corresponding wrapper class
- Unboxing: The automatic conversion of wrapper type into its corresponding primitive type
In a Nutshell
Java is still the most popular programming language and knowing the basics of it is the key success factor to advance in Java programming. Throughout this article, I try to cover how four principles of object-oriented programming are implemented in Java with main features like access modifiers, wrapper classes & interface.
Hope you like this article and especially new beginners to Java programming can benefit from this article.
Your questions & comments are always welcomed.