Core Java Concepts: Master OOP and More

Core Java Concepts

1. Overloading vs. Overriding

Overloading: When two or more methods in the same class have the same name but different parameters.

Overriding: When the method signature (name and parameters) is the same in the superclass and the child class.

2. Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. A Java interface can only contain abstract methods (no method body). It is used to achieve abstraction and multiple inheritances in Java.

3. Inheritance

Inheritance is one of the key features of OOP that allows us to create a new class from an existing class. The new class is known as a subclass (child or derived class), and the existing class from which the child class is derived is known as the superclass (parent or base class).

4. Types of Inheritance

  • Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

5. Abstract Classes and Methods

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.

The abstract keyword is a non-access modifier, used for classes and methods:

  • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
  • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).

An abstract class can have both abstract and regular methods.

6. Java Exceptions

When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).

7. Final Keyword in Java

The final keyword in Java is used to restrict the user. The final keyword can be used in many contexts.

8. Constructors in Java

In Java, a constructor is a block of code similar to a method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class. In such a case, the Java compiler provides a default constructor by default.

9. Java Static Keyword

The static keyword in Java is used for memory management mainly. We can apply the static keyword with variables, methods, blocks, and nested classes. The static keyword belongs to the class rather than an instance of the class.

10. Super Keyword in Java

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of a subclass, an instance of the parent class is created implicitly, which is referred to by the super reference variable.

11. Threads

Threads allow a program to operate more efficiently by doing multiple things at the same time. Threads can be used to perform complicated tasks in the background without interrupting the main program.