1

I am attempting to convert a C# abstract class to a java class that has the same encapsulation and functionality. I understand you can make a class internal in Java by declaring the class without any modifiers, and this results in a private package. I would like to achieve something similar except where the class is public, some of the methods inside the class are public and some are internal.

The class I am modifying looks as follows,

//// This is C#
public abstract class Response
{
   public String Request{get; internal set;}

   public String Body{get; internal set;}

}

I would like to end up with something that ideally looks like this,

//// This is Java
public abstract class Response
{
    public abstract String getRequest(){}

    abstract String setRequest(String r){}

    public abstract String getBody(){}

    abstract String setBody(String b){}
} 

Can this be achieved?

JME
  • 2,293
  • 9
  • 36
  • 56
  • *I understand you can make a class internal in Java by declaring the class without any modifiers, and this results in a private package* that's not true, if you don't set an access level modifier it will have default access or package access, which means every class in the same package could access to this field/method. – Luiggi Mendoza May 24 '13 at 14:10
  • Also, declaring w/o a modifier (default access) will not allow derived classes in different packages to access those abstract operations (and thus override them) which is probably not desirable. – cmbaxter May 24 '13 at 14:11
  • The answer to another SO question may be helpful, http://stackoverflow.com/a/215505/3340 – Jacob Schoen May 24 '13 at 14:11
  • I see, I must have interpreted the answers in other questions incorrectly. – JME May 24 '13 at 14:12

1 Answers1

1

There should be no body of abstract class

public abstract class Response
{
    public abstract String getRequest();

    abstract String setRequest(String r);

    public abstract String getBody();

    abstract String setBody(String b);
} 

Another thing. If you dont use any modifier then it won't be private. It will be package protected. that means that can be accessible from another class within the same package.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • 1
    Here is a nice table that shows what each modifier (or no modifier) does in terms of accessability. http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Flipbed May 24 '13 at 14:15
  • Alright, after reading the answers I think I have a better understanding of the difference. Since classes in other packages will be extending this class the best route appears to be the protected modifier. – JME May 24 '13 at 14:19