Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:43:16+00:00 2026-05-25T10:43:16+00:00

I wish to know is there any way in which I can make it

  • 0

I wish to know is there any way in which I can make it compulsory for the implementer class to declare the objects handles/primitives as they do with methods.
for e.g.:

public interface Rectangle {    
    int height = 0;
    int width = 0;

    public int getHeight();
    public int getWidth();
    public void setHeight(int height);
    public void setWidth(int width);                
}


public class Tile implements Rectangle{
    @Override
    public int getHeight() {
        return 0;
    }

    @Override
    public int getWidth() {
        return 0;
    }

    @Override
    public void setHeight(int height) {
    }

    @Override
    public void setWidth(int width) {   
    }

}

In the above method how can we compel Tile class to declare height and width attributes using the interface? For some reason I wish to do it with interface only!

I initially thought of using it with inheritance.
But thing is I have to deal with 3 classes.!

  1. Rectangle
  2. Tile
  3. JLabel.!

 

 class Tile extends JLabel implements Rectangle {}

would work.!

but

class Tile extends JLabel extends Rectangle {}

woud not.!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:43:17+00:00Added an answer on May 25, 2026 at 10:43 am

    The point of an interface is to specify the public API. An interface has no state. Any variables that you create are really constants (so be careful about making mutable objects in interfaces).

    Basically an interface says here are all of the methods that a class that implements it must support. It probably would have been better if the creators of Java had not allowed constants in interfaces, but too late to get rid of that now (and there are some cases where constants are sensible in interfaces).

    Because you are just specifying what methods have to be implemented there is no idea of state (no instance variables). If you want to require that every class has a certain variable you need to use an abstract class.

    Finally, you should, generally speaking, not use public variables, so the idea of putting variables into an interface is a bad idea to begin with.

    Short answer – you can’t do what you want because it is “wrong” in Java.

    Edit:

    class Tile 
        implements Rectangle 
    {
        private int height;
        private int width;
    
         @Override
        public int getHeight() {
            return height;
        }
    
        @Override
        public int getWidth() {
            return width;
        }
    
        @Override
        public void setHeight(int h) {
            height = h;
        }
    
        @Override
        public void setWidth(int w) { 
            width = w;  
        }
    }
    

    an alternative version would be:

    abstract class AbstractRectangle 
        implements Rectangle 
    {
        private int height;
        private int width;
    
         @Override
        public int getHeight() {
            return height;
        }
    
        @Override
        public int getWidth() {
            return width;
        }
    
        @Override
        public void setHeight(int h) {
            height = h;
        }
    
        @Override
        public void setWidth(int w) { 
            width = w;  
        }
    }
    
    class Tile 
        extends AbstractRectangle 
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to know is there any way that I can create the threads
I just wish to know if there is a simple way to call a
I am not familiar with android programming, I wish to know if there is
I have used the String Tokenizer in Java. I wish to know if there
I wish to know which are the Eclipse navigation keyboard shortcuts. I already use:
I wish to know the way to add ASP.NET (Machine_Name\IIS_IUSRS) to user role in
I wish to know what type of objects I've got in my environment. I
I want to know if there is a way to specify that in multiple
I wish to know how Old Linux scheduling algorithm SJF (shortest job first) calculates
In text fields of my JSP, I wish to know whether user is typing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.