Interface (Methods Public ????)
hello friends,
In interface , Y are the methods default declared as public???? and Y cannot we declare them as friend or protected ?
Can anybody tell me in details abt the same ????
Declaring protected would be unusual too since only
descendants can access protected methods and an
interface cannot have descendants of course you mean only descendands and classes
in the
same package can access protected methods ... :)Well yes, though redundant for this argument since that is already covered by package-private access. The difference between the two access-levels, the descendants, is an empty set for interfaces.
This should act a response to the previous post also - because it would make no sense to allow protected. I can see, and demonstrate, cases where package-private is of value however.
Sun made the decision it seems without full consideration. They cannot now change that decision because of the implicit manner in which package-private is indicated to the compiler (by the absence of another access level). That's a problem since the compiler automatically infers 'public' access for methods in interfaces in cases where the grammar would normally indicate package-private. Changing this would break a massive amount of code.
An option would be to introduce a new keyword 'friendly' or 'packageprivate' and keep the current rules for implied access. This would allow the new access without breaking much code. The only code to break would be that which uses the new keyword for another purpose.
These collisions would be uncommon because:
- The name of a type uses the convention of a leading uppercase first letter.
- The name of a method uses the conventions for property accessors and
mutators using the prefixes 'get', 'set', 'is' and 'has' or is a behaviour and
more likely to use a verb.
local variables and fields are the only likely collisions - these should be minimal and not used in exposed APIs.
Talden
Similar Messages
-
Why are interface methods public?
I want to define a package interface that will be implemented only by package classes which will be used only by other package classes. Why do the methods have to be public?
In java you don't have multiple inheritance. To
circumvent that it is said to extend the most
important class and implement the others with
interfaces. No. Java does not support multiple inheritance. Interfaces do not exist to supply that functionality. Interfaces can be used to simulate some of the patterns that multiple inheritence represents.
(You can have skeleton implementations to
help you implement those interfaces in other
classes).
But the interface methods are public. So all the
classes you 'extend' via this trick have all methods
public.
This is a bad thing. All of you who have used RMI (or
J2EE) know that you can serialize objects between two
networked machines. ValueObjects pattern from J2EE
toolbox is a good example of this. Now, if you happen
to have several quite similar objects, you obviously
would like them to inherit those common traits from
some base class. If you would also like them to have
some other common functionality (for example server
side 'maintenance' methods, state monitoring, checking
and clearing, you would have to make those methods
public 'cos you only have interface implementation
left - after spending that single credit to actual
inheritance.No. Convienence ("common functionality") should not be used in most cases when designing hierarchies. Inheritence should only be used to represent 'is a' relationships. -
Access specifiers for interface methods
When we implement the interface ,we have to specify the implementing method access specifier to be "PUBLIC" ,but not "PROTECTED" or "PRIVATE".
Compiler is giving an error -- attempting to assign weaker access privileges ,when we specify protected.
what is the internal reason for this?Why we shouldnt make the access weaker in the implementing class.
Can any one help me on this.Your help is highly appreciated.When we implement the interface ,we have to specify
the implementing method access specifier to be
"PUBLIC" ,but not "PROTECTED" or "PRIVATE".
Compiler is giving an error -- attempting to assign
weaker access privileges ,when we specify protected.
what is the internal reason for this?There is absolutely no point in having a private interface method. The interface represents a visible abstraction and private methods are never visible so it is a contradiction in terms.
An interface is intended to represent an abstraction that a user (software) uses. Protected via child/parent represents a usage that is restricted to a child from a parent. The child can already see the parent so there is no point in having an abstraction for the child. And it would probably be unwise to limit a child by such an abstraction.
Protected via the package and interfaces is more contentious as to why it is not allowed. There are those that argue that this should be allowed so that a package can use interfaces but restrict them to the package. To me this seems like a minor point given that most interfaces will probably represent an abstraction to other packages and not within a single package. This applies specifically to default access as well. -
Interface method implementation
While Implementing a method gety() i am getting follwing error -
gety() in class B cannot implement gety() I ;attempting to assign weaker access privilees; was public
class B extends A implements I {
int i;
public B(int a,int b) {
int q = super.i;
System.out.println(q);
i = b;
void show() {
System.out.println(X);
System.out.println("i in subclass:" +i);
void gety() {
System.out.println("my");
interface I {
public static final int X=3;
abstract void gety();
}If interface I says gety is public, then code that
uses an I expects it to have a public gety method.
Since B "is-a" I (by virtue of the fact that it
implements I), it has to do everything I promisesto
do.Nice class names... it makes it sound like you just
have really bad grammar :)Just following what the OP had. But yeah, I noticed that as I was typing, but couldn't be bothered to change it. -
Overriding Interface methods via JNI
Hello,
How we can override Interface methods via JNI?
For example:
At HelloWorldSwing example:
http://download.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java
I've written anonymous Interface as:
import javax.swing.*;
public class HelloWorldSwing1 {
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
static Runnable runnableObject = new Runnable() {
public void run() {
createAndShowGUI();
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(runnableObject);
}via JNI functions(http://download.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html) we cannot override Runnable Interface's run method? Can we?
Only:
RunnableClass's value isn't null with given example at below. FindClass finds the Runnable Interface.
jclass RunnableClass;
RunnableClass = (*env)->FindClass(env,"java/lang/Runnable");
if( RunnableClass == NULL ) {
printf("can't find class Runnable\n");
exit (-1);
}But How can we override Runnable class's run method?
Thanks in AdvanceThe only way you can override a method in Java is by defining a class that does so, either statically or as an anonymous inner class. You can't do either of those things in JNI. Ergo you cannot do what you are asking about.
-
Apply static method requirements on class (Static interface methods)?
I have a set of classes where is class is identified with a unique ID number. So each class has a public static int getId() method. Each class also has either a constructor that takes a byte array or a static factory method that takes a byte array.
I would like to maintain a hashmap of these classes keyed to the ID's however I am not sure how to have some interface of abstract parent class that requires each of these classes to implement these static methods.
What I was hoping to do was something like this>>>
public interface MyClasses{
static MyClasses createInstance(byte[] data);
HashMap<Integer, MyClasses> map;
public void init() {
map = new HashMap<Integer, MyClasses>();
map.put(Class1.getId(), Class1.class);
map.put(Class2.getId(), Class2.class);
public void createInstance (int id, byte[] data) {
map.get(id).createInstance(data);
}Is there some way I could do this?What about something like this?
public interface Initializable
public void initialize(byte[] data);
public class Factory
private final Map<Integer, Initializable> map = new HashMap<Integer, Initializable>();
public void registerClass(int id, Class klass)
if (! Initializable.class.isAssignableFrom(klass))
// you may also want to ensure the class declares a parameterless constructor
throw new IllegalArgumentException("duff class");
if (this.map.keySet().contains(id))
throw new IllegalArgumentException("duff id");
this.map.put(id, klass);
public Initializable createInstance(int id, byte[] data)
// need some exception handling of course
Initializable i = map.get(id).newInstance();
i.initialize(data);
return i;
} -
Class/Interface/Methods
Hi All,
I'm trying to understand the concept behind abap class/interface/methods.
1. Is it mandatory to have an interface in every class?
2. Once a class is created is it right to define methods as static and public and use them without reference to the interface?
Any help appreciated.
Meghnahi meghna,
interfaces in ABAP Objects play the same role as classes. Just like classes, interfaces are object types that reside in the namespace of all types. While a class describes all aspects of a class, an interface only describes a partial aspect
The syntax for declaring a local interface is:
INTERFACE intf.
DATA ...
CLASS-DATA ...
METHODS ...
CLASS-METHODS ...
ENDINTERFACE.
Basically, the declaration of an interface corresponds to the declaration part of a class, where instead of CLASSu2014ENDCLASS, you simply use INTERFACEu2014ENDINTERFACE. Interfaces can contain exactly the same components as classes. Unlike classes, however, interfaces don't need to be divided into different visibility sections because interface components are always integrated in the public visibility section of classes.
i dont want to confuse you .
please go to the below link to know more about... interface and classes.
[http://help.sap.com/saphelp_nw04/helpdata/en/ec/d9ab291b0b11d295400000e8353423/frameset.htm] -
Implementing Empty Versions of Interface Methods with Return Types
Subject probably is probably a bit long but I was stumped for a brief way to summarize my question. Let's say I have the interface below:
public interface DAO {
...snip...
public Object get(int id);
public Object[] get(String clause);
...snip...
}From this I want to make two concrete classes, say ClassOne and ClassTwo. Given the type of data each one is fetching, ClassOne only needs (and should only) implement get(int id) and ClassTwo only needs (and should only) implement get(String clause). This is because the first class will always deal with only one record from table_a and the second class will always deal with one or more records from table_b. So my question is what is the correct way to provide empty implementations for the unnecessary methods?
I've thought of using an adapter to implement empty methods but a) I'm not sure that makes sense and b) I still end up have to return something. Another option was maybe having multiple interfaces, one returning a single object and the other returning an array. Neither of those options felt right and looked a bit goofy when I prototyped them out. It seems to me that I wouldn't want the classes using the concrete DAO classes to be able to call the wrong method, but I don't know how to hide the unused method. Right now I just return an empty Object or Object[] for the unused method but that seems goofy too, and makes the class using ClassOne or ClassTwo know the innards of the implementations.
Anyway, any tips appreciated.
Thanks,
PabloIt sounds as if the methods weren't meant to be together, and so I'm wondering if you aren't better off with two distinct interfaces, one for each method. Otherwise one solution would be to implement a method that throws an exception if it is not meant to be called.
edit: D'oh! Just when you least expect them, Ninjas! They're everywhere!
Edited by: Encephalopathic on Apr 30, 2009 1:41 PM -
Regarding Interface methods implementation
Hi,
I have a interface with 3 methods.
public interface test{
public void A();
public void B();
public void C();
public class IntImpl implements test{
public void B(){
//implementation
Can we implement an interface with out implementing all the methods of interface .instead implementing a few methods in the interface?
i came to know that we can do the above one with out any exceptions? So any of you can resolve this issue ..
Thanks in advance
SriCan we implement an interface with out implementing
all the methods of interface .instead implementing a
few methods in the interface?In other words: can we implement an interface without implementing it?
The answer should be obvious.
i came to know that we can do the above one with out
any exceptions? Sure, it's quite easy, you already did it. The stuff above won't compile, hence it won't throw an exception when running.
Or you can also avoid compiler errors by declaring IntImpl as abstract. Which means you didn't implement the interface though. -
Interface method not implemented by class :(
I'm getting an error for interface method
Interface method handleLogout in namespace views.interfaces:IWiseMediator not implemented by class views.mediators:RatingViewMediator. RatingViewMediator.as
package views.interfaces
import flash.events.Event;
public interface IWiseMediator
function handleLogout(event:Event):void;
but I done this in RatingViewMediator
private function handleLogout(event:Event):void
viewObject.clearFields();interfaces are used to enforce a public 'interface' to the class. as a private method can only be accessed by the class itself, it doesn't make sense to include private methods in an interface.
-
Java.lang.AssertionError: Can not find generic method public abstract
I just downloaded the JDev 11g and trying to test the JEE web app. Below is my install:
JDeveloper 11g
JEE Web Project
EJB 3.0
Steps taken:
1. Created an entity bean from a table.
2. Created a session facade
3. Create sample Java client to use the facade (Right-click on the session facade, and choose New sample Java Client from the context menu)
4. Run the session facade
5. Run the Java client
I got the following error.
java.lang.AssertionError: Can not find generic method public abstract java.util.List<com.oracle.orm.model.ejb.persistence.Hosttags> queryHosttagsFindAll() in EJB Object
I checked the methods in the beans and interfaces, they are all there. Any idea?
Thanks.I have a customer that has this problem.
We wrote me that:
we got a java.lang.NoSuchMethodException while the method exist even in the generated (by WLS) stub.
when we change the interface by putting a "list" instead of list <> then it works.
hope it can help, but should be great know why.
Regards
Marco
글 수정: user10649412 -
Calling an interface METHOD of another abap web dynpro application
Hi Experts,
Can u plz tell how we can Call an interface METHOD of another abap web dynpro application in main WD Component.
Thanks
MaheshHi ,,
Example ALV interface method calling GET_MODEL interface method
View attribute declaration : M_WD_ALV type IWCI_SALV_WD_TABLE
DATA lo_INTERFACECONTROLLER TYPE REF TO IWCI_SALV_WD_TABLE .
wd_this->M_WD_ALV = wd_this->wd_cpifc_alv( ). "ALV is the usage name
DATA lv_value TYPE ref to cl_salv_wd_config_table.
lv_value = wd_this->M_WD_ALV->get_model( ). " interface method calling in ALV component usage.....
Regards,
Devi -
How can i pass the values to method public static void showBoard(boolean[][
I need x and y to pass to the method
public static void showBoard(boolean[][] board
i am very confused as to why its boolean,i know its an array but does that mean values ar true or false only?Thanks
import java.util.Random;
import java.util.Scanner;
public class Life1
public static void main(String[] args)
int x=0;
int y=0;
Scanner keyIn = new Scanner(System.in);
System.out.println("Enter the first dimension of the board : ");
x = keyIn.nextInt();
System.out.println("Enter the second dimension of the board : );
y = keyIn.nextInt();
boolean[][] board = new boolean[x][y];
fillBoard(board);
showBoard(board);
//Ask the user how many generations to show.
board = newBoard(board);
showBoard(board);
//This method randomly populates rows 5-9 of the board
//Rewrite this method to allow the user to populate the board by entering the
//coordinates of the live cells. If the user requests that cell 1, 1 be alive,
//your program should make cell 0,0 alive.
public static void fillBoard(boolean[][] board)
int row, col, isAlive;
Random picker = new Random();
for(row = 4; row < 9; row++)
for(col = 4; col < 9; col++)
if (picker.nextInt(2) == 0)
board[row][col] = false;
else
board[row][col] = true;
//This method displays the board
public static void showBoard(boolean[][] board)
int row, col;
System.out.println();
for(row=0; row < x; row++)
for(col=0; col<y; col++)
if (board[row][col])
System.out.print("X");
else
System.out.print(".");
System.out.println();
System.out.println();
//This method creates the next generation and returns the new population
public static boolean[][] newBoard(boolean[][] board)
int row;
int col;
int neighbors;
boolean[][] newBoard = new boolean[board.length][board[0].length];
makeDead(newBoard);
for(row = 1; row < board.length-1; row++)
for(col = 1; col < board[row].length-1; col++)
neighbors = countNeighbors(row, col, board);
//make this work with one less if
if (neighbors < 2)
newBoard[row][col]=false;
else if (neighbors > 3)
newBoard[row][col] = false;
else if (neighbors == 2)
newBoard[row][col]= board[row][col];
else
newBoard[row][col] = true;
return newBoard;
//This method counts the number of neighbors surrounding a cell.
//It is given the current cell coordinates and the board
public static int countNeighbors(int thisRow, int thisCol, boolean[][] board)
int count = 0;
int row, col;
for (row = thisRow - 1; row < thisRow + 2; row++)
for(col = thisCol - 1; col < thisCol + 2; col++)
if (board[row][col])
count++;
if (board[thisRow][thisCol])
count--;
return count;
//This method makes each cell in a board "dead."
public static void makeDead(boolean[][] board)
int row, col;
for(row = 0; row < board.length; row++)
for(col = 0; col < board[row].length; col++)
board[row][col] = false;
}this is what im workin with mabey you can point me in the right directionimport java.util.Random;
/* This class creates an application to simulate John Conway's Life game.
* Output is sent to the System.out object.
* The rules for the Life game are as follows...
* Your final version of the program should explain the game and its use
* to the user.
public class Life
public static void main(String[] args)
//Allow the user to specify the board size
boolean[][] board = new boolean[10][10];
fillBoard(board);
showBoard(board);
//Ask the user how many generations to show.
board = newBoard(board);
showBoard(board);
//This method randomly populates rows 5-9 of the board
//Rewrite this method to allow the user to populate the board by entering the
//coordinates of the live cells. If the user requests that cell 1, 1 be alive,
//your program should make cell 0,0 alive.
public static void fillBoard(boolean[][] board)
int row, col, isAlive;
Random picker = new Random();
for(row = 4; row < 9; row++)
for(col = 4; col < 9; col++)
if (picker.nextInt(2) == 0)
board[row][col] = false;
else
board[row][col] = true;
//This method displays the board
public static void showBoard(boolean[][] board)
int row, col;
System.out.println();
for(row=0; row < 10; row++)
for(col=0; col<10; col++)
if (board[row][col])
System.out.print("X");
else
System.out.print(".");
System.out.println();
System.out.println();
//This method creates the next generation and returns the new population
public static boolean[][] newBoard(boolean[][] board)
int row;
int col;
int neighbors;
boolean[][] newBoard = new boolean[board.length][board[0].length];
makeDead(newBoard);
for(row = 1; row < board.length-1; row++)
for(col = 1; col < board[row].length-1; col++)
neighbors = countNeighbors(row, col, board);
//make this work with one less if
if (neighbors < 2)
newBoard[row][col]=false;
else if (neighbors > 3)
newBoard[row][col] = false;
else if (neighbors == 2)
newBoard[row][col]= board[row][col];
else
newBoard[row][col] = true;
return newBoard;
//This method counts the number of neighbors surrounding a cell.
//It is given the current cell coordinates and the board
public static int countNeighbors(int thisRow, int thisCol, boolean[][] board)
int count = 0;
int row, col;
for (row = thisRow - 1; row < thisRow + 2; row++)
for(col = thisCol - 1; col < thisCol + 2; col++)
if (board[row][col])
count++;
if (board[thisRow][thisCol])
count--;
return count;
//This method makes each cell in a board "dead."
public static void makeDead(boolean[][] board)
int row, col;
for(row = 0; row < board.length; row++)
for(col = 0; col < board[row].length; col++)
board[row][col] = false;
} -
Hi experts,
I have the following scenario:
Component A opens, in a modal window, the component B.
When I do something in B, it's closed and I return to A.
There, I need read data from B (which is closed at the moment), to do something in A.
Anybody knows how can I do it?
I tried with external context mapping, but It doesn't work...
Could it be done with interface methods?
Thanks in advance!
LucasHi Padmam,
Thank you for your reply too, but I'm having problems with your solution.
I have created an interface Node in Component A, and I have created the mapping in the component usages of the component B.
When I run component A, it gives me an error. I supose that it is rise by this Interface Node.
This error is solved when I check out the property "Input Element (Ext)" of Interface Node, but without this property, I can't do the External Mapping...
Could you correct me if I'm wrong?
Thanks in advance!
Lucas -
Declaring constructor method public or private. What's the point?
My book declares the constructor method public. I tried declaring it private, and without any modifier out of curiousity. It runs exactly the same. What's the point of doing so?
public class test
public static void main( String[] args )
test fun = new test();
test fun2 = new test(2);
System.out.println( fun );
// System.out.println( test.toString() );
public String toString()
return "This is the toString";
test()
System.out.println( "This is the constructor method" );
test( int x )
System.out.println( "This is the constructor method: " +x );
}yougene wrote:
I'm new to OOP so maybe I'm not completely grasping the terminology. But this program works just fine with my class.
public class test2
public static void main( String[] args )
test foo = new test();
}It gives me the following output
----jGRASP exec: java test2
This is the constructor method
----jGRASP: operation complete.The constructor method is executing from an outside class. I tried this with and without the private modifier on the constructor. Same result.Try compiling this.
public class C1 {
private C1() {
System.out.println("C1 c'tor");
public class C2 {
public void foo() {
C1 c1 = new C1();
}
Maybe you are looking for
-
Trouble updating apps and wrong Apple ID's
Hello, Our kids got three iPods at Christmas, and I had one already. I set up my kids with 3 unique Apple IDs so they could have their own accounts for purchases, itunes cards, etc. I have a Macbook Pro with one user account and one iTunes library,
-
We made changes to the ILOM, but the system is not booting. In solaris 10 we user to booted with the cd and than we were able to fix it. Is it the same in Solaris 11-11-11 ? Thank-You :(
-
Hi Friends, What is a default layout variant? How to create one? Thanks in advance. Tamilarasan
-
Hi, I'm really enjoying my new Sony Smart Watch 2. It pairs nicely with the Sony Z Ultra phone I have, and it worked well until last night when I charged the watch using a computer USB port. The watch switched itself off and remained off for sev
-
# metastat -p d0 -m d1 d2 1 d1 1 1 c1t0d0s0 d2 1 1 c1t1d0s0 d10 -m d11 d12 1 d11 1 1 c1t0d0s1 d12 1 1 c1t1d0s1 d50 -m d51 d52 d53 1 d51 1 1 c1t0d0s4 d52 1 1 c1t1d0s4 d53 1 1 c1t2d0s4 # metainit -f d3 1 1 c1t2d0s0 ; metainit -f d13 1 1 c1t2d0s1 ; d3: