how to call a overridden method from subclass object in java [duplicate] - java

This question already has answers here:
Can java call parent overridden method in other objects but not subtype?
(4 answers)
Closed 8 years ago.
Is there a way to call test() in class a from class b object created in class c ?
class a {
void test(){
System.out.println("in a");
}
}
class b extends a {
void test(){
System.out.println("in b");
}
}
public class c{
public static void main(String[] args) {
b refb = new b();
refb.test();
}
}

You can do that only within the test() method of class b like following.
class b extends a {
void test(){
super.test();
System.out.println("in b");
}
}

In Java, all non static private methods are virtual by default. So, there's no way to invoke a#test from b instance unless you modify b#test. The only way to do it (by your current design) is using an instance of a:
public class c{
public static void main(String[] args) {
b refb = new b();
// code to call test() in class a
//this is the only way you have in Java
a refA = new a();
a.test();
}
}

Related

Is there different between this two ways of creating an object

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
}

Java Function Call with Overload [duplicate]

This question already has answers here:
Method overloading using derived types as parameters in Java
(3 answers)
Closed 4 years ago.
I want to know why the third output is NOT b.
Here is my code:
public class SimpleTests {
public void func(A a) {
System.out.println("Hi A");
}
public void func(B b) {
System.out.println("Hi B");
}
public static void main(String[] args) {
A a = new A();
B b = new B();
A c = new B();
SimpleTests i = new SimpleTests();
i.func(a);
i.func(b);
i.func(c);
}
}
class A {}
class B extends A {}
And here is the output:
Hi A
Hi B
Hi A
Could someone tell me why the 3rd output is Hi A, NOT Hi B. as the real c is a instance of B.
You're confusing overloading with polymorphism.
With polymorphism, when creating an instance of class B which is a subclass of class A, referenced to by an class A object, and overwrites the method of class A, calling the method will perform the method of class B.
With overloading, the called method only knows the type of the declaration of the argument, not the initialization.
public class A {
public void print() {
System.out.println("A");
}
}
public class B extends A {
#Override
public void print() {
System.out.println("B");
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
B b = new B();
A otherB = new B();
a.print();
b.print();
otherB.print();
}
}
This will output
A
B
B
Calls to overloaded methods are resolved based on the reference type (A) of the argument at compile time, not the object type (B) at runtime. You declared the variable to be of type A, so it is treated as type A.
JLS ยง8.4.9. Overloading:
When a method is invoked, the number of actual arguments (and any
explicit type arguments) and the compile-time types of the arguments
are used, at compile time, to determine the signature of the method
that will be invoked.
The compile-time types in your example are:
A a = new A(); // A
^
B b = new B(); // B
^
A c = new B(); // A
^
Therefore, the output is:
Hi A
Hi B
Hi A

How to call one class1 from another class2? [duplicate]

This question already has answers here:
Call a Class From another class [closed]
(5 answers)
Closed 5 years ago.
Actually am new to Java,How to call one class1 from another class2?
Class1 has main() and other methods.Class2 has different methods.
I want to call class1 from Class2. Please provide the syntax.
You need to first create an object of type class2 and call the methods of it from main method of class1.
class2 c = new class2();
c.methodOfClass2();
Say you have the following classes:
public class A {
int a1 = 15;
public void showMessage() {
System.out.println("Hey!");
}
}
public class B {
}
In case you want your class B to be able to read a1 and call showMessage(), you need to create an object of the class they belong, in the class you'll be working in. Like this:
public class A {
int a1 = 15;
public void showMessage() {
System.out.println("Hey!");
}
}
public class B {
public static void main(String[] args) {
A a = new A();
//call either variables or methods by putting
//a. in front of them
}
}
To call methods of Class1 from Class2
If static method, call by className. e.g - Class1.staticMethodToBeCalledFromClass2();
If non-static method, you need to create object of Class1. e.g - Class1 cls1 = new Class1(); cls1.nonStaticMethodToBeCalledFromClass2();
Assuming Your code :
public class Class1{
public static void main(String[] args) {
}
public void nonStaticMethodTobeCalledFromClass2() {
}
public static void staticMethodTobeCalledFromClass2() {
}
}
public class Class2 {
public void callClass1Here() {
Class1 cls1 = new Class1();
cls1.nonStaticMethodTobeCalledFromClass2();
Class1.staticMethodTobeCalledFromClass2();
}
}
If you look at the code, you will see, to call

In Java, Does Object of some class created in superclass can be used in subclass

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

Why can I not access an inner class in Java? [duplicate]

This question already has answers here:
What causes error "No enclosing instance of type Foo is accessible" and how do I fix it?
(11 answers)
Closed 8 years ago.
I am having a trouble using nested classes in Java, does anyone know why Java does not allow me to do it?
public class A{
private class B{
public B(){
System.out.println("class B");
}
}
public static void main(String[] args){
A a = new A();
B b = new B();
}
}
Because you are trying to access a non-static inner-class from a static method.
The 1st solution will be to change your inner-class B to static:
public class A{
private static class B {
public B() {
System.out.println("class B");
}
}
public static void main(String[] args){
A a = new A();
B b = new B();
}
}
a static inner-class is accessible from anywhere, but a non-static one, requires an instance of your container class.
Another solution will be:
A a = new A();
B b = a.new B();
This will give you a better understanding of how inner classes work in Java: http://www.javaworld.com/article/2077411/core-java/inner-classes.html
A a = new A();
B b = a.new B();
can solve your problem
You used private inner class.How can you get instance outside the A class?
public class JustForShow {
public class JustTry{
public JustTry() {
System.out.println("Initialized");
}
}
public static void main(String[] args) {
JustForShow jfs = new JustForShow();
JustTry jt = jfs.new JustTry();
}
}
You are trying to access a non-static membor from a static method. To solve that, you have two options:
Change your class B to be static, so add the static keyword in the class definition, like this:
public static class B { // ...
Change your main method, and use the created instance a to create B, like this:
B b = a.new B();
If B doesn't use any non-static resources of class A, I would recommend to use the first method.
public static void main(String[] args){
A a = new A();
A.B b = a.new B();
}

Categories

Resources