Maker Pro

Getting Started With Java

December 31, 2018 by Daniel Hertz

Get started with the programming language Java by learning the most important concepts for beginners.

This article will introduce you to the absolute basics of the Java programming language. It is targeted towards beginners and should give you an overview of the most important concepts of the language.

Before you start writing your code, evaluate whether Java is the right language for your project

Classes and Objects

Java is an object-oriented language, meaning you can instantiate classes to create objects. Classes are constructs that consist of methods and variables. The methods contain the functional code.

You can create an object from any class as long as it is not marked as abstract. By doing so, you invoke an individual instance of that class where all non-static variables will have their own value. The class itself only serves as a blueprint.

For example, you define a bottle-class and create two instances of it. Each bottle-object can now be filled with a different beverage and can have different properties:

classes and objects example for Java

Interfaces and Polymorphism

Interfaces and abstract classes can be used to further generalize your code. Let’s use the same bottle example, except that this time, you want each of your products to have a unique bottle and all bottles should be handled by the same machine.

You could create a class for each bottle (ColaBottle, WaterBottle, etc.) but that would require you to get a different machine for each type. It's also very likely that the separate classes will share a lot of the source code.

This is where interfaces come in handy. They allow you to create a general description of what properties a bottle has and what you can do with it. Each specific bottle will then implement the generic interface and will, therefore, become a type of that interface. This will also allow you to create a machine that accepts any bottle that implements that interface:

Interfaces and polymorphism example for java

Abstract Classes vs. Interfaces

You could also create an abstract class and let each of your bottle classes extend your abstract generic bottle. So what's the difference?

Probably the most important difference is that you can implement multiple interfaces in a class but you can only extend one class.

An abstract class can (but doesn't have to) contain a default implementation for methods, while an interface can only contain a method's signature.

You also have to implement every function defined in an interface while you only have to override abstract methods in a subclass.

Abstract Methods

Methods can also be marked as abstract but then they can only be contained in an abstract class and they are not allowed to have a function body. Therefore you can’t call these methods (only the overridden ones of the subclasses).

Access Modifiers

The visibility of classes, methods, variables and other constructs can be changed by adding an access modifier. If you don’t explicitly add one, the default modifier is used. The following table illustrates the external visibility of an element with each modifier:

access modifiers in Java

Hiding the fields of a class and only making them accessible through certain methods from the outside is referred to as data-hiding.

The Static Keyword

The static keyword indicates that a variable, function, etc. belongs to the class itself rather than an object of that class.

Each object of a class will have the same value in a static variable and the objects won’t have their own copy of it.

Methods that are marked as static can be called without creating an object of the class they are defined in. However, static methods can only use static variables and some keywords (used in the context of objects) will be invalid inside of them.

Understanding Java

Java is commonly used in web applications, web services, Android phones, and more. It is a powerful language that makers can use in a variety of projects. Understanding the important basics of the language can help you get your footing in this programming language.

Related Content


You May Also Like