Java basics: Why is overriding used?

I couldnt get the benefit of overriden methods !! Why overriding is used ? Is it a feature or demerit of java ? Methods declared in any interface need to be overriden or implemented by some other class ?

Replies

  • The_Small_k
    The_Small_k
    Overriding a method in a subclass is the concept come under OOP's.
    Abstraction is one another feature of OOP's that will be let you override the super class methods in sub classes.
    .
    If you implement an interface you have to override all the methods in it or you can take the adapter classes in between them if you don't want to override all or want to use some of them only.
  • durga ch
    durga ch
    I am not a professional programmer. As what I learnt, consider you have a class living creature and you have a method motion , breathe, yawn, sleep, run, eat etc.

    now when you are defining say new classes of a man and a seal , both of them have few methods in common, so both man and seal can be derive from animal class, but man can walk(motion) and a seal crawls(motion), only because there is difference in how they move, you wont end up re-writing a complete new class. that is when methods are overridden. to customise them .

    CS CEans: you are absolutely welcome to correct my view.
  • Alok mishra
    Alok mishra
    Thanx durga
  • Alok mishra
    Alok mishra
    While creating threads either we implement runnable or extend thread class , in both cases we override run method . I want to know if overriding has its worth only with abstract methods or if not so then how ,the methods which are mostly overriden, behaves in its actual form ?
  • Kaustubh Katdare
    Kaustubh Katdare
    @#-Link-Snipped-# - Please avoid starting separate threads on the similar topics and always provide a very appropriate title to the discussions. It helps others quickly understand what the topic is about.
  • Anoop Kumar
    Anoop Kumar
    Always remember to compare Java/Object oriented concepts with real world object.
    Overriding is writing same method with different implementation in parent class and child class.

    Now suppose you have following structure of your classes

    Animal (I) {method: motion}
       |
      / \__
     /      \
    Seal    Man (c)
    (c)           \
                  UsionBolt (c)
    (I)-Interface
    (c) -Class
    Let say these three classes have overrriden method motion() and implemented according to their need.
    Now as per coding standard you will write like this
    Animal animalA = new Seal();
    Animal animalB  = new Man();
    Animal animalC  = new UsionBolt();
    
    Creating new object is not necessary to be created like this and quite possible that there will be more than two level of hierarchy of classes.
    the variable animal* is mostly independent of the implementation.
    Now, if you call animal*.motion(); depending on type of animal you provided Seal will crawl , man will walk and UsionBolt will run.

    overriding and overloading are not the demerit but the one of most used feature of Object Oriented concept.
  • Alok mishra
    Alok mishra
    Yes gud elaboration , but i want to know that the methods which are overriden like
    run , init , doget, dopost , actionperformed etc are abstract ? If not then whats their actual behaviour ? I feel overriden methods are abstract in their original classes ,is it so ?
  • Anoop Kumar
    Anoop Kumar
    For a thread creation you have to implement run(), right. and to run thread you need to call Thread.start() which implicitly does following.

    From, javadoc.

    void
    Thread (Java Platform SE 6)()
    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
    So you have to implement run() and then write your code inside it to run as separate thread.
    I feel overriden methods are abstract in their original classes ,is it so ?
    It's upon your requirement that your original overridden method should be abstract or concrete. see my first example in Animal interface , method motion is abstract while in class Man and UsionBolt it's concrete. you can code without interfaces but it is highly recommended to have interface and you should access any class by interface type only.

    Why interface type, you should Google about it ☕
  • Alok mishra
    Alok mishra
    Yes because it helps implementing mulpile inheritence . Extending a class can affect execution time
  • vikaskumar11233
    vikaskumar11233
    Alok mishra
    Yes because it helps implementing mulpile inheritence . Extending a class can affect execution time
    Dear, JAVA doesn't support multiple inheritance. Interface is used to implement multiple inheritance in JAVA.
  • Anoop Kumar
    Anoop Kumar
    Why java doesn't support multiple inheritance ie. extending more than one class by a class?
  • Alok mishra
    Alok mishra
    because that becomes obsolete when there is multilevel and hierarchical type inheritances .
  • Anoop Kumar
    Anoop Kumar
    Alok mishra
    because that becomes obsolete when there is multilevel and hierarchical type inheritances .
    Nope, multilevel works and that's is true property of inheritance.
    Like in example man is inheriting Animal and Man again inherited by Usion Bold so UsionBolt will have default properties of Animal+Man.

    Question is why can't a class can extend two classes, what will be consequences ?
  • Alok mishra
    Alok mishra
    ianoop
    Nope, multilevel works and that's is true property of inheritance.
    Like in example man is inheriting Animal and Man again inherited by Usion Bold so UsionBolt will have default properties of Animal+Man.

    Question is why can't a class can extend two classes, what will be consequences ?
    Because there rises no need of it , one can inherit power of two classes by using only multilevel inheritence . Thats why i called multiple inheritence obsolete.
  • Anand Tamariya
    Anand Tamariya
    Alok mishra
    Because there rises no need of it , one can inherit power of two classes by using only multilevel inheritence . Thats why i called multiple inheritence obsolete.
    C++ and python supports multiple inheritance - so it's not necessarily obsolete.
  • Alok mishra
    Alok mishra
    Anand Tamariya
    C++ and python supports multiple inheritance - so it's not necessarily obsolete.
    i meant obsolete to java
  • Anoop Kumar
    Anoop Kumar
    No, it's not absolute. The problem is when you are extending two classes and both parent class have any common method.
    How could you solve this problem!!
  • Alok mishra
    Alok mishra
    I am not damn sure but it can work - Make that method static and access that method by only using class name, ex. System.exit (0), exit() is static in class System.
    ianoop
    No, it's not absolute. The problem is when you are extending two classes and both parent class have any common method.
    How could you solve this problem!!
    ianoop
    No, it's not absolute. The problem is when you are extending two classes and both parent class have any common method.
    How could you solve this problem!!
  • Anoop Kumar
    Anoop Kumar
    you are not getting the point.
    Multiple inheritance in java is not supported because two classes can have different implementation of same method.

    And keep in mind when you inheriting, you can't just change parent method to static. What if that class is being inherited by 20 classes, you never know the impact of this change.
    Even quite possible you don't have access to change parent class.

    When you are thinking in java, try to think more in perspective of business rather than just tricks.

You are reading an archived discussion.

Related Posts

We all knew that the great legends of bikes Kawasaki,Suzuki,yamaha and Ducati.Here are two crazy bikes released from Kawasaki for Indian customers and bike lovers.The monsters to be on Indian...
Hello everyone, I need an 8051 trainer kit. Since I am almost done with the theory, I think it will be good to prepare projects. So can you guys please...
My Mobile-Odin is unfortunately giving me error report & thus I am unable to flash Stock ROM or anyother custom Kernel without using it. Also there is no Linux version...
Most of developers know number of technologies to work with but prefer some specific combination which they like most. As i like to work with the combination of Struts1 +...
Top 5 standard courses in engineering field: 1. Computer Science & Engineering/IT 2. Electronics & Communication Engineering 3. Electrical & Electronics Engineering 4. Mechanical Engineering 5. Textile Engineering 1. Computer...