How to extends Abstract Inner Class in java - 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();
}
}

Related

Inheritance in java(subclass of a subclass)

say we have three classes: class a , class b , class c;
class b inherits class a , if we define that class c inherits class b(which inherits class a) will the code give an error .If not the can we say that class c inherits class a;
in all i ask that can there be a subclass of a subclass??
In short, yes, you could definitely have a "chain" of inheritance. When you have a class A that inherits another class B, then it doesn't matter whether class B inherits from another class.
Though, you should keep in mind that a class is not able to inherit from multiple classes (it would throw a compiler error). Multiple inheritance in Java is achievable through the use of interfaces.
Yes, Multilevel inheritance refers to a mechanism where one can inherit from a derived class, thereby making this derived class the base class for the new class.
for example
Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}

Is Inner Class inherited to subClass of OuterClass?

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

Can an innerclass also be a subclass and also

Can a innerclass also be a subclass. Also one more thing in this set of java planguage it's not allowing me to create a instance of subclass even though I already created a instance of my encapsulating class for the innerclass.
public class Main {
Main OpTypes[] = new Main[3];
public static void main(String[] args) {
Main c = new Main();
c.OpTypes[0] = new Division(6,3);
Jool x = new Jool();
}
public class Jool {
public Jool() {
}
}
}
If you try it, you'll find that an inner class can extend a class.
Also, since the inner class is not static, it requires an instance of the outer class when constructing it. In the code below, you'll see two ways of doing that.
Method test shows the most common way, which is to do it from an instance (non-static) method of the outer class, in which case the outer class is implicit.
Method main shows how to do it outside of an instance method of the outer class, in which case you have to give an outer class instance before the new operator.
class MyBaseClass {
}
class Main {
public static void main(String[] args) {
Main c = new Main();
Jool x = c.new Jool(); // "c" explicitly used as outer class instance
}
public void test() {
Jool x = new Jool(); // "this" implicitly used as outer class instance
}
public class Jool extends MyBaseClass { // Inner class extends unrelated class
}
}

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 :).

how to call inner class's method from static main() method

Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.
public class Test2 {
interface A{
public void call();
}
class B implements A{
public void call(){
System.out.println("inside class B");
}
}
class C extends B implements A{
public void call(){
super.call();
}
}
public static void main(String[] args) {
A a = new C();
a.call();
}
}
Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method.
Right now I am getting problem in the line : A a = new C();
Here the inner class is not static, so you need to create an instance of outer class and then invoke new,
A a = new Test2().new C();
But in this case, you can make the inner class static,
static class C extends B implements A
then it's ok to use,
A a = new C()
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
So you need to use :
A a = new Test2().new C();
Refer the Java Tutorial.
You should do this
A a = new Test2().new C();

Categories