• 0

Java class extend question


Question

I was wondering if class A extends B, would I be able to access the method of class A through B ??? i.e b.methodofA ??? or do i have to make B an abstract class.

Link to comment
Share on other sites

20 answers to this question

Recommended Posts

  • 0

im not sure i understand the question.

Are you asking that if B is the parent calss and A is an heir can A access methods in B?

Link to comment
Share on other sites

  • 0
I was wondering if class A extends B, would I be able to access the method of class A through B ??? i.e b.methodofA ??? or do i have to make B an abstract class.

No. B has no idea of who extends it. A, however, can access B's public and protected members. Making B abstract won't make any difference. All that is true if you're actually saying:

public class B
{
}

public class A extends B
{
}

Link to comment
Share on other sites

  • 0

If you run an overridden method then it is possible for B to run a method of A.

public class A extends B {
  public void runme() {
    juicy();
  }

  public void fruit() {
    System.out.println("A's method called.");
  }
}

public class B {
  public void juicy() {
    System.out.println("Calling A's method from B.");
    fruit();
  }

  public void fruit() {
  }
}

Link to comment
Share on other sites

  • 0
If you run an overridden method then it is possible for B to run a method of A.

public class A extends B {
  public void runme() {
    juicy();
  }

  public void fruit() {
    System.out.println("A's method called.");
  }
}

public class B {
  public void juicy() {
    System.out.println("Calling A's method from B.");
    fruit();
  }

  public void fruit() {
  }
}

I don't think that's right... Subclasses automatically inherit all superclass methods, no matter what. You can override, but it's only if you want to change the superclass's version of the method.

Link to comment
Share on other sites

  • 0
I don't think that's right... Subclasses automatically inherit all superclass methods, no matter what. You can override, but it's only if you want to change the superclass's version of the method.

That is correct. In em_te's example, class B would be calling class B's fruit() function -- not class A's.

Link to comment
Share on other sites

  • 0
That is correct. In em_te's example, class B would be calling class B's fruit() function -- not class A's.

exactly.... that makes what em_te's comment incorrect. not correct.

Link to comment
Share on other sites

  • 0

class A extends B {
 public void runme() {
   juicy();
 }

 public void fruit() {
   System.out.println("A's method called.");
 }
}

class B {
 public void juicy() {
   System.out.println("Calling A's method from B.");
   fruit();
 }

 public void fruit() {
  System.out.println("Wrong!  Called B's method");
 }
}

public class Test5 {
	public static void main(String[] args) {
  B b = new B();
  b.juicy();
	}
}

Output:

Calling A's method from B.

Wrong! Called B's method

Just as expected.

Link to comment
Share on other sites

  • 0
public class Test5 {
	public static void main(String[] args) {
  B b = new B();
  b.juicy();
	}
}

Output:

Calling A's method from B.

Wrong! Called B's method

Just as expected.

Obviously you'd get that if you ran it incorrectly, instead of calling the method "runme()". Why do you think I put that method there? The principles of inheritance allow a method of a superclass to be overridden by a method of a subclass without the superclass knowing.

Link to comment
Share on other sites

  • 0

Oh, and if you're talking about B calling a method from A in the following manner:

A a = new A();

a.juicy();

the function juicy is not calling a function from class A from class B. The reason is the function juicy is part of class A. You are actually calling a function of A from A. Since juicy is an overridden function, is is a member of class A. It is the equivilent of a C++ virtual function.

Link to comment
Share on other sites

  • 0
Obviously you'd get that if you ran it incorrectly, instead of calling the method "runme()". Why do you think I put that method there? The principles of inheritance allow a method of a superclass to be overridden by a method of a subclass without the superclass knowing.

I think this has become a huge misunderstanding. Now what you're saying is correct, but I think you didn't explain it as you wanted to earlier.

It seemed that earlier you meant that you needed to reference the juicy() method of class A inside of class B in order to be able to use class A's juicy() method from class B. That is incorrect, and is what I was replying to.

Link to comment
Share on other sites

  • 0

Summary:

Super Class S

- public method m1

- public method m2

Class C Extends S

- public method m2

- public method m3

Result:

C.m2 calls the method in the Class C version of m2 ONLY (unless you call super())

C.m1 calls what is in Class S

C.m3 calls what is in Class C (as it does not exist in Class S)

(Got a 5 on the Advanced Placement Comp Sci AB Test)

Link to comment
Share on other sites

  • 0
the function juicy is not calling a function from class A from class B. The reason is the function juicy is part of class A. You are actually calling a function of A from A. Since juicy is an overridden function, is is a member of class A. It is the equivilent of a C++ virtual function.

(1) The method juicy() is not part of class A. You can prove it by compiling both class A and B. Then edit class B to remove the method juicy() and compile class B only. Then try to run A and it won't work. (2) juicy() is not overridden because it does not exist in class A. (3) All methods in Java are similar to C++ virtual functions in that they are all late binding, with the exception of static and final methods.

It seemed that earlier you meant that you needed to reference the juicy() method of class A inside of class B in order to be able to use class A's juicy() method from class B. That is incorrect, and is what I was replying to.

I don't quite follow what you're saying. (1) The juicy() method does not belong to A because A does not override it. (2) class B doesn't reference class A anywhere and certainly doesn't reference juicy() anywhere. The most you can say is that class B references fruit() from class A.

exactly, that is what I thought too.  I thought he meant you could call a method of class A from an instance of class B.

The poster didn't say from which instance it had to be from. He even suggested abstract classes, which further shows that there was no such restriction because if B was abstract, B cannot be instantiated. Therefore I only wanted to point out one case in which it was possible which is with overridden methods.

Link to comment
Share on other sites

  • 0
Summary:

I'd like to hear from Original_ who has the credentials to shed light on this situation. Your summary seems contingent with my example. How are my comments incorrect?

Link to comment
Share on other sites

  • 0
Summary:

Super Class S

- public method m1

- public method m2

Class C Extends S

- public method m2

- public method m3

Result:

C.m2 calls the method in the Class C version of m2 ONLY (unless you call super())

C.m1 calls what is in Class S

C.m3 calls what is in Class C (as it does not exist in Class S)

(Got a 5 on the Advanced Placement Comp Sci AB Test)

If you had the same method in C as the super class does, doesn't that override it? and if the method header was the same and the parameters were different wouldn't it just mean the method m2 in class C becomes an additional overloaded method? and m2 from Super Class S is still accessible as normally without calling super.

so if both methods in super and class C had the same signature and parameters wouldnt u be inclined to think that m2 would be called from the Class C? Since it basically overrided it from the Super class.

lol this is more like a question, sorry i just need some clarification :D

Link to comment
Share on other sites

  • 0
If you had the same method in C as the super class does, doesn't that override it?
yep.
and if the method header was the same and the parameters were different wouldn't it just mean the method m2 in class C becomes an additional overloaded method? and m2 from Super Class S is still accessible as normally without calling super.

right again.

so if both methods in super and class C had the same signature and parameters wouldnt u be inclined to think that m2 would be called from the Class C? Since it basically overrided it from the Super class.

Correct again :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.