Java hierarchy and access level - java

In my java application I want to solve the following scenario
class C {
}
Class A extends C {
public A() {
B objB=new B(this);
objB.methodA();
}
public void methodX() {
}
}
Class B {
private A objA;
public B(A a) {
objA=a;
}
public void methodA() {
objA.methodX()
}
}
Is it possible to call methodX() of A from Class B. When I compile the code the objA(in class B) gives access only to Class C methods? Is alternate way exists for solving the above scenario?? Plz help

Apart from the fact that it makes no sense what you are trying to do and code is weired there is nothing wrong with it Semantically . I just tried it like below.
package com.test;
public class So {
/**
* #param args
*/
public static void main(String[] args) {
A a = new A();
B b = new B(a);
b.methodA();
}
}
class A extends C {
public A() {
B objB = new B(this);
objB.methodA();
}
public void methodX() {
}
}
class B {
private A objA;
public B(A a) {
objA = a;
}
public void methodA() {
objA.methodX();
}
}
class C {
}
here is an other variant for main(String[] args) method:
public static void main(String[] args) {
C ca = new A();
B b2 = new B((A)ca); // This is typcasting
b2.methodA();
}

Related

How Dynamic Cast Apply in HAS-A Relationship

I want to call both class A Method and Class B method each after. these two class relationship defined as "HAS-A" Relationship....
class A{
public void getData(){
System.out.println("Class A");
}
}
class B{
public void getData(){
System.out.println("class B");
}
}
public class Main {
public static void main(String[] args) {
A a=new A();
B b=new B();
new Main().call(a); //call A Class Method
new Main().call(b); //call B class Method
}
public void call((Class Name??) a){
a.getData();
}
}
You can make A and B extend Upper, with Upper either being an upper class or an interface. In both cases it should have the method getData(), so your call()-method can access it.
I got Solution Thanks for helping me.....
class A{
public void getData(){
System.out.println("class A");
}
}
class B {
public void getData(){
System.out.println("class B");
}
}
public class Main{
public static void main(String[] args) {
A a=new A();
B b=new B();
new Main().call(a);
new Main().call(b);
}
public void call(Object obj)
{
if(obj instanceof A)
((A) obj).getData();
if(obj instanceof B)
((B) obj).getData();
}
}

Java abstraction and interface

I am looking for a solution to a problem in Java. Hope team can help me.
Consider following classes:
Class A{
//constructor
Object obj;
public A(Object obj){
this.obj = obj;
}
public void ma1(){
system.out.println("In ma1");
obj.mc1();
}
public void ma2(){
system.out.println("In ma2");
//A should not be able to access mc2
obj.mc2();
}
}
Class B{
//constructor
Object obj
public B(Object obj){
this.obj = obj;
}
public void mb1(){
system.out.println("In mb1");
// B should not be able to access mc1
obj.mc1();
}
public void mb2(){
system.out.println("In mb2");
obj.mc2();
}
}
Class C{
public void mc1(){
system.out.println("In mc1");
}
public void mc2(){
system.out.println("In mc2");
}
}
public static void main(String[] args){
A a = new A(new C);
B b = new B(new C);
a.ma1();
a.ma2();
b.mb1();
b.mb2();
}
Now I want object 'a' should be able to access mc1 and not mc2
I want object 'b' should be able to access mc2 and not mc1
Can anyone explain me how to achieve above functionality ?
Note: Class A, B are my utility classes and its method are used at multiple places in my project. I want to restrict my developer from accessing particular method .
Regards,
Sand
Try this code, if it match with your requirement. Create two interfaces -
public interface A1 {
public void mc1();
}
public interface B1 {
public void mc2();
}
public class A {
//constructor
A1 obj;
public A(A1 obj){
this.obj = obj;
}
public void ma1(){
System.out.println("In ma1");
obj.mc1();
}
public void ma2(){
System.out.println("In ma2");
//A should not be able to access mc2
obj.mc2();
}
}
public class B {
//constructor
B1 obj;
public B(B1 obj){
this.obj = obj;
}
public void mb1(){
System.out.println("In mb1");
// B should not be able to access mc1
obj.mc1();
}
public void mb2(){
System.out.println("In mb2");
obj.mc2();
}
}
implement your C class with A1 and B1 interfaces -
public class C implements A1,B1{
public void mc1(){
System.out.println("In mc1");
}
public void mc2(){
System.out.println("In mc2");
}
}
here is your main method -
public static void main(String[] args){
A1 a1 = new C();
A a = new A(a1);
B1 b1 = new C();
B b = new B(b1);
a.ma1();
a.ma2();
b.mb1();
b.mb2();
}

Java: Get access from nested class to main class?

Code:
public class A{
B b = new B();
public class B{
public void fun(){ send(A); }
}
I want to do something with all A object in B.
I can create method in A class:
private A getThis(){return this;}
But is it other solution (some keyword)?
Try this code inside your inner class.
A.this
It should give you a reference to the enclosing instance from the outer class.
Here is a small example.
public class A {
private B b = new B();
public class B {
public void fun() {
}
public A getEnclosing(){
return A.this;
}
}
public static void main(String[] args){
A a = new A();
System.out.println(a == a.b.getEnclosing());
}
}
Try
B b = new B(this);
Then B contructor
public B(A a) {
this.a = a;
}

how can this downcast works:(B)super.clone()?

class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
B n = (B)m;
}
}
this code can not be complied. However, in the code below, this downcast works.
class A {
}
public class B extends A implements Cloneable{
#Override
public B clone() throws CloneNotSupportedException {
return (B)super.clone();
}
public static void main(String[] args) {
B m = new B();
B n = m.clone();
}
}
so, why this downcast works?
=============Correction============================
sorry for my fault, it should be B n = **(B)**m;, not B n = m;.
I'm very sorry. I have corrected it in the above code.
Even in first case -;
class A {
}
public class B extends A {
public static void main(String[] args) {
A m = new A();
// B n = m;
B n = (B)m;
}
}
It's work.
WHAT?
You cannot cast A to B no mather what you people are saying
IF A extends B than B can be threated as insance of A and B but A cannot be instance of B.

Force the execution of a method in a class

I'm using Java and I want to call the method f2 in class A from the class B. Is it possible to do this?
public class A{
private B b = new B();
public void f1(){
b.f3();
}
public void f2(){
// do something;
}
}
public class B{
public void f3(){
// Call f2 of class A from here.
}
}
You need an instance of A in class B and invoke f2 on that instance. For example, you could instantiate one inside the body of f3:
public class B {
public void f3() {
A a = new A();
a.f2();
}
}
Another way would be for f3 to receive an instance of A:
public class B {
public void f3(A a) {
a.f2();
}
}
And yet another way, you could have B instantiate one:
public class B {
private final A a;
public B() { this.a = new A(); }
public void f3() {
this.a.f2();
}
}
And lastly, B could receive one in it's constructor:
public class B {
private final A a;
public B(A a) { this.a = a; }
public void f3() {
this.a.f2();
}
}
The point being that if you want to invoke an instance method on a class you must have an instance of that class in your hand.
Finally, I notice that you have A.f1 invoking B.f3 and from there you want to invoke A.f2. So, it looks like your best option here is the second option above. That is:
public class A {
private final B = new B();
public void f1() { this.b.f3(this); }
public void f2() { /* do something */ }
}
public class B {
public void f3(A a) { a.f2(); }
}
The key here is that we are passing an instance of A to B.f3. The way that we achieve that is by passing the this reference, which is a reference to the currently executing instance. In A.f1, that would be the instance of A that is currently executing.
You need an instance of class A to do this.
public class A{
private B b = new B();
public void f1(){
b.f3(this);
}
public void f2(){
// do smthing;
}
}
public class B{
public void f3(A a){
a.f2(); // Call f2 of class A from here.
}
}
This type of code structure is usually more confusing than useful. I suggest instead doing this.
public class A{
private B b = new B();
public void f1(){
WhatAf2Needs w = b.f3();
f2(w);
}
public void f2(WhatAf2Needs w){
// do smthing;
}
}
public class B{
public WhatAf2Needs f3(A a){
return WhatAf2Needs;
}
}
If you want to call a method of the thing that called you, you have to have the caller pass itself in using the this keyword. In code, it would be:
public class A{
private B b = new B();
public void f1(){
b.f3(this);
}
public void f2(){
// do smthing;
}
}
public class B{
public void f3(A caller){
caller.f2();
}
}
You would have to instantiate class A in class B, given the way it's currently written, to make any method calls on it.
You can also declare f2 static and call it like A.f2(). This type of things depend a lot on the design of your classes though. The other answers here are very valid too.
public class A{
private B b = new B();
public void f1(){
b.f3();
}
public static void f2(){
// do smthing;
}
}
public class B{
public void f3(){
A.f2();
}
}

Categories