String concatenation in inherited class - java

I have a class called A and there's a String declared in it. And i have 2 other classes B and C which is inherited from A
public abstract class A {
protected String ss="";
public abstract String someMethod();
}
public class B extends A{
public String someMethod(){
int i=8;
return ss+="$"+i;
}
}
public class C extends A {
public String someMethod() {
int i=9;
return ss+="$"+i;
}
}
Test Code:
A aa = new B();
aa.someMethod();
A aaa = new C();
aaa.someMethod();
When I print aaa.someMethod(); - why haven't the strings from class B and C been appended? I want them to be appended. How can I do this ?

/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/javac A.java B.java C.java Test.java
/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test
$8
$9
nothing surprising here, B method someMethod() calls B method, C method someMethod() calls C method...
file A.java:
public abstract class A
{
protected String ss="";
public abstract String someMethod();
}
file B.java
public class B extends A
{
public String someMethod()
{
int i=8;
return ss+="$"+i;
}
}
file C.java
public class C extends A
{
public String someMethod()
{
int i=9;
return ss+="$"+i;
}
}
file Test.java
public class Test
{
public static void main(String pArgs[])
{
A aa = new B();
System.out.println(aa.someMethod());
A aaa = new C();
System.out.println(aaa.someMethod());
}
}

Overridden methods in Java do not automatically invoke their superclass parents. So, in your C subclass, calling someMethod does not invoke the method from its parent A, unless you explicitly call super.
public class C extends A
public String someMethod(){
int i=9;
return ss+= super.someMethod()+"$"+i;
}
}
I assume you are doing this to learn, because otherwise this is a pretty terrible way to manage your inherited classes and their properties.

Related

Modifiy method called from extended class

I have three classes, and I need to modify first class through the second that is extended :
my first class A :
public class A{
private String name;
public void setName(String name) {
this.name= name;
}
my second class B
public abstract class B {
public void init() {
A a = new A();
a.setHost("foo");
}
}
my third class C
public class C extends B {
// I want to use the method setName() of the a declared in class B
b.init.a.setName("bar");//compile error, I tried several syntax I don't know how to do it
}
expected output, in my third class :
a.Getname = "bar"
Your code has multiple issues:
1) Variable b is never declared.
2) Variable a is private to method init, so you can't access it outside the init method.
So the solution should be like:
Class B:
public abstract class B {
protected static A a = new A(); // Protected to make it visible to child class
public void init() {
a.setHost("foo");
}
}
Class C:
public class C extends B {
public static void main(String[] args) {
a.setName("bar");
System.out.println(a.getName()); //Output = bar
}
}
you can return a in the init method of B like below.
public A init() {
A a = new A();
a.setHost("foo");
return a;
}
Then you can set the value in C like below
public class C extends B {
public setNameinA() {
B b = new B();
b.init().setName("bar");
}
}

Call base method on generic object of derived class

Trying to add a base interface with method so all derived classes have to implement the method or use default method. What's the best way to going about getting this method callable? See comment in code block below.
public interface IA{}
public interface IB{
public Integer doWork();
}
public interface IC extends IB{
}
class B implements IB{
Integer doWork(){
return 2;
}
}
class C extends B implements IC{
#Override
Integer doWork(){
return 7;
}
}
//What do I need to do to cast clazz to an object so I can call the derived class' doWork method?
private Integer newClient(Class<T> clazz){
((B) clazz).doWork();
}
Ended up finding a solution:
B.class.cast(clazz);
As for how to ensure you call the derived class' method that overrides the base, that is a native behavior of Java.
Example Program:
public class Foo {
static class A {
int get() { return 0; }
}
static class B extends A {
#Override
int get() { return 1; }
}
public static void main(final String[] args)
{
A a = new A();
B b1 = new B();
A b2 = new B();
printA(a);
printA(b1);
printA(b2);
}
public static <T extends A> void printA(T bObj) {
System.out.println(bObj.get());
}
}
Output:
0
1
1
Note that the output returned from b2::get()::int is the same as b1::get()::int, even though b2 is type A and b1 is type B. This is because even though we only have a reference to the A class in b2, the object implementation is still B.
It seems that you only want to know how to instantiate the Class. Assuming it has a default constructor you can do it this way:
private Integer newClient(Class<B> clazz){
try {
((B) (clazz.getConstructor().newInstance())).doWork();
} catch ...
}

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;
}

Replace empty method with method from a class where it is used

In the following scenario, I want to replace Class B's (similarly D, E, F, etc.) method doSomething() with the method in Class A where it will be used. How would I go about this? Made up some example, hope it gets the message across
public class B implements GetNames{
public void getNameA(){ return "NameA"; }
public void getNameB() { return "NameB"; }
public void doStuff(){
//print names
doSomething(getNameA(), getNameB());
//print names
}
public void doSomething(String a, String b){}
}
public class A{
public void someMethod(){
B b = new B();
b.doStuff(); //*So I want it to call the method in B but somehow replace the doSomething method in B with the doSomething method in A
}
public void doSomething(String a, String b){
//print 'blabla' + a
//print 'blablabla' + b
//concatenate and print
}
}
Made abstract class A implement interface GetNames and then extend it in class B:
public abstract class A implements GetNames {
public void doSomething(String a, String b){
//print 'blabla' + a
//print 'blablabla' + b
//concatenate and print
}
}
public class B extends A {
public void getNameA(){ return "NameA"; }
public void getNameB() { return "NameB"; }
public void doStuff(){
// class A's doSomething will be called
doSomething(getNameA(), getNameB());
//print names
}
}
Class A should extend class B. If you make B an abstract class, the B.java file would look something like this:
public abstract class B {
...
public abstract void doSomething(String a, String b);
...
}
An abstract class has some functionality, like the getNameA method, which is already defined, but other methods like doSomething are left to its subclasses to implement.
Change class A to read:
public class A extends B {
...
#Override
public void doSomething(String a, String b) {
// custom behaviour
}
}
If what you want is to just make an instance of class B that has a different implementation of the method doSomething then what you could do is this:
B myBInstance = new B() {
#Override
public void doSomething(String a, String b) {
// custom behaviour here
}
};
myBInstance.doStuff();
Style-wise and design-wise though, this is only a quick-and-dirty way to define behaviour for a one-time use 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