Forum Stats

  • 3,827,843 Users
  • 2,260,829 Discussions
  • 7,897,397 Comments

Discussions

Disabling inherited method on derived class

2875841
2875841 Member Posts: 43
edited Mar 11, 2016 10:50AM in New To Java

I have a superclass

Class A{

method1();

method2();

method3();

}

class B extends A{

//dont need method 1 here;

}

class C extends A{

//all method here

}

class D extends A{

//all method here

}

so how can i exclude method1() from B because its common to C and D.Is there any way to do this in java

Tagged:
Rigoberto2875841TPD-Opitzeudriscabrera-JavaNet

Answers

  • Unknown
    edited Mar 8, 2016 12:21PM
    2875841 wrote:
    
    I have a superclass
    Class A{
    
    method1();
    method2();
    method3();
    }
    class B extends A{
    //dont need method 1 here;
    
    
    }
    class C extends A{
    //all method here
    }
    class D extends A{
    //all method here
    }
    so how can i exclude method1() from B because its common to C and D.Is there any way to do this in java
    
    

    You can't - when you extend you 'inherit' EVERYTHING - the good, the bad and the ugly.

    If you don't need method1 then don't inherit from A.

    Create a new class 'PreA' that only includes methods 2 and 3 and inherit from that. You can redefine A to inherit from 'PreA' and add method1.

    Rigoberto28758412875841
  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Mar 9, 2016 5:26AM

    You cannot exclude it, but you can change its implementation (override it). It's called polymorphism and in fact, it's one of the cornerstone concepts for the object-oriented programming. For more info, see https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

    eudriscabrera-JavaNet
  • Rigoberto
    Rigoberto Member Posts: 27
    edited Mar 10, 2016 7:30AM

    Hi!

    If you think that rp0428 answered your question, you should mark it "answered". That's how you return the favor to the user that took the time to answer your question. It helps the community to find answers, as well. :-)

    eudriscabrera-JavaNet
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Mar 10, 2016 9:25AM

    Also I'm ab bit confused that the OP marked her own answer as "helpful" and not the one that really helped...

    bye

    TPD

  • Rigoberto
    Rigoberto Member Posts: 27
    edited Mar 10, 2016 9:58AM

    I thought that one couldn't mark our own answer/questions as "answered' or "helpful'.

    Hmm. Now I am confused, as well.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Mar 11, 2016 2:55AM
    Rigoberto wrote:
    
    I thought that one couldn't mark our own answer/questions as "answered' or "helpful'.
    
    Hmm. Now I am confused, as well.
    
    
    

    OffTopic

    IMHO it's a good thing that you can mark your own answer as "correct" since sometimes the contributions do dot directly answer the question without being summarized by the OP.
    I think they left the "helpful" button on own answers too for the ease of it.
    
    At least you don't get reward points for own answers being marked...
    bye
    TPD
    
    
  • 2875841
    2875841 Member Posts: 43
    edited Mar 11, 2016 2:58AM

    HI,

    I marked my own answer as Helpful by mistake. So I have deleted my post ad marked his answer as helpful since there is no option to undo it.

    Thanks

    TPD-Opitz
  • Jiri.Machotka-Oracle
    Jiri.Machotka-Oracle Member Posts: 5,078
    edited Mar 11, 2016 4:49AM

    There's no shame in not knowing. Especially, if you are willing to learn.

    For you to understand:

    • marking the answer as Correct is perhaps even more important - it signifies that you got all the answers you needed (if not, make sure you describe where you got stuck), and also which answer helped you the most to get the clue,
    • you have 1 Correct and unlimited number of Helpful answers per one thread - don't hesitate to use them; of course, if you find the answers as such. It costs you nothing, but few clicks, and it's quite a nice way to express a thank-you to somebody who tried to help you.
    TPD-OpitzRigoberto
  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Mar 11, 2016 5:49AM

    OffTopic:

    2875841 wrote:
    I marked my own answer as Helpful by mistake. So I have deleted my post ad marked his answer as helpful since there is no option to undo it.
    
    
    

    No need do delete: "helpful" marked answeres get an "Unmark as helpful" button...

    bye

    TPD

  • Unknown
    edited Mar 11, 2016 10:50AM
    TPD-Opitz wrote:
    
    Also I'm ab bit confused that the OP marked her own answer as "helpful" and not the one that really helped...
    
    bye
    TPD
    

    OffTopic.

This discussion has been closed.