gudii9
asked on
static method overriding vs normal non static method overriding in child class
Hi,
How static method overriding vs normal non static method overriding in child class are different?. child Static method overriding hides parent class static method right which is same with non static methods also right? When we use each one. Please advise
public class Parent {
public static void test() {
System.out.println("test from Parent class");
}
}
public class Child extends Parent {
public static void test() {
System.out.println("test from Child");
}
}
now non static case as below
public class Parent {
public void test() {//non static
System.out.println("test from Parent class of nonstatic");
}
}
public class Child extends Parent {
public void test() {//non static
System.out.println("test from Child of nonstatic");
}
}
How static method overriding vs normal non static method overriding in child class are different?. child Static method overriding hides parent class static method right which is same with non static methods also right? When we use each one. Please advise
public class Parent {
public static void test() {
System.out.println("test from Parent class");
}
}
public class Child extends Parent {
public static void test() {
System.out.println("test from Child");
}
}
now non static case as below
public class Parent {
public void test() {//non static
System.out.println("test from Parent class of nonstatic");
}
}
public class Child extends Parent {
public void test() {//non static
System.out.println("test from Child of nonstatic");
}
}
ASKER
public class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
public void testInstanceMethod() {
System.out.println("The instance method in Animal");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
The Cat class overrides the instance method in Animal and hides the static method in Animal. The main method in this class creates an instance of Cat and invokes testClassMethod() on the class and testInstanceMethod() on the instance.
The output from this program is as follows:
The static method in Animal
The instance method in Cat
Cat myCat = new Cat();
Animal myAnimal = myCat;
as above after creating cat object(myCat) why they are assigning to Animal instance ie myAnimal
ASKER
package com.upcast;
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
myAnimal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
i changed
Animal.testClassMethod();
to
myAnimal.testClassMethod()
still got same output as
The static method in Animal
The instance method in Cat
what is exact meaning of hiding? i did not get that. i understood oerriding part of parent method with child method which dominates
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This type of code is confusing, and should generate a warning in the compiler:
i see warning but it does work though giving below output
The static method in Animal
ASKER
what is it exactly hiding here? why it is called hiding?
Cat myCat = new Cat();
Animal myAnimal = myCat;
as above after creating cat object(myCat) why they are assigning to Animal instance ie myAnimal??
Cat myCat = new Cat();
Animal myAnimal = myCat;
as above after creating cat object(myCat) why they are assigning to Animal instance ie myAnimal??
ASKER
when i try this example getting error
http://crunchify.com/java-method-hiding-and-overriding-override-static-method-in-java/
my error is'
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from CrunchifyCompany to Company
The method staticMethod() is undefined for the type Company
The method nonStaticMethod() is undefined for the type Company
at com.solution.CrunchifyHide StaticMeth od.main(Cr unchifyHid eStaticMet hod.java:1 1)
please advise
http://crunchify.com/java-method-hiding-and-overriding-override-static-method-in-java/
package com.solution;
import com.upcast.Company;
/**
* @author Crunchify.com
*/
public class CrunchifyHideStaticMethod {
public static void main(String args[]) {
Company cmp = new CrunchifyCompany();
// if we can override static, this should call method from Child class
cmp.staticMethod(); // Eclipse should show warning: The static method
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
}
}
package com.solution;
// CrunchifyCompany: Sub-Child Class
class CrunchifyCompany extends Company {
/*
* static method of same name and method signature as existed in super
* class, this is not method overriding instead this is called method hiding
* in Java
*/
public static void staticMethod() {
System.err.println("CrunchifyCompany: Overridden Static Instance method");
}
// non static method
public void nonStaticMethod() {
System.out.println("CrunchifyCompany: non-Static method");
}
}
package com.solution;
// Comapny: Super-Parent Class
class Company {
// public static method which can not be overridden in Java
public static void staticMethod() {
System.out.println("Company: Static Instance method");
}
// non static method
public void nonStaticMethod() {
System.out.println("Company: non-Static method");
}
}
package com.solution;
public class CohesionExampleProblemSolution {
}
class InternetDownloader {
void downloadFromInternet() {
}
class DataParser {
void parseData(String content) {
}
}
class DatabaseStorer {
void storeIntoDatabase(String data) {
}
}
void doEverything() {
new InternetDownloader().downloadFromInternet();
new DataParser().parseData();
new DatabaseStorer().storeIntoDatabase();
}
}
/*
* public class CohesionExampleProblem{
*
* }
*/
my error is'
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from CrunchifyCompany to Company
The method staticMethod() is undefined for the type Company
The method nonStaticMethod() is undefined for the type Company
at com.solution.CrunchifyHide
please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it got resolved.
I wonder why we got below output
Company: Static Instance method
CrunchifyCompany: non-Static method
i expected as below?
CrunchifyCompany: Overridden Static Instance method
CrunchifyCompany: non-Static method
please advise
I wonder why we got below output
Company: Static Instance method
CrunchifyCompany: non-Static method
i expected as below?
CrunchifyCompany: Overridden Static Instance method
CrunchifyCompany: non-Static method
please advise
ASKER
package com.solution;
import com.solution.Company;
/**
* @author Crunchify.com
*/
public class CrunchifyHideStaticMethod {
public static void main(String args[]) {
Company cmp = new CrunchifyCompany();
// if we can override static, this should call method from Child class
cmp.staticMethod(); // Eclipse should show warning: The static method
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
}
}
package com.solution;
// CrunchifyCompany: Sub-Child Class
class CrunchifyCompany extends Company {
/*
* static method of same name and method signature as existed in super
* class, this is not method overriding instead this is called method hiding
* in Java
*/
public static void staticMethod() {
System.err.println("CrunchifyCompany: Overridden Static Instance method");
}
// non static method
public void nonStaticMethod() {
System.out.println("CrunchifyCompany: non-Static method");
}
}
package com.solution;
// Comapny: Super-Parent Class
class Company {
// public static method which can not be overridden in Java
public static void staticMethod() {
System.out.println("Company: Static Instance method");
}
// non static method
public void nonStaticMethod() {
System.out.println("Company: non-Static method");
System.out.println("test commit in git");
}
}
package com.solution;
public class CohesionExampleProblemSolution {
}
class InternetDownloader {
void downloadFromInternet() {
}
class DataParser {
void parseData(String content) {
}
}
class DatabaseStorer {
void storeIntoDatabase(String data) {
}
}
void doEverything() {
new InternetDownloader().downloadFromInternet();
new DataParser().parseData();
new DatabaseStorer().storeIntoDatabase();
}
}
/*
* public class CohesionExampleProblem{
*
* }
*/
This is why it is not recommended to call a static method via an object. Since that reference was typed as a Company it calls that method. Most editors, like eclipse will give you a warning. You really should call static methods directly like Company.staticMethod();
ASKER
cmp.staticMethod(); // Eclipse should show warning: The static method
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
so i modified as below to make compiler happy
company.staticMethod(); // Eclipse should show warning: The static method
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
to conclude:
so method call is happened generally based on the reference type right but in case of non static overridden method it happens instead of reference type based on actual object type right?
but in case of static overridden method it happens instead of reference type best to use class name and call is based on actual classobject type right even though method seems like overrident but it is not but just hiding there ?
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
so i modified as below to make compiler happy
company.staticMethod(); // Eclipse should show warning: The static method
// staticMethod() from the type Company should be
// accessed in a static way
cmp.nonStaticMethod();
to conclude:
so method call is happened generally based on the reference type right but in case of non static overridden method it happens instead of reference type based on actual object type right?
but in case of static overridden method it happens instead of reference type best to use class name and call is based on actual classobject type right even though method seems like overrident but it is not but just hiding there ?
ASKER
https://docs.oracle.com/javase/tutorial/java/IandI/override.html
in above link
in above link
The second class, a subclass of Animal, is called Cat:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public void testInstanceMethod() {
System.out.println("The instance method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;//what is the need of this step. why assigning mycat to myAnimal. it makes no sense to me????
Animal.testClassMethod();
myAnimal.testInstanceMethod();
}
}
The Cat class overrides the instance method in Animal and hides the static method in Animal. The main method in this class creates an instance of Cat and invokes testClassMethod() on the class and testInstanceMethod() on the instance.
The output from this program is as follows:
The static method in Animal
The instance method in Cat
As promised, the version of the hidden
Parent.test() vs Child.test()
Both will work and call the static function declared in the classes.
However when you have a regular method. The method being called will be whichever the class implements.
Here is a good description. https://docs.oracle.com/javase/tutorial/java/IandI/override.html