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
}
Related
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();
}
}
}
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
I have a program with multiple classes, and when I try to make an instance of one of these objects in main, I get an error. How do I properly create a class in main with multiple classes?
public class A {
class B {
}
class C {
}
public static void main(String[] args) {
B b = new B();
C c = new C();
}
Error: No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
This is because B and C are inner classes. Unless you understand inner classes, this is probably not what you want.
Move them outside A:
public class A {
public static void main(String[] args) {
B b = new B();
C b = new C();
}
}
class B {
}
class C {
}
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();
}
In Java, what happens when you reference a private class in a Vector from outside the class?
Example:
public class A {
private class B {}
public Vector<B> vector = new Vector<B>();
public A() {
vector.add(new B());
}
}
public class C {
public C() {
A a = new A();
a.vector.get(0); // <- What does this return?
}
}
You can try this code:
public static void main(String[] args) {
A a = new A();
Object o = a.vector.get(0); // <- What does this return?
System.out.println(o.getClass());
}
The class is A$B, so it knows that B is an inner class of A.
But you cannot access any of the members of B. For example, if you change class A to this:
public class A {
private class B {
public int x;
}
public Vector<B> vector = new Vector<B>();
public A() {
vector.add(new B());
vector.get(0).x = 10;
}
}
You still won't be able to do this:
public static void main(String[] args) {
A a = new A();
System.out.println(a.vector.get(0).x); // this won't compile
}
It will say the type A.B is not visible.
It returns the reference to an Object of type A$B.
You will be able to assign it to an Object reference, e.g.
Object o = a.vector.get( 0 );
You can even use reflection to investigate properties of o.
Just a general reminder, please use java.util.ArrayList instead of Vector.
It will return an object of type A.B However you cannot do anything with it really because you will not be able to assign it to a variable or call any methods on it. If you do:
System.out.println(a.vector.get(0));
you will get something like:
A$B#42e816