Suppose I have a class say A which is an ordinary class.
Now A nests a class B , which in turn nests C.
Class B is an abstract class while A,C are not so.
Now, No object can be created for B as it is an abstract class.
But Unless i create an object for class B , i wont be able to create an object for class C.
since ,
outerclass out=new outerclass();
outerclass.innerlcass in=outerclass.new innerclass();
Since B has no object , the outerclass object "out" is not available in this case.
So how do I create an object for C?
One way:
public class A {
abstract class B {
abstract String get();
class C {
void run() {
System.out.println(get());
}
}
}
public static void main(String[] args) {
new A().new B(){ String get() { return "hi"; } }.new C().run();
}
}
Or alternately, with the same A, B, and C as above:
class ConcreteB extends B {
String get() {
return "I'm not anonymous!";
}
}
public static void main(String[] args) {
new A().new ConcreteB().new C().run();
}
Related
I just have learned java. I'm fiding the different between this both ways of creating an object
public class A {
}
public class B extends A {
}
public static void main(String[] args){
A object = new B();
B object = new B();
}
Lets understand it with the example below.
In class A we added a getMethodofA(). So creating reference variable as A or B does not matter. As A is super class getMethodofA() will be available for both the objects of Type A or Type B
In class B we added a getMethodofB(). So creating reference variable as A or B matters. If you create object with reference variable as A, then only getMethodofA() will be available. While If you create object with reference variable B both the methods will be visible getMethodofA() and getMethodofB()
public class A {
public void getMethodofA(){
System.out.println("I am method A")
}
}
public class B extends A {
public void getMethodofB(){
System.out.println("I am method B")
}
}
public static void main(String[] args){
A objectA = new B();
objectA.getMethodofA();//No error
objectA.getMethodofB();//Compile time error
B objectB = new B();
objectB.getMethodofA();//No error
objectB.getMethodofB();//No error
}
I have a class A and within class A I have function func1 and func2.
In the same file I have class B and in that class B I have function func3.
In the main class, an object obj1 of class A is declared.
With this object, func1 of class A is called.
Within func1 of class A, an object obj2 of class B is created.
With this object, func3 of class B is called.
Now within func3 of class B, i want call func2 of class A with the object obj1. For this I want to refer to that object from within func3 of class B. Is it possible? If yes, how?
I tried using this.this.func2 which wouldn't work.
For now I am passing the object obj1 as an argument and it works fine. But I want to do the same without passing it because I want to use an array of objects and every time the object should differ
class A {
int attr1, attr2;
public void func1() {
int attr1 = 3;
int attr2 = 6;
B obj2 = new B();
obj2.func3();
}
public void func2() {
this.attr1 = 5;
this.attr2 = 10;
}
}
class B {
int atr1, atr2;
public void func3() {
atr1 = 4;
atr2 = 8;
// here I want to access the object obj1 to call the function func2()
}
}
public class Main {
public static void main(String args[]) {
A obj1 = new A();
A.func1();
}
}
Is it possible?
Yes.
If yes, how?
Option 1: Pass as parameter
Pass obj1 as parameter to func3.
Or more precisely, since func3 is called from a method of obj1, pass this as the parameter value:
class A {
public void func1()
{
B obj2 = new B();
obj2.func3(this);
}
}
class B {
public void func3(A a)
{
a.func2();
}
}
Option 2: Pass to constructor
Pass the A reference to the B constructor, and have B remember it in a field.
class A {
public void func1()
{
B obj2 = new B(this);
obj2.func3();
}
}
class B {
A a;
public B(A a) {
this.a = a;
}
public void func3()
{
this.a.func2();
}
}
Option 3: Inner class
Make class B an inner class of A. Essentially the same as option 2, but the compiler handles the reference to A for you.
class A {
public void func1()
{
B obj2 = new B();
obj2.func3();
}
class B {
public void func3()
{
A.this.func2();
}
}
}
I am thinking of an optimum design pattern which I can use to transfer objects to the methods in different classes other than passing them as arguments.
class A{
}
class B{
public A a;
public B()
{
a = new A();
}
}
class C
{
public void c()
{
//need to access "a" of class B other than passing "a" as argument;
}
}
Here, a in class A attribute needs to be accessed in many other class methods. Is there an optimum design pattern or any possible way other than passing this object (a) as arguments?
It's hard to say how your program is really structured but two options come to mind:
Pass an instance of B to C's constructor.
class A {};
class B {
public A a;
public B() {
a = new A();
}
};
class C {
public B b;
public C( B b ) {
this.b = b;
}
public void someMethod() {
System.out.println( b.a );
}
};
If only one instance of class A ever exists (ie a Singleton). That means that class B holds an instance of class A, not each instance of class B holds an instance of class A.
class A {};
class B {
public static final A a = new A();
};
class C {
public void someMethod() {
System.out.println( B.a );
}
};
class A
{
class B b;
B b = new b();
}
class B extends A
{
b.function();
}
Here can B use the same object created in A?
Following is your program:
class C {
public String cvariable;
public void cfunction(){
System.out.println("string");
}
}
class A {
public C c1;
public void funtiona(){
c1 = new C();
}
}
public class B extends A {
public void functionb(){
c1.cfunction();
}
public static void main(String args[]){
B b = new B();
b.functionb();
}
}
It is correctly throwing null pointer exception. It proceed as follows:
In the main method you call functionb()
In functionb() you call cfunction() with c1, but c1 is just an variable of type C(as not initialized yet) which contains null. So getting null pointer exception.
See the following program, It will throw java.lang.StackOverflowError
class B{
A a = new A();
public B(){
System.out.println(a.hashCode());
}
}
public class A extends B{
public void show(){
a.hashCode();
}
public static void main(String[] args){
new A().show();
}
}
This is because program goes in the infinite loop, As before creating a child class object it calls the parent class constructor and in parent class for hash code it again calls the child class constructor. so an infinite loop
So, one friend sent me this code and said that it had compiled successfully and returned 42.
But, the bothering thing is the method in parent class that "returns" 42 is private, and the method that is called on is in child class, and it's public. So, can anybody tell why and how this works?
static class A {
private int f() {
return 42;
}
}
static class B extends A {
public int f2() {
return super.f();
}
}
public static void main(String[] args) {
System.out.print(new B().f2());
}
It returns 42.
I tried to get rid of static, and
class A {
private int f() {
return 42;
}
}
class B extends A {
public int f2() {
return super.f();
}
}
public static void main(String[] args) {
Main m= new Main();
B b= m.new B();
System.out.print(b.f2());
}
it still returns 42.
Since both of the classes (A and B) are nested in Main, they can access the private int f() method.
If you extract the sources of A and B in top-level classes, this won't happen and you'll fail to compile.
The point of private is that "outside" classes should not be able to see private variables. But A and B are both part of the same class, or are nested within each other, so they can access each others private members.
So this will work:
public class A {
private void a() {
int bVal = this.new B().val; //! Accessing B private
}
class B {
A a = new A();
private int val = 10;
public void b() {
a.a(); // !! Accessing A private
}
}
BUT, this will fail, even if both A and B are in the same file but not within each other:
class A {
private void a() {}
}
class B extends A {
A a = new A();
public void b() {
a.a(); // can't see even if B extends A
}
}
This is because both classes A and B are nested inside another class, i.e both classes are inner classes of (or "part of") another same class. Since they (Data Members and Methods) are basically a member of the outer class,they are accessible within other inner classes even if private.
Java allows us Nesting of classes,If You Don't know about nested classes first read this :
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
class Outer{
class A {
private int f() {
return 42;
}//Method f() is a private member of A and accessible by Outer
}
class B extends A {
public int f2() {
return super.f();
}//As class B is inner class of Outer it can access members of outer,thus indirectly member of A
}
public static void main(String[] args) {
System.out.print(new B().f2());
}
}