What is not inherited in Java?

Brown 66 Published: 07/20/2024

What is not inherited in Java?

I'd be happy to help!

In the object-oriented programming (OOP) language Java, inheritance plays a crucial role in creating hierarchies of classes and objects. Inheritance allows a subclass (also known as a derived class or child class) to inherit the properties, methods, and behavior of its superclass (also known as a base class or parent class).

However, there are some things that are not inherited in Java. Here are a few examples:

Local Variables: Local variables declared within a method are not inherited by subclasses. Each method creates its own separate scope for local variables.

Static Variables: Static variables declared at the class level (outside any method) are not inherited either. Every class, including subclasses, has its own independent copy of these static variables. Local Blocks: Local blocks such as synchronized methods or inner classes do not get inherited by subclasses. Private Members: Private members (variables and methods) declared within a class are not directly accessible from subclasses, even if the subclass inherits from the superclass. Subclasses can, however, use accessor methods to access private members of their parent class. Final Classes: Final classes cannot be inherited by any other class, including subclasses. Trying to extend a final class will result in a compilation error. Method Overriding: When a subclass overrides a method from its superclass, the overridden method is used instead of the original one. However, if no overriding is done and the subclass simply inherits the method without changing it (which is called "method shadowing"), then the inherited method remains unchanged and continues to behave as before. Access Modifiers: The access modifiers (public, private, protected) for methods and variables are not inherited by subclasses either. Subclasses can inherit the access modifier from their superclass or override them with their own. Constructors: Constructors are special methods that are used to initialize objects. They are not inherited by subclasses, even if the subclass inherits from its parent class. Abstract Methods: Abstract methods are declared using the abstract keyword and have no implementation in the superclass. Subclasses must override these methods with their own implementation. Abstract methods themselves are not inherited; instead, the subclass must implement them independently.

In summary, while Java does support inheritance as a fundamental concept in object-oriented programming, there are certain things that are not inherited by subclasses, such as local variables, static variables, and method overriding (if no explicit override is done).

Polymorphism in Java example

I'd be happy to provide a polymorphic example in Java!

Polymorphism is the ability of an object to take on multiple forms. The most common forms are method overriding and method overloading.

Let's consider an example where we have a class called "Vehicle" with two methods: "startEngine()" and "stopEngine()". Now, we'll create two subclasses, "Car" and "Motorcycle", which will override these methods to provide their own implementation. This is an example of method overriding in polymorphism.

Here's the code:

class Vehicle {

void startEngine() {

System.out.println("Vehicle engine started");

}

void stopEngine() {

System.out.println("Vehicle engine stopped");

}

}

class Car extends Vehicle {

@Override

public void startEngine() {

System.out.println("Car engine roaring to life");

}

@Override

public void stopEngine() {

System.out.println("Car engine silenced");

}

}

class Motorcycle extends Vehicle {

@Override

public void startEngine() {

System.out.println("Motorcycle engine revving loud and clear");

}

@Override

public void stopEngine() {

System.out.println("Motorcycle engine quieted");

}

}

Now, let's see an example of method overloading in polymorphism. We'll add a "makeSound()" method to the Vehicle class with different parameters. This method will produce different sounds based on the type of sound it receives.

Here's the code:

class Vehicle {

void startEngine() {

System.out.println("Vehicle engine started");

}

void stopEngine() {

System.out.println("Vehicle engine stopped");

}

void makeSound(int volume) {

System.out.println("Vehicle making a " + volume + " sound");

}

}

class Car extends Vehicle {

@Override

public void startEngine() {

System.out.println("Car engine roaring to life");

}

@Override

public void stopEngine() {

System.out.println("Car engine silenced");

}

}

class Motorcycle extends Vehicle {

@Override

public void startEngine() {

System.out.println("Motorcycle engine revving loud and clear");

}

@Override

public void stopEngine() {

System.out.println("Motorcycle engine quieted");

}

}

Now, let's test our code:

public class Main {

public static void main(String[] args) {

Vehicle vehicle = new Vehicle();

vehicle.startEngine();

Car car = new Car();

car.makeSound(5);

Motorcycle motorcycle = new Motorcycle();

motorcycle.stopEngine();

}

}

The output will be:

Vehicle engine started

Vehicle making a 5 sound

Motorcycle engine quieted

In this example, we demonstrated method overriding and method overloading in polymorphism. Method overriding allows subclasses to provide their own implementation of methods inherited from the superclass. Method overloading enables methods with the same name but different parameters or return types.

These concepts are crucial for creating robust and scalable software systems that can adapt to changing requirements and provide flexibility in code design.