Reputation: 7031
I have created the class like below. But Override method is not called. Is anything I missed? Otherwise i need to add any key word(like Virtual
) to use as Override?
MyWebView.java
package com.example.stackoverflow;
import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
public class MyWebView extends WebView
{
WebView localView;
static Context localcontext;
A aclass;
public MyWebView(Context context)
{
super(context);
localcontext=context;
}
public LinearLayout init()
{
LinearLayout layout=new LinearLayout(localcontext);
localView=new WebView(localcontext);
localView.setWebViewClient(new MyClient());
aclass=new A();
layout.addView(localView);
return layout;
}
class MyClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
aclass.startMyMethod();
return super.shouldOverrideUrlLoading(view, url);
}
}
}
A.java
package com.example.stackoverflow;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.app.Activity;
public class A extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyWebView view=new MyWebView(this);
LinearLayout layout = view.init();
view.loadUrl("https://www.google.co.in/");
setContentView(layout);
}
public void startMyMethod()
{
System.out.println("ParentMethod");
}
}
B.java(MainActivity)
package com.example.stackoverflow;
import android.os.Bundle;
public class B extends A {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void startMyMethod() {
System.out.println("ChildMethod");
super.startMyMethod();
}
}
Note: class B is client side class. others two in jar file. how can i use new B() inside jar file(library source). name B may be changed. If parent method is overridden, then parent content should not be executed
Upvotes: 0
Views: 3413
Reputation: 5946
In your init() method of MyWebView.java
you initialize variable
aclass
variable as aclass=new A();
In shouldOverrideUrlLoading()
method, you call
aclass.startMyMethod();
Since A is parent class, it will only called startMyMethod() of A class.
In order to get advantage of inheritance and override, you can initialize
aclass
variable as aclass=new B();
This will call startMyMethod() of B class and then call startMyMethod() of A class because of using super.startMyMethod();
http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html said that
@Override—the @Override annotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled "Interfaces and Inheritance").
Not for program execution sequence. I hope it may be your want.
Upvotes: 1
Reputation: 16158
Java does not have any keyword like virtual
, by default all java non-static methods are virtual. See http://en.wikipedia.org/wiki/Virtual_function#Java.
@Override annotation is used to instruct compiler that you are overriding super class method. See here
A a =new A();
a.startMyMethod(); // calls A.startMyMethod()
A b = new B(); // Object reference of type A and Object of B
a.startMyMethod(); // calls B.startMyMethod()
Upvotes: 2
Reputation: 8278
It should work if you create an instance of ClassB and call the overridden method like below:
ClassB b = new ClassB();
b.strartMyMehtod();
Put some prints inside the ClassB method before the call to the parent to see the difference.
class A
{
public void startMyMethod()
{
System.out.println("A.startMyMethod() called");
}
}
class B extends A
{
@Override
public void startMyMethod() {
System.out.println("B.startMyMethod() called");
// TODO Auto-generated method stub
super.startMyMethod();
}
}
Upvotes: 1