A Deep Dive into Java Core Concepts

Notes on Java for beginners with examples and resources.

Hari Sri Veni Amulothu
5 min readMay 13, 2024

Java is a object-oriented and platform independent programming language.

Java is not a purely object-oriented programming language because of primitive datatypes , static methods, static classes and so on.

why java came into the existence even though we have the C and C++?

C and C++ are platform dependent hence the programs cannot be able to run on different platforms due to this reason, the father of java gosling wanted to develop a language which should be able to run on different machines.

how java is platform independent?

The java compiler complies the program into an intermediate byte code format which is then executed by java virtual machine(JVM). Each machine has its respective JVM but all of them understand the bytecodes from any machine and execute it because it decodes the byte code and gives the output.

Data types and Reference types

there are 8 primitive data types. They are byte, short, int, long, float, double, boolean and char. They hold the data itself.

Reference types

Reference type in java refers to data types that stores the references of objects in memory rather than the actual data itself. They are Arrays, interfaces, classes, Strings , enum, etc. they are also known as non-primitive types.

Type Conversion and Casting

Type Casting means converting the datatype of one variable into another data type in which the destination variable datatype is smaller the source datatype. Hence this is called as narrowing casting. Type Conversion is vice versa to the type casting but this was done by the java at compile type by compiler.

Java OOPs

class- bundling of data and methods that operate on that data within a single unit. we can also refer class as a blue print of instances or objects.

object — They are the physical existence of the class.

There are mainly four concepts in oops. They are abstraction, Encapsulation, Inheritance and Polymorphism.

Encapsulation

A way of hiding the implementation details of a class from outside access and only exposing a public interface that can be used to interact with the class. we achieve this by declaring the instance variables as private so that they can only access with in the class only. If we want to provide access to other class public methods called getter and setter methods are used, which are used to retrieve and modify the contents of the instance variables. For more details Encapsulation.

Abstraction

Abstraction in Java involves presenting only the crucial aspects or functionalities to the user, while hiding the implementation details. For Example, consider a man drives the car. He only know that pressing the accelerator it will increase the speed, breaks to stop the car but he didn’t know the internal working mechanism of the car for the breaking system, and so on. This is called abstraction. The abstraction is achieved by the interfaces and abstraction classes. we can achieve the 100% abstraction through the interfaces. Abstraction. Abstraction class may also have the concrete methods. Abstract methods doesn’t have the implementation part or body. The classes which extends the abstract class should override this method and implement them otherwise the subclass should also be declared as the abstract class.

Inheritance

This allows a class to inherit properties and behaviors from other class. This Enables the code reusability and promotes the creation of hierarchical relationships between the classes. For example, a Golden Retriever is part of the classification dog, which in turn is part of the mammal class, which is under the larger class animal. Without the use of hierarchies, each object would need to define all of its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. using the extends keyword we can achieve this inheritance . extends refers to deriving the properties from super class. Inheritance.

Super class /parent class— the class which is inherited.

subclass/child class— the class which inherits the other class.

super keyword is used to represent the parent class. while calling the super constructor using the super() this line should be placed in the first line of the subclass constructor.

Types of Inheritance:

Single Inheritance — In which only one class inherits the super class

Multilevel Inheritance- In this type, one class inherits the one class which is inherited by another class and so on.

hierarchical inheritance — in this type, two or more classes inherits the same class.

Multiple Inheritance — in this type , one class inherits two class. in java, multiple inheritance is not possible if the two classes contains the same method then the method overriding task will lead to exception.

hybrid Inheritance- combination of any of these inheritance.

Polymorphism

Poly means many and morphs means forms so it means many forms. This allows us to perform a single action in different ways. for example “+” if we perform this operation on numerical operands it performs addition otherwise if one of the operand is the string it performs the concatenation. They are two types of polymorphism they are: Compile time polymorphism and run-time polymorphism. Compile time polymorphism is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading. In method overloading where the have same method name but varies data types and number of parameters. Runtime polymorphism is achieved by the method overriding in which it provides it implementation already defined in its super class.

Exception Handling

An exception is an abnormal condition arises in a code sequence at run time. In other words, Exception is a runtime error. Java Exception handling is managed by the five keywords try, catch, throw, throws and finally. The code which may occur the exception is placed within the try block, if the exception occur it is thrown then it is handled by the catch block. To manually throw the exception we use the throw keyword and to throw out of the method we use the throws keyword. Any code code that absolutely must be executed after a try block completes is put in a finally block. Exception is the sub-class of the Throwable class. We can also create our own exception classes by extending the Exception class. Exception Handling.

Generics

The term generics means parameterized types. With generics, you can define an algorithm once, independently of any specific type of data, and then apply that algorithm to a wide variety of data types without any additional effort. Parameterized types are important because they enable you to create classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter. Using generics, it is possible to create a single class, for example, that automatically works with different types of data. A class, interface, or method that operates on a parameterized type is called generic, as in generic class or generic method.

--

--