Inheritance in Java is a mechanism where one class acquires the properties (fields) and behaviors (methods) of another class. The class which inherits the properties of another is known as the subclass (derived class, child class), and the class whose properties are inherited is known as the superclass (base class, parent class).
Code Reusability, Method Overriding, and Polymorphism are key benefits of using inheritance in Java.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class TestInheritance {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Console Output:
This animal eats food.
The dog barks.
Single Inheritance is when a class inherits from only one superclass. This is the simplest form of inheritance in Java.
In this example, the class `Car` inherits from the class `Vehicle`, acquiring its properties and methods.
class Vehicle {
void move() {
System.out.println("The vehicle is moving.");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("The car is being driven.");
}
}
public class TestSingleInheritance {
public static void main(String args[]) {
Car c = new Car();
c.move();
c.drive();
}
}
Console Output:
The vehicle is moving.
The car is being driven.
Multilevel Inheritance is when a class is derived from another derived class. This creates a hierarchy of inheritance.
In this example, `Dog` inherits from `Animal`, and `Bulldog` inherits from `Dog`, forming a multilevel inheritance chain.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Bulldog extends Dog {
void growl() {
System.out.println("The bulldog growls.");
}
}
public class TestMultilevelInheritance {
public static void main(String args[]) {
Bulldog b = new Bulldog();
b.eat();
b.bark();
b.growl();
}
}
Console Output:
This animal eats food.
The dog barks.
The bulldog growls.
Hierarchical Inheritance occurs when multiple classes inherit from a single superclass. This allows different classes to share a common behavior.
In this example, both `Cat` and `Dog` inherit from the `Animal` class, showcasing hierarchical inheritance.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
System.out.println("The cat meows.");
}
}
public class TestHierarchicalInheritance {
public static void main(String args[]) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
}
Console Output:
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
Method Overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass. This allows for dynamic method dispatch and polymorphism.
In this example, the `Dog` class overrides the `eat()` method of the `Animal` class to provide a specific implementation.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
@Override
void eat() {
System.out.println("The dog eats bones.");
}
}
public class TestMethodOverriding {
public static void main(String args[]) {
Animal a = new Dog();
a.eat();
}
}
Console Output:
The dog eats bones.
The `super` keyword in Java is used to refer to the immediate parent class object. It is used to access parent class methods and constructors.
In this example, the `Dog` class uses `super.eat()` to call the `eat()` method of its superclass, `Animal`.
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("The dog eats bones.");
}
}
public class TestSuperKeyword {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
}
}
Console Output:
This animal eats food.
The dog eats bones.
Constructor chaining in Java is the process of calling one constructor from another constructor with respect to the current object. It can be within the same class or between parent and child classes.
In this example, the `Dog` class constructor calls the `Animal` class constructor using `super()`.
class Animal {
Animal() {
System.out.println("Animal is created.");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog is created.");
}
}
public class TestConstructorChaining {
public static void main(String args[]) {
Dog d = new Dog();
}
}
Console Output:
Animal is created.
Dog is created.
An abstract class in Java is a class that cannot be instantiated and is used to declare common characteristics for subclasses. It can contain abstract methods (without a body) and concrete methods (with a body).
In this example, `Animal` is an abstract class with an abstract method `sound()`, which is implemented by the `Dog` class.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
System.out.println("The dog barks.");
}
}
public class TestAbstractClass {
public static void main(String args[]) {
Animal a = new Dog();
a.sound();
}
}
Console Output:
The dog barks.
An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields.
In this example, the `Animal` interface is implemented by the `Dog` class, which provides the implementation for the `sound()` method.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("The dog barks.");
}
}
public class TestInterface {
public static void main(String args[]) {
Animal a = new Dog();
a.sound();
}
}
Console Output:
The dog barks.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies