How can I override instance methods of super class as static in subclass? I think this is impossible but is there any indirect way?
public class A {
public void test(){
System.out.println("");
}
}
public class B extends A{
public static void test(){//test() in B cannot override test() in A
//overriding method is static
}
}
You can't, since a static method is not an instance method. You could override the instance method with an instance method that calls the static method.
public class B extends A {
#Override
public void test(){
staticTest();
}
public static void staticTest() {
}
}
I'm not sure how much sense that would make though.
Related
suppose I have an interface:
public interface abcd{
public int a();
public void b();
public void c();
public String d(String h, String j);
}
and I implement it in some class:
public class xyzw implements abcd{
}
but I want the method d() to be static, but I can't do this:
public class xyzw implements abcd{
public static void c(){
//some code
}
}
neither can I do this:
public interface abcd{
public int a();
public void b();
public static void c();
public String d(String h, String j);
}
I wonder if there is something or some workaround or some language construct which allows me to make a method defined by an interface a static method?
You can define a static method in interface, but only with implementation.
public interface A {
public static void b() {
System.out.println("Hi");
}
}
Overriding of static methods is not allowed in Java, because you call it on Class object, not on implementation object.
If you can implement a static method in an interface, but you cannot overwrite it, remember that a static method is referenced by the class itself and not by an instance of it.
To solve your problem maybe you could define an abstract class
No, its not possible and doesn't make any sense. An interface is meant to be implemented by subclasses you can only hava a non-abstract, implemented, static method in an interface. You could not statically call your interface method with
abcd.c()
when it has no implementation. Thats why static elements can not be overridden.
It's not possible to override static methods in java.
However, in the subclass, you can declare static method with the same name and "mask it as" the original method - which is as close as you can get.
interface a {
public static int f() {
return 0;
}
}
interface b extends a {
public static int f() {
return 1;
}
}
System.out.println(a.f());
>> 0
System.out.println(b.f());
>> 1
I'm working on a game, and this question came up: if a variable is set to an instance of a class, and that classes constructor sets the variable, what does the variable end up as? For example:
public class MainClass {
public static MainInterface mainInterface;
public static void main(String[] args) {
mainInterface = new SubClass1();
}
}
public interface MainInterface {
public void method();
}
public class SubClass1 implements MainInterface {
public SubClass1() {
MainClass.mainInterface = new SubClass2();
}
public void method() { }
}
public class SubClass2 implements MainInterface {
public void method() { }
}
So, in this example, would mainInterface be an instance of SubClass1 or SubClass2?
Test it and find out! :) A simple System.out.println(mainInterface.getClass()) will print out the class value.
I'm fairly certain that the process works like this:
SubClass1's constructor is called, setting mainInterface to a new
SubClass2 object.
SubClass1's constructor finishes and is assigned to mainInterface, resulting in mainInterface being the newly created SubClass1 object.
I have 3 classes. It seems basic question. But I can'nt find answer by googling.
public abstract class Test {
void t1()
{
System.out.println("super");
}
}
public class concret extends Test{
void t1()
{
System.out.println("child");
}
void t2()
{
System.out.println("child2");
}
}
public class run {
public static void main(String[] args) {
Test t=new concret();
t.t1();
}
}
How do I call abstract class t1 method? Since I cant create object from abstract class how do I call t1 in abstract class?
Thank you.
Either you create a concrete class which doesn't override the method, or within a concrete class which does override the method, you can call super.t1(). For example:
void t1()
{
super.t1(); // First call the superclass implementation
System.out.println("child");
}
If you've only got an instance of an object which overrides a method, you cannot call the original method from "outside" the class, because that would break encapsulation... the purpose of overriding is to replace the behaviour of the original method.
you should be able to do it using
Test test = new Test(){};
test.t1();
Abstract class means the class has the abstract modifier before the class keyword. This means you can declare abstract methods, which are only implemented in the concrete classes.
For example :
public abstract class Test {
public abstract void foo();
}
public class Concrete extends Test {
public void foo() {
System.out.println("hey");
}
}
See following tests:
public abstract class BaseClass {
public void doStuff() {
System.out.println("Called BaseClass Do Stuff");
}
public abstract void doAbstractStuff();
}
public class ConcreteClassOne extends BaseClass{
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassOne Do Stuff");
}
}
public class ConcreteClassTwo extends BaseClass{
#Override
public void doStuff() {
System.out.println("Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassTwo Do Stuff");
}
}
public class ConcreteClassThree extends BaseClass{
#Override
public void doStuff() {
super.doStuff();
System.out.println("-Overriding BaseClass Do Stuff");
}
#Override
public void doAbstractStuff() {
System.out.println("Called ConcreteClassThree Do Stuff");
}
}
public class Test {
public static void main(String[] args) {
BaseClass a = new ConcreteClassOne();
a.doStuff(); //Called BaseClass Do Stuff
a.doAbstractStuff(); //Called ConcreteClassOne Do Stuff
BaseClass b = new ConcreteClassTwo();
b.doStuff(); //Overriding BaseClass Do Stuff
b.doAbstractStuff(); //Called ConcreteClassTwo Do Stuff
BaseClass c = new ConcreteClassThree();
c.doStuff(); //Called BaseClass Do Stuff
//-Overriding BaseClass Do Stuff
c.doAbstractStuff(); //Called ConcreteClassThree Do Stuff
}
}
use keyword 'super' to do that
void t1()
{ super.t1();
System.out.println("child");
}
Make sure you use that in the overriden method though.
Your code seems to call t1(). However this is calling the concrete t1() because the abstract t1() has been overridden by the concrete class.
If you wish to call the abstract t1 method from main code, do not override the t1() in concrete.
Or you can create a method in the concrete class for example:
public void invokeSuperT1(){
super.t1();
}
Create an anonymous Inner class,
Abstract class:
abstract class Test{
abstract void t();
public void t1(){
System.out.println("Test");
}
}
Here is how to create anonymous inner class:
Test test = new Test() {
#Override
void t() {
//you can throw exception here, if you want
}
};
Call the class via the object created for abstract class,
test.t1();
An abstract class is used when we want that every class that inherited from our abstract class should implement that abstract method, so it is must to implement method otherwise it gives the compile-time error.
void t1()
{
super.t1; // means the parent methods
System.out.println("child");
}
For example: Bird class has method sing() and there other classes that inherited from it like the sparrow, Pigeon, Duck, so these all have sing method so we make Bird class Abstract and make the sing() method abstract in it so every child of bird that implements Bird class should have a method of sing() with its on implementation.
First Create abstarct class like as shown in link: Creating Abstract Class
Create Sub-Classs like as shown in link: Sub-class extending
Creating main method for executing this as show in link: Instanciate the subclass to access
Result as shown here: Result
Given the following block of code:
public class Trial {
public static void main (String[] args){
B obj = new B();
obj.doMethod(); #prints "From A".
}
}
class A {
private void method(){System.out.print("from A");}
public void doMethod(){method();}
}
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
It turns out that the method() from class A is invoked. Why is this?
You explicitly implement it that way. super calls method from base class which is A
public void doMethod(){super.doMethod();}
So the method chaining is like this:
B.doMethod() -> A.doMethod() -> A.method() -> "from A"
I think your question is if in class A private void method(){System.out.print("from A");} is private then why is printing "from A" in class B.
Answer is very simple you can't call method() of A class form any other class .But you can call it with object of its own.
when you calls super.doMethod(); then its function of super and method() is its own method so it can call it.
Because, see below:
class B extends A {
public void method(){System.out.print("from B");}
public void doMethod(){super.doMethod();}
}
Here in Class B's doMethod() you're invoiking Class A's doMethod() using super.doMethod(). So obviously it's printing Class A's doMethod().
You call the doMethod with super keyword. It's means it will call parent implementation
More on super keyword
Your code gives simple object creation (B obj = new B();) and a call using super. Super is used like other people mentioned for parent class. Things could have been different if you try something like (A obj = new B();), which is more interesting.
method() in class A is private and private methods can't be overriden. And when overriding it's better to use #Override annotion.
class B extends A {
#Override
public void method(){System.out.print("from B");} // Compile error
}
A similar thing happens, if you change the method to a static method.
class A {
public static void method(){System.out.print("from A");}
}
class B extends A {
public static void method(){System.out.print("from B");}
}
class base
{
public void superMethod()
{
System.out.println("Hello i m a super class method");
}
}
class der extends base
{
super.superMethod();// error identifier expected
}
i need to call a base class method without overloading it into derive class please provide me solution for it
You don't have to do anything apart from calling the method. You do need to be inside a instance method, constructor or a instance initializer block to do that though. You only need the super keyword if you have to make a distinction when you are overriding the method.
class Base
{
public void superMethod()
{
System.out.println("Hello i m a super class method");
}
}
class Der extends base
{
public void method()
{
superMethod();// error identifier expected
}
public void superMethod()
{
super.superMethod()
}
}
You can't put code inside a method. The following should work:
class base {
public void superMethod() {
System.out.println("Hello i m a super class method");
}
}
class der extends base {
public void someMethod() {
super.superMethod();
}
}
You don't need to do anything special. Just call the method in the base class. Since you have not overloaded it, it will automatically call the base class method.
You can call the super class method following the below example.
class base
{
public void superMethod()
{
System.out.println("hello i m a super class method");
}
}
class der extends base
{
{
super.superMethod();
}
}
class Supertest
{
public static void main(String args[])
{
new der();
}
}
class base{
public void superMethod(){
System.out.println("hello i am super class method");
}
}
class der extends base{
void supermethod(){
super.superMethod();
System.out.println("hello this is der class");
}
}
class mainclass{
public static void main(String[] args){
der r=new der();
r.supermethod();
}
}