CrazyEngineers
  • 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
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • The_Small_k

    MemberSep 4, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberSep 4, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 4, 2013

    Thanx durga
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 4, 2013

    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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 4, 2013

    @#-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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 4, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 4, 2013

    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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 4, 2013

    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
    <a href="https://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#start()" target="_blank" rel="nofollow noopener noreferrer">Thread (Java Platform SE 6)</a>()
    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 ☕
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 4, 2013

    Yes because it helps implementing mulpile inheritence . Extending a class can affect execution time
    Are you sure? This action cannot be undone.
    Cancel
  • vikaskumar11233

    MemberSep 4, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 4, 2013

    Why java doesn't support multiple inheritance ie. extending more than one class by a class?
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 4, 2013

    because that becomes obsolete when there is multilevel and hierarchical type inheritances .
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 4, 2013

    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 ?
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 5, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anand Tamariya

    MemberSep 5, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 9, 2013

    Anand Tamariya
    C++ and python supports multiple inheritance - so it's not necessarily obsolete.
    i meant obsolete to java
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 9, 2013

    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!!
    Are you sure? This action cannot be undone.
    Cancel
  • Alok mishra

    MemberSep 9, 2013

    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!!
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 9, 2013

    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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register