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 :).
Related
I wrote the below code to test the concept of classes and objects in Java.
public class ShowBike {
private class Bicycle {
public int gear = 0;
public Bicycle(int v) {
gear = v;
}
}
public static void main() {
Bicycle bike = new Bicycle(5);
System.out.println(bike.gear);
}
}
Why does this give me the below error in the compiling process?
ShowBike.java:12: non-static variable this cannot be referenced from a static context
Bicycle bike = new Bicycle(5);
^
Make ShowBike.Bicycle static.
public class ShowBike {
private static class Bicycle {
public int gear = 0;
public Bicycle(int v) {
gear = v;
}
}
public static void main() {
Bicycle bike = new Bicycle(5);
System.out.println(bike.gear);
}
}
In Java there are two types of nested classes: "Static nested class" and "Inner class". Without the static keyword it is an inner class and you will need an instance of ShowBike to access ShowBike.Bicycle:
ShowBike showBike = new ShowBike();
Bicycle bike = showBike.new Bicycle(5);
Static nested classes and normal (non-nested) classes are almost the same in functionality, it's just different ways to group things. However, when using static nested classes, you cannot put definitions of them in separated files, which will lead to a single file containing a lot of class definitions.
Bicycle is an inner class, so you need outer class instance to create inner class instance like :
ShowBike sBike = new ShowBike();
Bicycle bike = sBike.new Bicycle(5);
Or you can simply declare Bicycle class as static to make your current way work.
The main method cannot access a non-static member of its class.
By the way, classes should be named after nouns, not verbs. So a better way to do it is :
private class Bicycle {
public int gear = 0;
public Bicycle(int v) {
gear = v;
}
public void showGear() {
System.out.println(gear);
}
public static void main(String[] args) {
Bicycle bike = new Bicycle(6);
bike.showGear(); // Notice that the method is named after a verb
}
}
You need to create outer class object instate of inner class. or you need to make inner class as static. so for inner class no object required.
Your Bicycle class is not static, and therefore cannot be used in a non-static method. If you want to use it in the main method, change it to
private static class Bicycle
class A { //1st code starts here
private void display() {
System.out.println("A class");
}
}
class B extends A {
protected void display() {
System.out.println("B class");
}
}
class Test {
public static void main(String args[]) {
A obj = new B();
obj.display();
}
}
Output : Test.java:22: error: display() has private access in A
obj.display();
class Outer{ //2nd Code starts here
class Inner1{
private void m2() {
System.out.println("Inner1 class");
}
}
class Inner2 extends Inner1{
protected void m2() {
System.out.println("Inner2 class");
}
}
public static void main(String args[]) {
Outer o=new Outer();
Outer.Inner1 i=o.new Inner2();
i.m2();
}
}
Output : Inner1 class
Why compile time error in 1st code while output Inner1 class in 2nd code???
The code of the Outer class can access any member or method declared within the Outer class, regardless of the access level. However, the m2 method being called is the method of the base class Inner1, since you can't override a private method.
On the other hand, the code of the Test class cannot access a private method of a different class, which is why that code doesn't pass compilation.
Because private members are accessible only in class.
when class B extends A private members are inaccessible For B.
In case of Inner classes, An inner class is a member of class and have access to all members of enclosing class.
When we define inner classes, I understand that static inner classes are accessed with the outer class and inner member class exist with the instance of the outer class.
The confusion is, if I want to hold an instance of an non-private inner member class, then the variable declaration goes like:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
How outer class is able to reference the inner member class like this? What is happening here?
Sample Code:
public class NestedClasses {
public static void main(String[] args) {
A.B b = new A().new B(); // <- How A is directly accessing B, B is not defined as static.
A.StaticClass staticClass = new A.StaticClass();
}
}
class A {
static int x;
int y;
A() {
System.out.println(this.getClass().getName());
}
static class StaticClass {
StaticClass() {
System.out.println(this.getClass().getName());
}
}
class B {
B() {
System.out.println(this.getClass().getName());
}
}
}
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();
}
}
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();