Entri Blog
No Result
View All Result
Thursday, December 7, 2023
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Oil and Gas Course
  • Others
    • Railway
    • SSC
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions
  • Test Prep
    • PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Kerala PSC SI Exam Coaching
      • KTET Coaching
      • SET Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • Khadi Board LDC Exam Preparation
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSA Exam Preparation
      • EMRS Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
    • UPSC Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
    • RRB JE Online Coaching
      • RRB Exams Coaching
    • CAT Online Coaching
    • GATE Online Coaching
Data Science
Entri Blog
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Oil and Gas Course
  • Others
    • Railway
    • SSC
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions
  • Test Prep
    • PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Kerala PSC SI Exam Coaching
      • KTET Coaching
      • SET Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • Khadi Board LDC Exam Preparation
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSA Exam Preparation
      • EMRS Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
    • UPSC Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
    • RRB JE Online Coaching
      • RRB Exams Coaching
    • CAT Online Coaching
    • GATE Online Coaching
No Result
View All Result
Entri Blog
Spoken Eng
  • HTML Tutorial
  • DSA Tutorials
  • Tutorials in Hindi
    • HTML Tutorial for Beginners in Hindi
    • Python Tutorial for Beginners in Hindi
    • GIT and GITHUB Tutorial for Beginners in Hindi
  • Tutorials in Tamil
    • JavaScript Tutorial in Tamil
  • Tutorials in Telugu
    • HTML Tutorial For Beginners in Telugu
    • CSS Tutorial for Beginners in Telugu
    • Bootstrap Tutorial for Beginner in Telugu
  • Tutorials in Kannada
    • HTML Tutorial For Beginners in Kannada
banner top article banner top article
Home Articles

Introduction To Inheritance In Java

by Ayesha Surayya
November 19, 2022
in Articles, General Knowledge, Java Programming
Introduction To Inheritance In Java
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • Terms used in Inheritance
  • Types of inheritance in java

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Terms used in Inheritance

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

Learn to code from industry experts! Enroll here!

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

Single Inheritance 

When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

File: TestInheritance.java

classAnimal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

voidbark(){System.out.println(“barking…”);}

}

classTestInheritance{

public static void main(String args[]){

Dog d=newDog();

d.bark();

eat();

}}

Output:

barking…eating…

Multilevel Inheritance 

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

File: TestInheritance2.java

classAnimal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

voidbark(){System.out.println(“barking…”);}

}

classBabyDog extends Dog{

void weep(){System.out.println(“weeping…”);}

}

class TestInheritance2{

publicstatic void main(String args[]){

BabyDog d=new BabyDog();

weep();

d.bark();

eat();

}}

Output:

weeping…barking…eating…

Learn to code from industry experts! Enroll here!

Hierarchical Inheritance 

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

File: TestInheritance3.java

classAnimal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

voidbark(){System.out.println(“barking…”);}

}

classCat extends Animal{

void meow(){System.out.println(“meowing…”);}

}

class TestInheritance3{publicstatic void main(String args[]){

Cat c=new Cat();

meow();

c.eat();

//c.bark();//C.T.Error

}}

Output:

meowing…eating…

Multiple Inheritance

Java does not support multiple inheritance. Multiple inheritance means a class derived from more than one direct super class. This increases complexities and ambiguity in the relationship among classes. The problem is clearly visible if we consider what happens in function overriding. Suppose there are two classes, A and B, each defining a function called func(). Now, let’s say we define another class, C, which inherits both from A and B (multiple inheritance), but let’s say this class does not override the function called func().

Now, if we do something like the following:

C c = new C();c.func();

Can we determine which member function func() is invoked? Is it function defined by class A or class B? As we can see, the class C inherits this function doubly from both A and B. This creates ambiguity and the compiler is in a fix to resolve the issue. There is solution to this problem, but it’s extremely complex; therefore, Java decided to stay away from the cause of the problem by not allowing multiple inheritance.

Multiple Inheritance with Interfaces

Although Java does not allow multiple inheritance, it allows a multiple implementation of interfaces. So, in a way the idea is still there. Now, what is an interface?

An interface describes a set of methods but does not provide any concrete implementation for all methods. We can implement the interface with the help of a class which provides the concrete implementation of the method. In this way, a class can implement more than one interface and therefore can provide a concrete implementation of methods derived from one or more interfaces. The class that implements one or more interfaces forms a is-a relationship with the interface type. This also means that objects instantiated from the class are guaranteed to provide the functionality declared by the interface. Any subclass derived from this class also provides the same functionality.

Interfaces are especially useful for providing a common functionality to a number of possibly unrelated classes. Therefore, the object of classes that implement same interface can respond to method calls described in all of the interfaces.

From Java 8, interfaces support default methods with its full implementation. As we know, a class can implement more than one interface; therefore, if multiple interfaces contain a default method with the same method signature, the implemented class should specify which particular method to use or override.

Example

package com.mano.examples;

public interface Interface1 {

default void func(){

System.out.println(“from interface 1”);

}}

package com.mano.examples;public interface Interface2 {

default void func(){

System.out.println(“from interface 2”);

}}

package com.mano.examples;public class MyClass implements Interface1, Interface2 {

public void func(){      // Invoking Interface1 func() method

Interface1.super.func();      // Invoking Interface2 func() method      Interface2.super.func();

}

public static void main(String[] args){

MyClass c=new MyClass();      c.func();

}

Learn Coding in your Language! Enroll Here!

Why is it important to choose Entri?

  • Excellent online platform for all the Competitive Exams.
  • Provides updated materials created by the Entri Experts.
  • Entri provides a best platform with full- length mock tests including previous year question papers.
  • You can download the app for free and join the required classes.
  • Entri wishes you all the best for your examinations and future endeavours.

“YOU DON’T HAVE TO BE GREAT TO START, BUT YOU HAVE TO START TO BE GREAT.”

 

 

Share61SendShare

Ayesha Surayya

Related Posts

How to Become Performance Marketer in India [2024]
Articles

How to Become Performance Marketer in India [2024]

December 5, 2023
Top 30 Montessori teacher interview questions and answers You Need to Know in 2024
Articles

Montessori Teacher Interview Questions and Answers You Need to Know in 2024

December 5, 2023
7 Benefits Of Online Trading: Why You Should Be Online?
Articles

7 Benefits Of Online Trading: Why You Should Be Online?

December 5, 2023
Next Post
Deadlock Prevention and Avoidance

Deadlock Prevention and Avoidance

Latest Posts

  • Kerala bank clerk Notification expected soon 2024
  • How to Crack German A1 in Two Months: A step-by-step Guide!
  • How to Crack German B1 in Two Months: A step-by-step Guide!
  • MEP Engineer Interview Questions and Answers
  • Top 5 Performance Marketing Certifications to Kickstart Your Career

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    152243 shares
    Share 60894 Tweet 38059
  • List of Government Banks in India 2023: All you need to know

    65548 shares
    Share 26219 Tweet 16387
  • Kerala Bank Assistant Manager Syllabus 2023: Download Expected Syllabus PDF

    701 shares
    Share 280 Tweet 175
  • KSDA Recruitment 2023 Apply Online for 9264 FDA SDA Posts – Qualification

    9449 shares
    Share 3780 Tweet 2362
  • TNPSC Group 2 Posts and Salary Details 2022

    40012 shares
    Share 16005 Tweet 10003

Courses

  • Data Science Course
  • Full Stack Developer Course
  • Data Science Course in Malayalam
  • Full Stack Developer Course in Malayalam
  • Full Stack Developer Course in Hindi
  • Full Stack Developer Course in Tamil
  • Full Stack Developer Course in Telugu
  • Full Stack Developer Course in Kannada
  • Python Programming Course
  • Practical Accounting Course
  • Quantity Surveying Course
  • Stock Market Course
  • Stock Market Course in Malayalam
  • Stock Market Course in Tamil
  • Stock Market Course in Kannada
  • Options Trading Course

Company

  • Become a teacher
  • Login to Entri Web

Spoken English Courses

  • Spoken English Course
  • Spoken English Course in Malayalam
  • Spoken English Course in Hindi
  • Spoken English Course in Tamil
  • Spoken English Course in Telugu
  • Spoken English Course in Kannada
  • Spoken English Course for Housewives
  • Spoken English Course for Working Professionals
  • Spoken English Course for School Students
  • Spoken English Course for College Students
  • Spoken English Course for Job Seekers
  • AI Powered Spoken English Course

Quick Links

  • Aptitude Questions
  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling
  • Kerala PSC

Other Courses

  • OET Coaching Classes
  • Nurse Recruitment Abroad
  • Forex Trading Course
  • Montessori Teachers Training
  • Performance Marketing Course
  • German Language Course
  • MEP Course
  • Oil and Gas Course Online
  • Yoga Teacher Training Course
  • Digital Marketing Course
  • German Language A1 Course
  • German Language A2 Course
  • German Language B1 Course
  • German Language B2 Course

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2023 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Oil and Gas Course
  • Others
    • Railway
    • SSC
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions
  • Test Prep
    • PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Kerala PSC SI Exam Coaching
      • KTET Coaching
      • SET Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • Khadi Board LDC Exam Preparation
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSA Exam Preparation
      • EMRS Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
    • UPSC Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
    • RRB JE Online Coaching
      • RRB Exams Coaching
    • CAT Online Coaching
    • GATE Online Coaching

© 2023 Entri.app - Privacy Policy | Terms of Service