Welcome Guest ( Log In | Register )

Bump Topic Topic Closed RSS Feed

Outline · [ Standard ] · Linear+

 Java Interfaces and Variables, Some yes, some no

views
     
TSUserU
post Jan 15 2019, 05:33 PM, updated 6y ago

CSONLINE2.NET - FREE COUNTER-STRIKE
Group Icon
Elite
5,013 posts

Joined: Mar 2009
From: Land of make believe

I'm a little confused here. I read up some articles on Interfaces that variables are not allowed to be declared:

According to this article, the compiler would run into an error:

CODE
interface TestInterface  
{  
   int x = 4;  // Filed Declaration in Interface  
   void getMethod();  
   string getName();  
}  
 
abstract class TestAbstractClass  
{  
   int i = 4;  
   int k = 3;  
   public abstract void getClassName();  
}  



But the ones below would run without any:
CODE

Source: http://tutorials.jenkov.com/java/interfaces.html#java-interface-example

public interface MyInterface {

   public String hello = "Hello";

   public void sayHello();
}


CODE

Source: https://beginnersbook.com/2013/05/java-interface/

interface Try
{
  int a=10;
  public int a=10;
  public static final int a=10;
  final int a=10;
  static int a=0;
}


Anyone could explain why?
cordelialoo
post Jan 15 2019, 05:45 PM

New Member
*
Newbie
44 posts

Joined: Mar 2014
string getName(); this part, i find it suspicious without void

Can Java Sifu help to explain this part?
cytyler
post Jan 15 2019, 05:46 PM

Casual
***
Junior Member
383 posts

Joined: Nov 2014
i have wrong read the question sorry ..wrong explanation

This post has been edited by cytyler: Jan 15 2019, 05:51 PM
Ryutaro
post Jan 15 2019, 07:53 PM

~ Just a noob programmer ~
****
Senior Member
541 posts

Joined: Mar 2011
ok not sure did you realize it but the first piece of code you get from is using C#
and the one at the bottom is using java...

unless you are asking even deeper question as to why C# forbid it and Java allow it

This post has been edited by Ryutaro: Jan 15 2019, 07:54 PM
TSUserU
post Jan 16 2019, 12:02 PM

CSONLINE2.NET - FREE COUNTER-STRIKE
Group Icon
Elite
5,013 posts

Joined: Mar 2009
From: Land of make believe

QUOTE(Ryutaro @ Jan 15 2019, 07:53 PM)
ok not sure did you realize it but the first piece of code you get from is using C#
and the one at the bottom is using java...

unless you are asking even deeper question as to why C# forbid it and Java allow it
*
My bad, just noticed it.

I was recently exploring Java interfaces and only sighted more of those with just abstract methods hence my curiosity. But from the bold statement, do you mind explaining a bit?

This post has been edited by UserU: Jan 16 2019, 12:04 PM
Ryutaro
post Jan 16 2019, 05:31 PM

~ Just a noob programmer ~
****
Senior Member
541 posts

Joined: Mar 2011
QUOTE(UserU @ Jan 16 2019, 12:02 PM)
My bad, just noticed it.

I was recently exploring Java interfaces and only sighted more of those with just abstract methods hence my curiosity. But from the bold statement, do you mind explaining a bit?
*
An interface is like a contract where if another class were to implement that interface, that class agree to implement the methods that were stated in the interface, having an interface make sure that when you write the class you will not forget to implement certain methods that are required for your program to work, take a shape interface with a calculateArea(float height, float width) method signature as an example both square and rectangle class need to have area method signature implemented in order for your program to calculate the area of square and rectangle correctly, if you forgot to implement calculateArea() on either square or rectangle class the compiler will throw an error

tbh I am not sure about why Java decide to allow interface with constant variables actually but their documentation did mentioned that they allow constant variable (meaning that variable will not change throughout execution of programs)

c#'s documentation stated that interface should not include any variables in it

my personal opinion lean towards not declaring variable in interface as there might be class that doesn't need that constant variable

Lord Tiki Mick
post Jan 16 2019, 06:27 PM

Regular
******
Senior Member
1,018 posts

Joined: Jul 2012
Java interfaces can contain fields. These fields though will become a static field.
E.g.
CODE

interface Hello {
 String WORLD = "World";
}

System.out.println(Hello.WORLD); // will print World.


Moreover, since Java 8, interfaces also can contain default methods.
TSUserU
post Jan 17 2019, 12:24 PM

CSONLINE2.NET - FREE COUNTER-STRIKE
Group Icon
Elite
5,013 posts

Joined: Mar 2009
From: Land of make believe

QUOTE(Ryutaro @ Jan 16 2019, 05:31 PM)
An interface is like a contract where if another class were to implement that interface, that class agree to implement the methods that were stated in the interface, having an interface make sure that when you write the class you will not forget to implement certain methods that are required for your program to work, take a shape interface with a calculateArea(float height, float width) method signature as an example both square and rectangle class need to have area method signature implemented in order for your program to calculate the area of square and rectangle correctly, if you forgot to implement calculateArea() on either square or rectangle class the compiler will throw an error

tbh I am not sure about why Java decide to allow interface with constant variables actually but their documentation did mentioned that they allow constant variable (meaning that variable will not change throughout execution of programs)

c#'s documentation stated that interface should not include any variables in it

my personal opinion lean towards not declaring variable in interface as there might be class that doesn't need that constant variable
*
QUOTE(Lord Tiki Mick @ Jan 16 2019, 06:27 PM)
Java interfaces can contain fields. These fields though will become a static field.
E.g.
CODE

interface Hello {
 String WORLD = "World";
}

System.out.println(Hello.WORLD); // will print World.


Moreover, since Java 8, interfaces also can contain default methods.
*
So that means the variables have to be declared static and final. For the reason why C# isn't able to accommodate variables, I guess properties are only allowed according to some thread on SO.

Thanks both of you!

Topic ClosedOptions
 

Change to:
| Lo-Fi Version
0.0152sec    0.30    5 queries    GZIP Disabled
Time is now: 29th March 2024 - 02:58 AM