What are your thoughts on Java inheritance?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arushi
    New Member
    • Oct 2022
    • 7

    What are your thoughts on Java inheritance?

    Which changes do I need to make to the code below in order for a.f2(); to work?

    Code:
    public class A {
      void f1(){}   
    }
    
    public class B extends A {
      void f2(){}
    }
    
    void main(String[] args) {
      A a =  ...     <- free space here
      a.f1();
    }
    I've been asked to change the way I added void f2(){} to class A.

    No additional free space was included in this code.
    I went through some resources but didn't get it.
  • cetpainfotech
    New Member
    • Jan 2023
    • 15

    #2
    Inheritance is a fundamental concept in object-oriented programming, and Java fully supports it. Inheritance allows a class to inherit properties and methods from a parent class, which can greatly simplify code reuse and organization. However, it is important to use inheritance judiciously and not overuse it, as it can lead to complex and difficult-to-maintain code if not used correctly. Additionally, Java also supports interfaces, which allow for a form of polymorphism called "interface polymorphism" which is also a powerful technique for code reuse and organization.

    Comment

    • pritikumari
      Banned
      New Member
      • Jan 2023
      • 23

      #3
      According to my thoughts 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*a lso.

      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*sys tem).

      Inheritance represents the IS-A relationship which is also known as a parent-child*relations hip.

      The most important use of inheritance in Java is code reusability. The code that is present in the parent class can be directly used by the child class. Method overriding is also known as runtime polymorphism. Hence, we can achieve Polymorphism in Java with the help*of*inherit ance.

      Comment

      Working...