Understanding Base Class in Java

A key idea in object-oriented programming (OOP) is inheritance, which enables classes to take on traits and characteristics from other classes. Through the usage of base classes and derived classes, the idea of inheritance is implemented in Java. We shall examine what a base class in Java is in this post as well as its importance in the world of programming.

A base class, usually referred to as a superclass or parent class, is a type of class that acts as a template or guide for additional classes. Multiple derived classes may have its common features and behaviours. You can create a hierarchical relationship between classes by establishing a base class, which encourages code reuse and offers a structured method of organising your code.

To create a base class in Java, you simply define a class as you normally would, but without explicitly inheriting from another class. Let's consider a simple example to illustrate this:

In the above code snippet, we have defined a base class called "Animal." It has two private instance variables, name and age, and two public methods, eat() and sleep(). Any class that wants to inherit these properties and behaviours can extend the Animal class.

Let's create a derived class, also known as a subclass or child class, that inherits from the Animal class:

Using the extends keyword, the class Dog in the example above extends the Animal class. As a result, all the public and protected members (variables and methods) of the Animal class are passed on to the Dog class. In this instance, the Dog class inherits the Animal class's members for name, age, eat(), and sleep(). Additionally, a new function named bark() that is exclusive to dogs is added to the Dog class.

We can build a hierarchy of classes that represent relationships and behaviours in the actual world by utilising inheritance and base classes. Multiple degrees of inheritance are possible, with each derived class enhancing the characteristics of its base class. This encourages code reuse, lessens duplication, and enhances the structure and organisation of your codebase as a whole.

When referring to base class members inside of derived classes in Java, you can also utilise the super keyword. You may then access and use constructors or methods from base classes thanks to this. In the Dog class example, you can initialise the name and age variables inherited from the base class by using the super(name, age) line to invoke the Animal class' constructor.

Java offers single inheritance, which means a class can only extend one base class, which is something to keep in mind. The use of interfaces, which give classes a method to create contracts that they can implement, allows you to accomplish multiple inheritance-like behaviour.

Here's the complete code for the example mentioned in the article:

File Name: Main.java

Output:

The animal is eating.
The animal is sleeping.
The dog is barking.

We have three classes in the code above: Animal, Dog, and Main. The Dog class extends the Animal class and is derived from it, whereas the primary class houses the primary method used to run the programme. Animal is the basic class.

The name "Buddy" and the age 3 are sent to the constructor of a dog instance of the Dog class that is created in the main method. Then we call the dog object's eat(), sleep(), and bark() functions. The accompanying messages showing that the animal is eating, sleeping, and barking are displayed in the output.

This exemplifies how the Dog class derives the eat() and sleep() methods from the Animal class and adds its own distinctive behaviour with the bark() function.

Please be aware that each class must be saved in its own file with the same name as the class and the.java extension, for example, Animal.java, Dog.java, and Main.java.

In conclusion, a Java base class acts as a base or model for additional classes. It encourages code reuse and logically structures your codebase by encapsulating shared characteristics and behaviours that may be passed down to derived classes. You can create Java programmes that are more scalable and manageable by comprehending and properly utilising basic classes.