Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Is it possible to inherit only specific methods of a class?

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is it possible to inherit only specific methods of a class?

    Hi there.
    I want to create my own File class to work easier with files.
    Is it possible to inherit for example only file.exists from java file class not every methods?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Is it possible to inherit only specific methods of a class?

    Why would you want to do that? That doesn't make sense to me.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to inherit only specific methods of a class?

    Quote Originally Posted by PhHein View Post
    Why would you want to do that? That doesn't make sense to me.
    Because of code encapsulation.
    I want that when my user typed "myFile." onley sees ,y classes and inherited methods I want, Not all the methods of java.file class.
    what to do?

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Is it possible to inherit only specific methods of a class?

    You can't reduce the visibility of inherited methods in subclasses. It doesn't make sense to castrate classes. Your only chance is to reimplement only those methods you need.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Is it possible to inherit only specific methods of a class?

    Please read the Announcement at the top of the sub-Forums to learn how to post your code in code/formatting tags and how to ask questions that get attention and receive helpful answers.
    Because of code encapsulation.
    Nonsensical response.

    Inherited methods can be overridden, either rewritten to perform a different function entirely or to simply "zero them out." I'm not suggesting you do this, but you could experiment with "cascading inheritance," to see if you can achieve the desired result.

Similar Threads

  1. Replies: 1
    Last Post: April 1st, 2013, 04:13 PM
  2. How do I inherit values in JavaFX?
    By Leprechaun_hunter in forum Object Oriented Programming
    Replies: 0
    Last Post: December 2nd, 2012, 04:17 PM
  3. Accessing specific class if condition is true
    By Hash in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2012, 09:06 AM
  4. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  5. [SOLVED] Help with Class Methods(Probably simple)
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 9
    Last Post: May 27th, 2010, 09:53 AM