Menu

1 / 2

Inheritance in Java

DEV Community·Sasireka·6 months ago
#dwmV45Et
#coding#java#programming#software#class
Reading 0:00
15s threshold

1) What is Inheritance?

Inheritance in Java is a core principle of Object-Oriented Programming (OOP) that allows a subclass (child) to acquire the properties (fields) and behaviors (methods) of a superclass (parent).

Syntax:

class ParentClass { // fields (variables)

// methods }

class ChildClass extends ParentClass { // additional fields

// additional methods }

Enter fullscreen mode

Exit fullscreen mode

2) Types of Inheritance

Single Inheritance

One child class inherits from one parent class.

public class Animal { public void eat () { System . out . println ( "Eating" ); } }

public class Dog extends Animal { public void bark () { System . out . println ( "Barking" ); } }

Enter fullscreen mode

Exit fullscreen mode

Multilevel Inheritance

A class inherits from another class, which itself is inherited from another class.

public class Animal { public void eat () { System . out . println ( "Eating" ); } }

public class Dog extends Animal { public void bark () { System . out .…

Continue reading — create a free account

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