This question already has answers here:
Java: Static vs inner class [duplicate]
(8 answers)
Closed 9 years ago.
What is the purpose of defining a static class inside a normal class.
public class ClassA extends ClassB implements IA, IB {
public static classStatic extends ClassC implements I1, I2 {
}
}
What is the purpose of defining a static class inside a normal class.
Do all fields/variables need to be static, if its accessing inside the static class.
Can anyone explain/show article me what exactly is achieved in oops by implementing this in JavaScript way.
In Java you can only have one public/package only class per file; that class may require some computation/logic that is to complex and would fit better in another class but it is not general enough to be in another file, it is tight to the encompassing class.
If the inner class wouldn't be static, then you would need an instance of the encompassing class (in your case ClassA) to instantiate the inner class ClassC.
No they do not need to be static; it is a class like any other class from this point of view.
JavaScript is not a class based object-oriented language, so you cannot have classes of any kind.
Related
This question already has answers here:
Getting hold of the outer class object from the inner class object
(7 answers)
Closed 6 years ago.
I have basic classes
abstract class Unit {
Unit target;
abstract class UnitAI {/*...*/}
}
From these, I have derived
class Infantry extends Unit {
class InfantryAI extends UnitAI {/*...*/}
}
Can the class InfantryAI somehow get the secondary(implicit) this that is used for accessing the members of it's surrounding class Infantry?
Specifically, it needs to determine if its surrounding class Infantry is being targetted by its target, like this:
if (/*secondary_this.*/target.target == secondary_this)
or, generally, by another Unit.
You can access the outer this by prepending the classname:
Infantry.this.target; //"this" of the Infantry class from inside InfantryAI
Unit.this.target; //"this" of the Unit class from inside UnitAI
This doesn't work with static nested classes though as they don't belong to a instance of the outer class.
This question already has answers here:
Final interface in Java?
(10 answers)
Closed 6 years ago.
As since in a class I can do:
public final class Foo{}
wich means no more classes can extends that Foo class... e.g. String class is final, so no custom class can extends the class String.
How can I prevent to do the same with an interface?
If I do
public interface ISome{
void fly();
}
I would like to allow that
class A implements ISome {}
but block that
public interface IHouse extends ISome{
void fly();
}
doing this
public final interface ISome{}
makes no sense... and will bring a compile error like:
Illegal modifier for the interface
You can't.
Supposedly the Java designers didn't think there would ever be an appropriate use case for this: if you don't want an interface to be extended then really you ought to declare those functions directly in a concrete class.
That said, you can achieve this in C++ as in this language an interface is more of a convention - consisting of only pure virtual functions, and you can enforce non-extensibility with techniques such as friendship.
This question already has answers here:
Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more [duplicate]
(8 answers)
Closed 7 years ago.
In this tutorial (http://www.studytonight.com/java/object-and-classes) I read that a java class may optionally extend one parent class. By default, it will extend java.lang.Object.
Note: important statement that i was read that Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class.
according to note our normal java class should not extend other class like enum (enum types cannot extend another class).but we can able to inherit one class. is this multiple inheritance.?
in java class can derived by extends keyword. like this
class SomeClass
{ }
class MyClass extends SomeClass{}
How can all java classes by default extends java.lang.Object class without extends keyword in java?
When our class extends some base class, it becomes multiple inheritance. I searched in stackoverflow, but still I am not clear.
By default any class extends Object class. Doesn't it mean java supports multiple inheritance?
Can anybody clarify this with a simple example.
Every class, except for java.lang.Object, extends exactly one class.
If you write extends Something, then your class extends Something.
If you don't write extends Something, then your class extends java.lang.Object. (the same as if you wrote extends Object)
If you don't extend any class, you will still extend Object. If you explicitely extend some class, than you extend just this class, but the extended class will in other turn extend Object by default. This way Object is always in class hierarchy.
This question already has answers here:
Why can't static methods be abstract in Java?
(29 answers)
Closed 3 years ago.
I want to have the family of classes which all have the same method. As this method is quite simple I want to make it final static, something like
class A {
public static final String createSth () { }
}
(in fact this method returns only String).
Sometimes it would be useful to upper-cast all those A, B, C classes with the same method. Thus I would like to create mother class.
class abstract Mother {
public static abstract String createSth () { }
}
and then to add the appropriate extends for all my classes A, B, C ex. class A extends Mother.
Unfortunately it's not allowed by Java. I'm wondering why?
Sure I can remove static final modifier and then everything is ok. But on another hand if each subclass returns the constant String which is not modifiable in any way, why not?
You can ask what is the purpose of such a construction?
Simply I want to create database sql strings. For each table I created a separate class, and as a result of createSth method I want to return sql creating a table for this class.
Interfaces does help neither.
The only solutions seems to be to remove abstract modifier from createSth method in Mother class. But then I'm not allowed to uppercast to Mother class and to call createSth method for children. So I have to remove static modifier for children classes.
Concluding why abstract method is not allowed to be static? Ok here I can even understand that, but either why non static abstract method is not allowed to be replaced by static method in child class?
static methods (just like final and private) can't be overriden so there is no way to implement them in derived class so making them abstract would not make sense.
Static methods don't get inherited, so making them abstract would be meaningless.
final methods do not get overridden
static methods do not get overridden, but they can be hidden in the subClass.
This question already has answers here:
Why are you not able to declare a class as static in Java?
(14 answers)
Closed 9 years ago.
I am trying to create static class in Java Web Application using eclipse. but it says
Illegal modifier for the class ClassName; only public, abstract & final are permitted
can we create static class in web application ? if no why ?
No, because there are no static top-level classes in Java.
Static class you can create in general only as nested class in another class. This has nothing to do with Web-Application. It has a general validity in Java.
Each compilation unit should contain a public or default (non-static) class. Then within it you can declare static nested classes.
Only inner classes can be made as static.
That means except inner class you can not create static class
A static class is, in practice, nothing else than a standard non-inner class declared in another class.
In other words, your app web application has nothing to do with classes being declared as static or not.
Outer class cannot be static. Only nested class can be static. For example below is possible
class OuterClass {
static class StaticNestedClass {
...
}
}
But not
static class OuterClass {
...
}