Results 1 to 2 of 2
  1. #1
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    6

    Default Implementing classes instead of extending to get methods?

    Basically I want to make a class called library, but I don't want to make an interface because I actually want to define the methods. I think I can only use abstract classes but not really sure how to use those. But I still have a problem, I want to create a Map that classes implementing Library class have to have in their code, and the Map will be a HashMap with <String, ParentClassHere>, so basically let's say I make a class called Car, implemeing Library to the Car class would create a Map library = new HashMap<String, Car>. Can I do something like this? And also include methods to get values and set values to the library Map?

    Regards
    augustas656

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Implementing classes instead of extending to get methods?

    First you are mixing up terminology. You say you don't want Library to be an interface yet you
    talk about Car implementing it. Car would extend the class. Also, super classes do not
    know of their subclasses. You could probably do what you want with generic types.

    Java Code:
    class Library<T> {
       private Map<String, T> library = new HashMap<>();
       // other code here - e.g setters and getters
    }
    
    class Car extends Library<Car> {
       // code as necessary
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Alternative to extending classes from interfaces?
    By TacoManStan in forum New To Java
    Replies: 5
    Last Post: 09-10-2011, 03:31 AM
  2. I'm having trouble with extending classes
    By ziongio in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 07:33 AM
  3. Extending Classes and What is Necessary
    By GavinCash in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 07:07 AM
  4. Got confused with extending classes.
    By nethz13 in forum New To Java
    Replies: 7
    Last Post: 04-19-2010, 12:19 AM
  5. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •