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

    Default Can you get a sub-class' interface implementation in the interface?

    Hello,

    So if you have a class called ClassA that implements an interface called InterfaceA, that contains an unimplemented instance method called methodA. Can you virtually obtain the implementation of methodA from the sub-class' and call it, to return its value, in a static method in the interface?

    Kind Regards,
    augustas656
    Regards
    augustas656

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Can you post some code showing what you are trying to do?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Can you get a sub-class' interface implementation in the interface?

    A few remarks:

    1) an interface doesn't know what class(es) implement that interface;
    2) an interface doesn't have static methods.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Quote Originally Posted by JosAH View Post
    2) an interface doesn't have static methods.
    Not anymore...
    https://docs.oracle.com/javase/tutor...ltmethods.html
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Quote Originally Posted by Tolls View Post
    Yeah, sort of a half-abomination feature that is a necessary evil for the new lambda-driven age. you can basically skip most of that article and go all the way to the bottom of it to learn the real reason why this was added. To basically be able to re-use interfaces and not have to add double the amount of classes to the JDK.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Quote Originally Posted by Tolls View Post
    Oh no, not those post-modernistic big city frillies again ... ;-)

    kindest regards,

    Jos (<--everything used to be better in the old days)
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Quote Originally Posted by gimbal2 View Post
    To basically be able to re-use interfaces and not have to add double the amount of classes to the JDK.
    Actually, I believe you are talking about default methods (but for the reason you cite). Static methods also exist.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Let's presume there is some magical way to do what you want. Why would you want to?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Quote Originally Posted by jim829 View Post
    Actually, I believe you are talking about default methods (but for the reason you cite). Static methods also exist.

    Regards,
    Jim
    In this rant they can be lumped together :) As does the article, I quote:

    "This example demonstrates how the Comparator interface has been enhanced with default methods, static methods, lambda expressions, and method references to create more expressive library methods whose functionality programmers can quickly deduce by looking at how they are invoked"
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Can you get a sub-class' interface implementation in the interface?

    Sorry for the delay,

    I think I replied to this topic but somehow the post isn't here, peculiar... Anyway, I'll begin by writing some code:

    Java Code:
    package org.test.test;
    
    public abstract class Glass {
    	public final String shape;
    	public abstract int getOunceCapacity();
    	
    	public Glass(String shape) {
    		this.shape = shape;
    	}
    	
    	public static void main(String[] args) {
    		Small.getOunceCapacity();	// Error, I know why, but the value is hard-coded, workaround?
    		Medium.getOunceCapacity();	// Error, I know why, but the value is hard-coded, workaround?
    		Large.getOunceCapacity();	// Error, I know why, but the value is hard-coded, workaround?
    	}
    	
    	public static class Small extends Glass {
    		public Small(String shape) {
    			super(shape);
    		}
    
    		@Override
    		public int getOunceCapacity() {
    			return 6;
    		}
    	}
    	
    	public static class Medium extends Glass {
    		public Medium(String shape) {
    			super(shape);
    		}
    
    		@Override
    		public int getOunceCapacity() {
    			return 8;
    		}
    	}
    	
    	public static class Large extends Glass {
    		public Large(String shape) {
    			super(shape);
    		}
    
    		@Override
    		public int getOunceCapacity() {
    			return 12;
    		}
    	}
    }
    Now another way to accomplish the glass capacity is to instead of making an abstract method for getOunceCapacity I can make a final instance field with capacity which is initialized in the constructor, therefore must be in every sub-class constructor. That's not the issue though, as you may see. These method-body hard-coded values are practically static, they are class-level as method implementations are.

    Kind Regards,
    augustas656
    Regards
    augustas656

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

    Default Re: Can you get a sub-class' interface implementation in the interface?

    I still don't understand what you are trying to accomplish. If a given glass size may have a different shape then just do it like you are doing but create
    an instance of the nested class. What is wrong with the following for example:

    Java Code:
    Small smallGlass = new Small("Goblet");
    System.out.println(smallGlass.getOunceCapacity());
    And since your super class contains main, it makes it cumbersome to choose which class to load.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    augustas656 is offline Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    193
    Rep Power
    7

    Default Re: Can you get a sub-class' interface implementation in the interface?

    The only reason I wrote the sub-classes of Glass within itself because I wasn't bothered writing Glass suffix and copying from separate class files in eclipse. And I write my code so it is usable like a library or a work able to be easily extended and adapted by others because it's mainly for creating games which I would like others to be able to create mods for, and easily. Saying that, let's say someone else other than me wanted to have a Huge glass size and added that, but the ounce capacity is only obtainable via a singleton even though it shouldn't in, terms of memory and not that much java, I haven't thought of a reason but there may be that ounce capacity should be able to be accessed in a static manner. I try to make my project code as flexible as possible, for myself and others, every time I use a single-time complex structure I make sure to reform it into classes even though I don't plan to use it later, this is because it often results in actually proving useful and is also adaptable by others. If you think there couldn't possibly be a problem without a workaround please tell me.

    Kind Regards,
    Kreative
    Regards
    augustas656

Similar Threads

  1. Interface - Implementation link
    By vasanthj in forum Advanced Java
    Replies: 7
    Last Post: 10-05-2012, 10:23 AM
  2. Replies: 1
    Last Post: 07-08-2012, 05:23 PM
  3. interface implementation issue
    By sat123stud in forum New To Java
    Replies: 1
    Last Post: 08-29-2011, 09:00 AM
  4. Interface Implementation
    By Samyx in forum New To Java
    Replies: 3
    Last Post: 12-02-2009, 12:20 PM
  5. Interface and implementation erorr
    By mihael in forum Advanced Java
    Replies: 1
    Last Post: 12-07-2008, 11:06 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
  •