Menu

Java Inheritance: Reusing Code Made Simple
πŸ“°

Java Inheritance: Reusing Code Made Simple

DEV Community: javaΒ·ChandruΒ·6 days ago
#mEOVpbB2
#devtojava#devto#dev#class#inheritance
Reading 0:00
15s threshold

Inheritance is one of the important concepts of Object-Oriented Programming (OOP) in Java.

It allows one class to acquire the properties and methods of another class. This helps us reuse code and avoid unnecessary duplication.

What is Inheritance?

Inheritance allows a child class to inherit variables and methods from a parent class.

Java uses the extends keyword to implement inheritance.

Simple Example

class Animal {

void eat () { System . out . println ( "Animal is eating" ); } }

class Dog extends Animal {

void bark () { System . out . println ( "Dog is barking" ); } }

public class Main {

public static void main ( String [] args ) {

Dog dog = new Dog ();

dog . eat (); dog .…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.