Is Inner Class inherited to subClass of OuterClass? - java

I tried below code and according to it I have the understanding that inner class is inherited to OuterClass's subclass.Is it correct?
class Outter{
int i=1;
class Inner{
int i=2;
public void hello(){
System.out.println("hello from outter.inner");
}
}
}
class ChildClass extends Outter{
class ChildInner{
}
}
public class Classes {
public static void main(String[] args) {
Inner inner = (new ChildClass()).new Inner();
System.out.println(inner.i);
inner.hello();
}
}
Output as excepted:
2
hello from outter.inner
Inner inner = (new ChildClass()).new Inner();
As this line of code worked it should mean that Inner class is inherited to ChildClass
I am getting confused because of the below statement I found on Link
When an outer class is extended by it’s sub class, Member inner classes will not be inherited to sub class. To use inner class properties inside the sub class of outer class, sub class must also have an inner class and that inner class must extend inner class of the outer class.

So I will illustrate that statement with an example:
When an outer class is extended by it’s sub class, Member inner
classes will not be inherited to sub class. To use inner class
properties inside the sub class of outer class, sub class must also
have an inner class and that inner class must extend inner class of
the outer class.
class Outter {
void method(){
Inner test=new Inner();
test.i=5; //No problem to do that even if i is private because it is inner class
}
class Inner {
private int i = 2;
}
}
class ChildClass extends Outter{
void method2(){
Inner test=new Inner();
test.i=5; //Does not compile
}
}
You cannot access "i" in the child class. And if you also extend the inner there you can:
class ChildClass extends Outter{
void method2(){
Inner2 test=new Inner2();
test.i=5; //Compiles fine because we have also extended the inner class (like written in the quoted text)
}
class Inner2 extends Inner{ }
}

The statement:
Inner inner = (new ChildClass()).new Inner();
Is not really inherited to ChildClass
If you break it you are basically doing this:
ChildClass child = new ChildClass();
Inner inner = child.new Inner();
Now you can call new Inner() on the ChildClass because it extends Outter
This doesn't mean that ChildClass can call any of the methods/properties inside Inner just because Inner is part of Outter

Related

How to extends Abstract Inner Class in java

I confused if
Abstract Class A{method();method2();}
And Other Class B Which Have Inner Class C
Class B{Abstract Class C{method(){//body}}}
And now Question is how to extends Class C b/C Abstract Class must be extends else
this is Unused class.
First, let's make it simpler - this has nothing to do with Android directly, and you don't need your A class at all. Here's what you want:
class Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
}
That doesn't compile, because when you create an instance of Child you need to provide an instance of Outer to the Inner constructor:
Test.java:6: error: an enclosing instance that contains Outer.Inner is required
class Child extends Outer.Inner {
^
1 error
There are two options that can fix this:
If you don't need to refer to an implicit instance of Outer from Inner, you could make Inner a static nested class instead:
static abstract class Inner {
}
You could change Child to accept a reference to an instance of Outer, and use that to call the Inner constructor, which uses slightly surprising syntax, but works:
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
Note that these are alternatives - you should pick which one you want based on whether or not the inner class really needs to be an inner class.
You simply extend it
class B{abstract class C{abstract void method();}}
class D extends B{
class E extends C{
void method(){
System.out.println("Hello World");
}
}
}
Or slightly more complicated without extending outer class
class B{abstract class C{abstract void method();}}
public class F extends B.C{
F(B b){
b.super();
}
void method(){
System.out.println("Hello World");
}
public static void main(String[] args){
B b = new B();
F f = new F(b);
f.method();
}
}

Instantiate subclass of abstract class

how do I instantiate the sub class of an abstract class? it gives error -- no enclosing instance of type abstractclass is accessible. no matter how I interchange the values. I know I cant use motorvehicle cuz abstract class cant be instantiated....
public class abstractclass {
public static void main(String args[]){
Car car1 = new Car();
}
abstract class MotorVehicle
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract void run();
}
class Car extends MotorVehicle
{
void run()
{
System.out.print("Wrroooooooom");
}
}
}
It won't let you instantiate them because you've declared them as inner classes. Precede the class declarations with static and you'll be able to do it:
class Outer {
class Inner {
}
static class Nested {
}
}
If a nested class is inner (non-static), it belongs to instances of the outer class, not the outer class itself. Inner classes need an instance of the outer class to be instantiated. Static nested classes do not.
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Outer.Nested nested = new Outer.Nested();
See Nested Classes tutorial. That is what the "no enclosing instance" message is about. You are right that an abstract class can't be instantiated directly, but Car isn't abstract.
Hi Here Car class is an inner class for abstractclass. so you can instantiate inner class like this only if its declared as static
Let's change your Program:
public class Abstract {
public static void main(String args[]){
Car car1 = new Car();
}
abstract class MotorVehicle
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract void run();
}
static class Car
{
void run()
{
System.out.print("Wrroooooooom");
}
}
}
Here Car class i declared as static and it can be instantiated inside "abstractclass".
For further reference you can look into Getting a "No enclosing instance of type..." error
Remove this:
public class abstractclass {
And also ending } from end of the file.
You have wrapped your whole program into one abstract class. Thats just wrong :).

Call inner class member from a different class

Please forgive me if this question is silly as i'm completely new to JAVA program. I'm looking into nested classes concept and come across the following program.
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("Display: outer_x = " + outer_x);
}
}
}
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
// Inner inner = new Outer().Inner();
// inner.display();
}
}
And my doubt is how to access members of Inner class from NestedClass. In "Java - The complete reference", it is given that "You can, however, create an instance of Inner outside of
Outer by qualifying its name with Outer, as in Outer.Inner". But if i try to use it as,
Inner inner = new Outer().Inner();
inner.display();
it is throwing error. So please help me experts.
You need to create a new Inner instance by using the new keyword.
Inner inner = new Outer().new Inner(); // "new" keyword is required to create a new Inner instance.
If you do not have the import for import com.java.test.Outer.Inner; added, add it. Or else, you can do something like this
Outer.Inner inner = new Outer().new Inner();
Outer st = new Outer();
Outer.Inner fl = st.new Inner();
Note that the code above would be exactly the same whether the main() method is inside the outer class (because main is a static method) or even in some other class. Some other class could only run the code if it has access to outer class . But, Outer Class will have default, or package access since access modifiers are not specified when Outer Class is declared. This essentially means that any class within the same package as OuterClass will be able to run the code above without any issues.
You first have to create an instance of the outer class. After that, you can create an instance of that nested inner class by the outer class' instance.
Outer outer = new Outer();
Outer.Inner a = outer. new Inner();
a.display();
Inner class can be accessed only through live instance of outer class.
Try this:
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}
Use this:
Outer.Inner inner=new Outer().new Inner();
as you need to create object of Inner class too as it is not static inner class.
So your code becomes:
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}

java inner classes method access

I have an outer class. I also have a private inner class that extends JPanel. This is the design of the code.
public class Outer{
private class Inner extends JPanel{
public void doSomeWork(){}
}
public Outer(){
Inner inner = new Inner();
inner.doSomeWork();
}
public static void main(String args[]){
Outer outer = new Outer();
}
}
I am not being able to access the doSomeWork() method of the inner class from the outer class. Please help.
Here is how you make an object of inner and access its variables...
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.doSomeWork();
Example code from Oracle is here...

Java how to access a class within a class

Here is basically what I have:
public class Game extends Activity{
public class Work{
public class Shuffle{
*Class I need to access*
}
}
}
Here is the class I will be accessing Shuffle from:
public class Deck extends Game {
public int shuffle() {
//WHAT DO I NEED TO DECLARE HERE IN ORDER TO ACCESS Shuffle.getShuffle()?
int[] shuffDeck = (MY SHUFFLE CLASS).getShuffle();
int x = shuffDeck[i];
String y = String.valueOf(x);
i += 1;
return x;
}
}
What do I need to declare to be able to access Shuffle.getShuffle() in my Deck class?
Now taking of Nested Classes its of 2 types :
1. Inner Classes (Non-static)
2. Top Level Classes (static)
- Inner Class (Non-static) has an Implicit Reference to its Outer Class (Enclosing Class).
To access an Inner Class from outside you need to access it using the Outer Class Object.
Eg:
public class Outer{
class Inner{
}
}
public class Test{
public static void main(String[] args){
Outer o = new Outer();
Outer.Inner i = o.new Inner();
}
}
- A Top-Level Class (static) is just like a separate class in the Outer Class.
Top-level class needs to create an object of the Outer Class to access its Non-static members, but It can Access the Static methods and Static variables of the Outer class directly.
Eg:
public class Outer{
class Inner{
}
}
public class Test{
public static void main(String[] args){
Outer.Inner i = new Outer.Inner();
}
}
You cant access Inner class methods, and fields, from outer classes, directly. However you can access outer class methods, inside inner classes.
To access inner classes, you need to create an object of inner class, than only you can access inner class fields. See nested classes for more clarifications:
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
You are talking about nested classes, try to read also this article. Maybe it can help.
http://en.wikibooks.org/wiki/Java_Programming/Nested_Classes
A nested class should exist only to server outer class. You should not expose inner classes to outside world.
public class Game extends Activity{
public static class Shuffle
{
// provide shuffle method here which will use internal implementation of
// shuffle.
}
int[] shuffle()
{
// call inner class method from here. Also declare inner class as
// static since I guess your inner class does not require instance
// of outer class.
return null;
}
And access shuffle() method of Game using Object of Game
int[] shuffDeck = (Game object).getShuffle();
public class Outer{
class Inner{
}
}
Outer.Inner art = (new Outer()).new Inner();
import android.content.Context;
import android.widget.Toast;
public class Outer{
private Context context;
public Outer(Context con) {
context = con;
String text = "Hello, I'm dbManager.";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration ).show();
}
public class Inner{
public Inner() {
String text = "Hello, I'm «Art dbManager».";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration ).show();
}
}
}
Outer.Inner art = (new Outer(this)).new Inner();

Categories