Java Lesson 9 – Theory

This is the final tutorial in the Java series and today we’ll be focussing more on the theory. We’ll be looking at how Java as a language actually works and the advantages and disadvantages that come with it.

We’ll also be looking at key object orientation concepts and explaining what they mean.

We’ll then have a reflective summary of what you should have learnt by this point. So without any further ado, let’s dive right in.

As some of you may know, Java is a cross-platform language. That is to say, you can run Java on a Mac, Windows or Linux computer, which we’ve seen when we went to download the JDK or any applications that are centred around Java development.

This cross-platform nature means that you need not worry about developing different blocks of code for different systems and makes the life of the developer that much easier.

This is because Java is not just solely a language but rather a language coupled with its own computing environment – the JVM.

It is the JVM (Java Virtual Machine) that allows it to run on multiple platforms.

What this allows Java to do is to take the Java code and compile it to byte code. This byte code is then deployed in the JVM, which will determine how to execute the code on the system it is running off.

Since the JVM is compiling the byte code at the application runtime, it means that external classes can be loaded during runtime too. This is where things like reflection work, and how you can extend program functionality through abstract classes.

As we said earlier in the series all variables of Object types are references, with all objects being allocated on a heap or potentially a stack.

We also discussed the garbage collector in Java which handles the memory management of Java applications, by removing variables from memory if they have not been referenced recently or will be referenced soon.

Now we’ll take a look at object orientation. As we discussed earlier, Java is an object-oriented language. You write classes which, when instantiated, create objects. But what benefits are there to object orientation and how does this relate to the tutorial material we’ve covered thus far.

The main components/keywords we’ll be examining here are “Abstraction”, “Inheritance”, “Encapsulation” and “Polymorphism”.

Abstraction is the process of reducing or limiting the amount of information required to represent a given data item. It solves issues at design level, look at our vehicle example in previous weeks. We had only the bare minimum information required in vehicle and the classes that extended it added only the information that was relevant to them.

Inheritance is then the process of deriving a class from another. This then yields the parent class (being inherited from) and a child class (doing the inheriting). In the below example, you can see all classes inheriting from the Vehicle abstract class.

Encapsulation is the concept of storing data and methods that operate on them together. This then keeps both safe from interference from external sources. This is done through the access modifiers we’ve seen such as “private” and “public”. When you create a class in Java you are generally using encapsulation to store all the fields with its methods together.

Lastly, Polymorphism is the concept where you can perform the same action in multiple different ways. For example, two people may have the same function “makeCoffee()” but the way in which they do that will depend on the person and the objects give to them. This can be achieved in Java by overriding functions from parent classes, allowing the subclass to perform its own action when the function is called. This is particularly prevelant with interfaces, where the inheriting class is forced to override the function declaration in its parent and implement its own definition for the function.

All thats left to do now is summarise what you’ve learned.

  • Variables, Objects and Data types
  • Control statements
  • Function declaration
  • Abstraction and Inheritance
  • Building interfaces

You should have everything you need to really get stuck in and start making some proper applications! I hope you found this series of guides helpful. Please follow the blog that would be much appreciated and feel free to read any of the other guides produced.

Thank you for your time.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started