It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
class demo {
interface test {
}
}
is it possible in Java innerclasses?
I'm not sure if I exactly understand your question, but I think the answer is: Yes, a class can declare an inner interface.
Yes, inner classes are possible in Java.
In your code it looks like you're talking about interfaces though. Having inner interfaces are possible, too:
public class Demo {
public static interface Inner {
}
}
You can declare inner classes, too:
public class Demo {
private class Inner {
}
}
Syntactically interface inside a class is correct .
But it depends upon your usage. Logically it will be correct depending upon your usage.
You can have any enum, class, interface or annotation inside one another.
public interface A {
public #interface B {
public class C {
public enum D {;
public interface E {
// keep on going.
}
}
}
}
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
What will happen if we have a private constructor in the class?
(in Java)
It means that (without reflection) constructor wont be accessible outside of your class so other classes wont be able to call it. Only members of your class will be bale to create its object.
class A{
private A(){} //private constructor
private static A a = new A(); //you can create A object as a field
void test(){
new A(); // you can create A object inside methods of your class
}
class Inner{ // inner classes are also members of outer class so you can use
// A constructor here
A a = new A(); //OK
void test(){
new A(); //OK
}
}
}
class B{
A a = new A();//error: we don't have access to A constructor
}
You can only use that constructor within the current class.
You will only be able to call it from other constructors in the class, or from static methods in the class.
This is normally a way of restrict creation of object from this class. This means you cannot create object from the class.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Suppose there is a class A. Which of the following two access modifiers is a default one for a constructor?
public A()
{
private A()
{
//some code....
}
protected A()
{
//some code....
}
}
It means the exact same thing as modifiers to functions and variables, only now it refers to who can CONSTRUCT an instance of the class.
public - any one can call the constructor from anywhere in the code.
private - Unable to construct from outside the class - typically used to enable control over who gets to instanciate the class with the use of a static member factory method. A good example of an appication found here
protected - Like private but now inheritance is involved - any subclass factory method can be used because now they can call this constructor.
As #dasblinkenlight mentions, if you do not specify any modifier, then they default to being package-private, meaning they are only visible to classes within the package.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Why must implement always be written after extend in a class declaration? For example:
public class Register extends ActionSupport implements ModelDriven
Why can it not be:
public class Register implements ModelDriven extends ActionSupport
The latter produces a compile-time error.
When the Java compiler turns a class into bytecode, it must first look to a parent class. That is because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields. Then it adds in pointers to the code of the child class functions - some of which are mandated by the 'implements' keyword.
Because the parent class must be compilable, it is easier if the compiler knows up front what that class is. Further, you can extend only one class but implement any number of interfaces. The compilation time climbs if the extends keyword can be intermingled amongst any number of implements instructions. Compilers want to fail as fast as possible to decrease dev time, so this choice is logical. Further, it helps you think clearly about the class for the same reason.
Probably to make the compiler's job easier. It's just a convention. There isn't any advantage to being able to rearrange these things.
It's like asking why Java functions aren't written in pre-order notation like public int (int a, int b)add{ return a+b; }.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Right now I am confused with inheritance and object of class. We can do the same using object as inheritance does.
ok here is first code
public class MainForm {
GUI g = new GUI();
g.show();
g.destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
now second one
public class MainForm extends GUI {
//GUI g = new GUI();
void show();
void destroy();
}
class GUI {
void show(){
......
}
void destroy (){
......
}
}
both code do the same.Right? then
Why should I extend a class when i can do the same using an object of class?
Inheritance is often used when you want to create multiple classes of a parent class, without duplicating a lot of logic. I may have a class named Animal which has methods speak(), walk(), and sleep(). I also may want to have specific instances of Animal like Cat and Dog.
Rather than implementing all three of those methods individually, I can write them in Animal and then have my other classes extend Animal to make use of those, as well as add any methods specific to those classes (like claw() or fetch()).
The benefits are reduced code duplication and better 'abstraction' of the objects. However, there are some drawbacks as well, as this article points out. It is important to know when inheritance is useful, and when it is not.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
public abstract class Two extends One
{
}
Class One is defined as
public abstract class One
{
}
Yes, and you cannot instantiate it either, of course.
Yes, you can.
If you extend a class with an abstract class and do not define or provide the implementation for the base class abstract methods then the child class extending it would automatically become abstract.