Java method in parent class

Java Inheritance Tutorial: explained with examples

Inheritance is the process of building a new class based on the features of another existing class. It is used heavily in Java, Python, and other object-oriented languages to increase code reusability and simplify program logic into categorical and hierarchical relationships.

However, each language has its own unique way of implementing inheritance that can make switching difficult.

Today, we’ll give you a crash course Java inheritance and show you how to implement the core inheritance tools like typecasting, method overriding, and final entities.

Here’s what we’ll cover today:

Start mastering Java with our hands-on courses today.

Java is one of the most sought after and widely used programming languages in the tech industry, and will continue to be for the foreseeable future. It runs on everything from phones to game consoles to data centers. This path is perfect for you if you already have experience programming, but are new to Java. It’ll help you become an employable Java developer with incredibly valuable skills, ready to jump in and contribute to real-world projects.

What is Inheritance?

Inheritance is a mechanism that allows one class to inherit properties or behaviors from another class. Multiple classes can inherit from the same parent class, forming a tree-like hierarchy structure. Inheriting classes can add features beyond those inherited from the parent class to allow for unique behavior.

Читайте также:  Downloading multiple files using php

Inheritance is essential to advanced Object Oriented Programming (OOP) as it allows you to reuse one class’s features across your program without replicating code.

Inheritance is often used to represent categories (parent classes) and sub-categories (subclasses). The parent class sets the features present in all objects regardless of subcategory, while each subclass represents a smaller, more specific category.

For example, you could create the class Car that specifies wheels = 4 and a subclass Sedan that includes the attribute doors = 4 . The flow of inheritance relationships often reflects the logical relationship similar to squares and rectangles; in this case, all sedans are cars, but not all cars are sedans.

Inheritance has three main advantages:

  1. Reusability: Inheritance allows you to reuse the features of an existing class an unlimited number of times across any class that inherits that class. You can keep consistent functionality across all objects of the same type without rewriting the code.
  2. Code Structure: Inheritance provides a clear, drawable logic structure for your program. It allows developers to understand your code as a collection of related but unique categories rather than simply a block of code.
  3. Data Hiding: The base class can be set to keep some data private so that it cannot be altered by the derived class. This is an example of encapsulation, where access to data is restricted to only the classes that need it for their role.

What is inheritance in Java?

Each programming language has slightly different terminology for inheritance. In Java, the parent class is called the superclass, and the inheritor class is called the subclass. Developers may also call superclasses base or parent classes and subclasses derived or child classes.

Subclasses are linked to superclasses using the extends keyword during their definition. Subclasses can define new local methods or fields to use or can use the super keyword to call inherited methods or the super constructor.

class b < // implementation of inheritedMethod() >class a extends b

When to use super keyword

super is essentially a “previous value” button called from within a child class that allows you to read and access features from the parent class regardless of their value in the current child class.

The super keyword is used to:

  • Access parent class fields: super.var reads the value of var set in the parent class, while var alone reads the modified value from the child.
  • Calling a parent class method: super.method() allows the child to access the parent class implementation of method() . This is only required if the child class also has a method with the same name.
  • Using Constructors: This allows you to create new instances of the parent class from within a child class.

As a refresher, constructors in Java are special methods used to initialize objects. Calling the super constructor creates a new object that requires all the fields defined in the parent class constructor.

You can then add additional fields in other statements to make the child instance more specific than the parent. Essentially, it allows you to use the parent class constructor as a template for your child class constructor.

 public Car(String make, String color, int year, String model, String bodyStyle) < super(make, color, year, model); //parent class constructor this.bodyStyle = bodyStyle; >

Types of Inheritance

There are several types of inheritance available in Java:

  • Single inheritance is when a single subclass inherits from a superclass, forming one layer of inheritance.
  • Multilevel Inheritance is when a superclass is inherited by an intermediate class, which is then inherited by a derived class, forming 3 or more levels of inheritance.
  • Hierarchical inheritance is when one superclass serves as a baseline for multiple specific subclasses. This is the most common form of inheritance.

There are also two other types of inheritance that are only available in Java through a combination of class and interface inheritance.

  • Multiple inheritance, when a single subclass inherits from multiple parent classes.
  • Hybrid inheritance, a mix of two or more of the above kinds of inheritance.

Java does not support multiple inheritance with classes, meaning both of these types of inheritance are impossible with Java classes alone. However, a subclass can inherit more than one interface (an abstract class). You can therefore simulate multiple inheritance if you combine the use of interfaces and classes.

Java inheritance examples

To help you understand inheritance more, let’s look at Java inheritance examples in pseudocode. Pay attention to the syntax components of inheritance we’ve seen so far, like super and shared methods.

To declare inheritance in Java, we simply add extends [superclass] after the subclass’s identifier.

Here’s an example of a class Car that inherits from base class Vehicle using private strings and getter/setter methods to achieve encapsulation.

// Base Class Vehicle
class Vehicle
// Private Fields
private String make;
private String color;
private int year;
private String model;
// Parameterized Constructor
public Vehicle(String make, String color, int year, String model)
this.make = make;
this.color = color;
this.year = year;
this.model = model;
>
// public method to print details
public void printDetails()
System.out.println("Manufacturer: " + make);
System.out.println("Color: " + color);
System.out.println("Year: " + year);
System.out.println("Model: " + model);
>
>
// Derived Class Car
class Car extends Vehicle
// Private field
private String bodyStyle;
// Parameterized Constructor
public Car(String make, String color, int year, String model, String bodyStyle)
super(make, color, year, model); //calling parent class constructor
this.bodyStyle = bodyStyle;
>
public void carDetails() < //details of car
printDetails(); //calling method from parent class
System.out.println("Body Style: " + bodyStyle);
>
>
class Main
public static void main(String[] args)
Car elantraSedan = new Car("Hyundai", "Red", 2019, "Elantra", "Sedan"); //creation of car Object
elantraSedan.carDetails(); //calling method to print details
>
>

This is an example of single inheritance, as only one object inherits from the parent class. On line 37, you can see that we use super to call the superclass constructor that simplifies our Car constructor. You can also see how Car has access to the Vehicle class printDetails() method on line 42.

printDetails() can be called without super because Car does not have its own implementation of printDetails() . The super keyword is only needed when the program must decide which version of the method is being used.

Start mastering Java with our hands-on courses today.

Java is one of the most sought after and widely used programming languages in the tech industry, and will continue to be for the foreseeable future. It runs on everything from phones to game consoles to data centers. This path is perfect for you if you already have experience programming, but are new to Java. It’ll help you become an employable Java developer with incredibly valuable skills, ready to jump in and contribute to real-world projects.

Typecasting in Java

Java also allows you to reference a subclass as an instance of its superclass, essentially treating the subclass as if it were of the superclass type. This process is known as typecasting. It is a great way to create modular code as you can write code that will work for any subclass of the same parent. For example, you can reference a Car type variable as a Vehicle type object.

Car car = new Car(); Vehicle vehicle = car; 

We first create a Car instance then assign that instance to a Vehicle type variable. Now the Vehicle variable reference points to the Car instance. This allows you to treat any subclass of Vehicle as the same Vehicle type, even if you don’t know which subclass of Vehicle it is. The two types of typecasting are upcasting and downcasting.

Upcasting is when you treat a child class as if it were an instance of the parent class, like our previous example. Any fields unique to the child class will be hidden to let them fit the mold of the parent class.

Downcasting is when you treat an instance of the parent class as if it were one of its child classes. While any subclass can be upcast, only objects that were originally subclass typed can be downcast.

In other words, an object can be downcast if the object was originally of the subclass type but was later upcast to the parent class.

//valid code Car car = new Car(); // upcast to Vehicle Vehicle vehicle = car; // downcast to car again Car car2 = (Car) vehicle; 

The upcast object still retains the fields it had and therefore can be added back to make it a valid object of the child class type again.

However, objects that were originally of the parent class do not have values for any essential fields unique to the child class. As a result, it will compile but will throw an error at runtime.

Источник

Оцените статью