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();
}
Related
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();
}
}
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;
}
Consider following situation. I want to achieve the different behavior for methoddA() of class A depending upon from where it is getting call like here from class D or class C. How this can be achieved, method overriding is not working here.
class A
{
public methodA(){ //some code }
}
class B
{
A a = new A()
public methodB()
{
a.methodA();
}
}
class C
{
B b = new B();
public methodC()
{
b.methodB();
}
}
class D
{
B b = new B();
public methodD()
{
b.methodB();
}
}
What you need here is Polymorphism. First create an interface -
public interface MyInterface
{
void methodA();
}
then create two different implementations for two different behaviors -
public class First implements MyInterface
{
public void methodA() {
// first behavior
}
}
public class Second implements MyInterface
{
public void methodA() {
// second behavior
}
}
Now create your other classes as follows -
class B
{
public void methodB(MyInterface m)
{
m.methodA();
}
}
class C
{
B b = new B();
public void methodC()
{
// Pass the corresponding behavior implementation
// as argument here.
b.methodB(new First());
}
}
class D
{
B b = new B();
public void methodD()
{
// Pass the second behavior implementation.
b.methodB(new Second());
}
}
This will result in a more maintainable code.
You can pass the class name to your method as a String and in your method check
if(className.equals("A") // or use isInstanceOf() if you are passing objects of A/B
//do something
if(className.equals("B")
// do something else.
Why do you need two different implementations?
This easy trick can work for you... Please correct me if i am wrong..
I following code I have modified the method signature of Class A1 and Class B1 to accept Object and similarly while calling the methods from Class C and Class D whereever we are calling this method of class B1 pass this as reference. In Class A1 we can then check instanceof object and identify the calling class.
class A1
{
public void methodA(Object c){ //some code }
if (D.class.isInstance(c)){
System.out.println("Called from Class D");
}else if (C.class.isInstance(c)){
System.out.println("Called from Class c");
}else{
System.out.println("Called from Some diff class");
}
}
}
class B1
{
A1 a = new A1();
public void methodB(Object c)
{
a.methodA(c);
}
}
class C
{
B1 b = new B1();
public void methodC()
{
b.methodB(this);
}
}
class D
{
B1 b = new B1();
public void methodD()
{
b.methodB(this);
}
}
public class Testnew{
public static void main(String args[]){
D d = new D();
d.methodD();
C c = new C();
c.methodC();
B1 b = new B1();
b.methodB(b);
}
}
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();
}
}
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();
}