I'm currently doing an assignment for one of my classes, and in it, I have to give examples, using Java syntax, of static and dynamic binding.
I understand the basic concept, that static binding happens at compile time and dynamic binding happens at runtime, but I can't figure out how they actually work specifically.
I found an example of static binding online that gives this example:
public static void callEat(Animal animal) {
System.out.println("Animal is eating");
}
public static void callEat(Dog dog) {
System.out.println("Dog is eating");
}
public static void main(String args[])
{
Animal a = new Dog();
callEat(a);
}
And that this would print "animal is eating" because the call to callEat uses static binding, but I'm unsure as to why this is considered static binding.
So far none of the sources I've seen have managed to explain this in a way that I can follow.
From Javarevisited blog post:
Here are a few important differences between static and dynamic binding:
Static binding in Java occurs during compile time while dynamic binding occurs during runtime.
private, final and static methods and variables use static binding and are bonded by compiler while virtual methods are bonded during runtime based upon runtime object.
Static binding uses Type (class in Java) information for binding while dynamic binding uses object to resolve binding.
Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.
Here is an example which will help you to understand both static and dynamic binding in Java.
Static Binding Example in Java
public class StaticBindingTest {
public static void main(String args[]) {
Collection c = new HashSet();
StaticBindingTest et = new StaticBindingTest();
et.sort(c);
}
//overloaded method takes Collection argument
public Collection sort(Collection c) {
System.out.println("Inside Collection sort method");
return c;
}
//another overloaded method which takes HashSet argument which is sub class
public Collection sort(HashSet hs) {
System.out.println("Inside HashSet sort method");
return hs;
}
}
Output: Inside Collection sort method
Example of Dynamic Binding in Java
public class DynamicBindingTest {
public static void main(String args[]) {
Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
vehicle.start(); //Car's start called because start() is overridden method
}
}
class Vehicle {
public void start() {
System.out.println("Inside start method of Vehicle");
}
}
class Car extends Vehicle {
#Override
public void start() {
System.out.println("Inside start method of Car");
}
}
Output: Inside start method of Car
Connecting a method call to the method body is known as Binding. As Maulik said "Static binding uses Type(Class in Java) information for binding while Dynamic binding uses Object to resolve binding." So this code :
public class Animal {
void eat() {
System.out.println("animal is eating...");
}
}
class Dog extends Animal {
public static void main(String args[]) {
Animal a = new Dog();
a.eat(); // prints >> dog is eating...
}
#Override
void eat() {
System.out.println("dog is eating...");
}
}
Will produce the result: dog is eating... because it is using the object reference to find which method to use. If we change the above code to this:
class Animal {
static void eat() {
System.out.println("animal is eating...");
}
}
class Dog extends Animal {
public static void main(String args[]) {
Animal a = new Dog();
a.eat(); // prints >> animal is eating...
}
static void eat() {
System.out.println("dog is eating...");
}
}
It will produce : animal is eating... because it is a static method, so it is using Type (in this case Animal) to resolve which static method to call. Beside static methods private and final methods use the same approach.
Well in order to understand how static and dynamic binding actually works? or how they are identified by compiler and JVM?
Let's take below example where Mammal is a parent class which has a method speak() and Human class extends Mammal, overrides the speak() method and then again overloads it with speak(String language).
public class OverridingInternalExample {
private static class Mammal {
public void speak() { System.out.println("ohlllalalalalalaoaoaoa"); }
}
private static class Human extends Mammal {
#Override
public void speak() { System.out.println("Hello"); }
// Valid overload of speak
public void speak(String language) {
if (language.equals("Hindi")) System.out.println("Namaste");
else System.out.println("Hello");
}
#Override
public String toString() { return "Human Class"; }
}
// Code below contains the output and bytecode of the method calls
public static void main(String[] args) {
Mammal anyMammal = new Mammal();
anyMammal.speak(); // Output - ohlllalalalalalaoaoaoa
// 10: invokevirtual #4 // Method org/programming/mitra/exercises/OverridingInternalExample$Mammal.speak:()V
Mammal humanMammal = new Human();
humanMammal.speak(); // Output - Hello
// 23: invokevirtual #4 // Method org/programming/mitra/exercises/OverridingInternalExample$Mammal.speak:()V
Human human = new Human();
human.speak(); // Output - Hello
// 36: invokevirtual #7 // Method org/programming/mitra/exercises/OverridingInternalExample$Human.speak:()V
human.speak("Hindi"); // Output - Namaste
// 42: invokevirtual #9 // Method org/programming/mitra/exercises/OverridingInternalExample$Human.speak:(Ljava/lang/String;)V
}
}
When we compile the above code and try to look at the bytecode using javap -verbose OverridingInternalExample, we can see that compiler generates a constant table where it assigns integer codes to every method call and byte code for the program which I have extracted and included in the program itself (see the comments below every method call)
By looking at above code we can see that the bytecodes of humanMammal.speak(), human.speak() and human.speak("Hindi") are totally different (invokevirtual #4, invokevirtual #7, invokevirtual #9) because the compiler is able to differentiate between them based on the argument list and class reference. Because all of this get resolved at compile time statically that is why Method Overloading is known as Static Polymorphism or Static Binding.
But bytecode for anyMammal.speak() and humanMammal.speak() is same (invokevirtual #4) because according to compiler both methods are called on Mammal reference.
So now the question comes if both method calls have same bytecode then how does JVM know which method to call?
Well, the answer is hidden in the bytecode itself and it is invokevirtual instruction set. JVM uses the invokevirtual instruction to invoke Java equivalent of the C++ virtual methods. In C++ if we want to override one method in another class we need to declare it as virtual, But in Java, all methods are virtual by default because we can override every method in the child class (except private, final and static methods).
In Java, every reference variable holds two hidden pointers
A pointer to a table which again holds methods of the object and a pointer to the Class object. e.g. [speak(), speak(String) Class object]
A pointer to the memory allocated on the heap for that object’s data e.g. values of instance variables.
So all object references indirectly hold a reference to a table which holds all the method references of that object. Java has borrowed this concept from C++ and this table is known as virtual table (vtable).
A vtable is an array like structure which holds virtual method names and their references on array indices. JVM creates only one vtable per class when it loads the class into memory.
So whenever JVM encounter with a invokevirtual instruction set, it checks the vtable of that class for the method reference and invokes the specific method which in our case is the method from a object not the reference.
Because all of this get resolved at runtime only and at runtime JVM gets to know which method to invoke, that is why Method Overriding is known as Dynamic Polymorphism or simply Polymorphism or Dynamic Binding.
You can read it more details on my article How Does JVM Handle Method Overloading and Overriding Internally.
The compiler only knows that the type of "a" is Animal; this happens at compile time, because of which it is called static binding (Method overloading). But if it is dynamic binding then it would call the Dog class method. Here is an example of dynamic binding.
public class DynamicBindingTest {
public static void main(String args[]) {
Animal a= new Dog(); //here Type is Animal but object will be Dog
a.eat(); //Dog's eat called because eat() is overridden method
}
}
class Animal {
public void eat() {
System.out.println("Inside eat method of Animal");
}
}
class Dog extends Animal {
#Override
public void eat() {
System.out.println("Inside eat method of Dog");
}
}
Output:
Inside eat method of Dog
There are three major differences between static and dynamic binding while designing the compilers and how variables and procedures are transferred to the runtime environment.
These differences are as follows:
Static Binding: In static binding three following problems are discussed:
Definition of a procedure
Declaration of a name(variable, etc.)
Scope of the declaration
Dynamic Binding: Three problems that come across in the dynamic binding are as following:
Activation of a procedure
Binding of a name
Lifetime of a binding
With the static method in the parent and child class: Static Binding
public class test1 {
public static void main(String args[]) {
parent pc = new child();
pc.start();
}
}
class parent {
static public void start() {
System.out.println("Inside start method of parent");
}
}
class child extends parent {
static public void start() {
System.out.println("Inside start method of child");
}
}
// Output => Inside start method of parent
Dynamic Binding :
public class test1 {
public static void main(String args[]) {
parent pc = new child();
pc.start();
}
}
class parent {
public void start() {
System.out.println("Inside start method of parent");
}
}
class child extends parent {
public void start() {
System.out.println("Inside start method of child");
}
}
// Output => Inside start method of child
All answers here are correct but i want to add something which is missing.
when you are overriding a static method, it looks like we are overriding it but actually it is not method overriding. Instead it is called method hiding. Static methods cannot be overridden in Java.
Look at below example:
class Animal {
static void eat() {
System.out.println("animal is eating...");
}
}
class Dog extends Animal {
public static void main(String args[]) {
Animal a = new Dog();
a.eat(); // prints >> animal is eating...
}
static void eat() {
System.out.println("dog is eating...");
}
}
In dynamic binding, method is called depending on the type of reference and not the type of object that the reference variable is holding
Here static bindinghappens because method hiding is not a dynamic polymorphism.
If you remove static keyword in front of eat() and make it a non static method then it will show you dynamic polymorphism and not method-hiding.
i found the below link to support my answer:
https://youtu.be/tNgZpn7AeP0
In the case of the static binding type of object determined at the compile-time whereas in
the dynamic binding type of the object is determined at the runtime.
class Dainamic{
void run2(){
System.out.println("dainamic_binding");
}
}
public class StaticDainamicBinding extends Dainamic {
void run(){
System.out.println("static_binding");
}
#Override
void run2() {
super.run2();
}
public static void main(String[] args) {
StaticDainamicBinding st_vs_dai = new StaticDainamicBinding();
st_vs_dai.run();
st_vs_dai.run2();
}
}
Because the compiler knows the binding at compile time. If you invoke a method on an interface, for example, then the compiler can't know and the binding is resolved at runtime because the actual object having a method invoked on it could possible be one of several. Therefore that is runtime or dynamic binding.
Your invocation is bound to the Animal class at compile time because you've specified the type. If you passed that variable into another method somewhere else, noone would know (apart from you because you wrote it) what actual class it would be. The only clue is the declared type of Animal.
Related
I am learning Java and have been playing with up and downcasting in my command prompt using the classes I defined in Eclipse. I am working with three classes that I included here, and my question is at the bottom:
public class Animal {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void eat() {
System.out.println("Yummy");
}
public void sleep() {
System.out.println("I like sleep.");
}
}
public class Mammal extends Animal {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void controlTemp() {
System.out.println("Temperature regulation ");
}
}
public class Lion extends Mammal {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
#Override
public void sleep() {
super.sleep();
System.out.println("Especially on the grass.");
}
#Override
public void controlTemp() {
super.controlTemp();
System.out.println("I'm an endotherm.");
}
public void roar() {
System.out.println("ROARRRRR!");
}
}
Here is what I did in jshell in my cmd:
jshell> Animal a1 = new Lion();
a1 ==> Lion#4afc40bb
| created variable a1 : Animal
jshell> ((Mammal)a1).controlTemp();
Temperature regulation
I'm an endotherm.
jshell> ((Lion)a1).controlTemp();
Temperature regulation
I'm an endotherm.
My question is why does casting a1 to Mammal give me the result of the overridden controlTemp() method in my Lion class instead of the one in the Mammal class?
Because all non-static methods in java are always resolved via a thing called dynamic dispatch.
To be slightly more specific, what method signature you actually select depends on the type of the thing you invoke it on. The expression ((Mammal) a1) is of type Mammal (not because a1 is a lion; it's because anything cast to Mammal is an expression of type Mammal).
But, for a given signature, the actual method you get? That depends entirely on the runtime type of the actual object that your expression references.
At the runtime level, methods don't just have a name: Their full signature includes their parameter types, their name, and their return type.
As your #Override annotation indicates, the public void controlTemp() in your Lion class has the same name, the same argument types (namely: noa arguments), and the same return type (namely: void), and thus it overrides the void controlTemp() method in Mammal.
This gets us to the following flow:
You 'selected' the controlTemp() method as defined in Mammal, and this is compiled into your class file. In other words, during compilation, it's all about Mammal. That's what the expression's type is.
But at runtime, this method is dynamically dispatched: The actual object that your a1 ref is pointing at is checked, is found to be an instance of Lion, and the most specific version of the controlTemp method that any instance of Lion has, is the one defined in Lion. So that one gets invoked.
So how do you invoke the one from animal? not possible, except from within public class Lion extends Mammal {}, where you can use super.controlTemp() to invoke it. Anywhere else, you simply cannot do so (at least, not without raw class hackery, which is well beyond scope of the level you're at and not something anybody writing in java-the-language ever does for good reason).
This question already has answers here:
What is method hiding in Java? Even the JavaDoc explanation is confusing
(8 answers)
When is method hiding practically used?
(5 answers)
Closed 4 years ago.
The question has been put up to discuss around the term hiding which is associated with static methods in java.
Whenever a static method with the same signature is defined in the parent and the child class, the child class method is said to have hidden the method in the parent class. My question is around the usage of hiding, as we know static methods would be accessed by the class name or if we try to create a reference (which is a bad practice), method would be called based on the reference type. So how does hiding comes into picture, take example of below code:
public class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public static void main(String[] args) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod(); // prints The static method in Animal
}
}
Can someone please explain how child method has hidden the parent method here? (parent method is being called using parent reference, so how does hiding comes into picture)
This demonstrates exactly why it's a bad practice to intend to hide a static method. The method is chosen statically, at compile time, without any consideration for the actual instance type.
I suppose the take home phrase should be "hiding is not overriding... and don't do it", to mean that Java doesn't even look at the object/instance (only looking at the declared type):
Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod();
myAnimal.testClassMethod(); //bad to do, but you can see it uses the declared "Animal" class
Cat.testClassMethod(); //Uses method in Cat
myCat.testClassMethod(); //Uses method in Cat
((Animal) null).testClassMethod(); //Uses Animal method, doesn't look at instance
((Cat) null).testClassMethod(); //Uses Cat method, doesn't look at instance
Now the interesting part: what if you remove the method from Cat?
All of these invocations will still work, using the Animal method, which means that:
It's a very bad practice to hide static methods
It's equally bad to use class instances to invoke static methods, because in the case of hidden methods, it's easy to be misled...
To answer the question: hiding is illustrated with Cat.testClassMethod() or myCat.testClassMethod(), which invokes the static method based on the declared type. When there's no testClassMethod() in Cat, Java calls the parent's.
How has the child method hidden the parent method here?
As you said, by defining a static method with the identical signature.
How does hiding comes into the picture?
Cat.testClassMethod();
Some may expect the invocation of the parent method here (by analogy with polymorphism). But the idea of hiding by class methods is different:
If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible (§6.6) to code in C.
...
A hidden method can be accessed by using a qualified name or by using a method invocation expression (§15.12) that contains the keyword super or a cast to a superclass type.
...
class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name()); // Goodnight, Dick
}
}
JLS 10 - 8.4.8.2. Hiding (by Class Methods)
This example is perfect to show the difference between overriding and hiding. At the same time, it is a bad practice demonstration - calling static methods on an instance.
I will try to make it clear by providing another example.
Since public static methods are inherited, the following snippet
class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
}
class Cat extends Animal {
public static void main(String[] args) {
// Cat: I don't have own method, probably I inherited it from the parent.
// O, yes. I can call it.
Cat.testClassMethod();
}
}
prints
The static method in Animal
Now we're going to add testClassMethod to Cat.
class Animal {
public static void testClassMethod() {
System.out.println("The static method in Animal");
}
}
class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The static method in Cat");
}
public static void main(String[] args) {
// Cat: I have two version of the method: mine and the inherited one.
// The caller specified my class name, so he wanted my version.
// I will call my method hiding the parent method.
Cat.testClassMethod();
// If he wanted Animal's version, he would write
Animal.testClassMethod();
// or (DON'T DO THIS)
((Animal)new Cat()).testClassMethod();
}
}
All final, static and private methods and variables use static binding and are bonded by compiler. Static binding uses Type information for binding (in this case Animal).
So, in your case myAnimal.testClassMethod(); will print the static method in Animal because declared type is Animal.
static methods don't take part in dynamic binding (polymorphism).
static methods aren't overritten, static methods are hidden by the subclass.
someone who calls Cat.testClassMethod() might expect the behaviour of Animal.testClassMethod()
So Cat.testClassMethod() is hiding Animal.testClassMethod()
Javadoc says:
the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass.
doesn't ring a bell to me. Any clear example showing the meaning of this will be highly appreciated.
public class Animal {
public static void foo() {
System.out.println("Animal");
}
}
public class Cat extends Animal {
public static void foo() { // hides Animal.foo()
System.out.println("Cat");
}
}
Here, Cat.foo() is said to hide Animal.foo(). Hiding does not work like overriding, because static methods are not polymorphic. So the following will happen:
Animal.foo(); // prints Animal
Cat.foo(); // prints Cat
Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;
a.foo(); // should not be done. Prints Animal because the declared type of a is Animal
b.foo(); // should not be done. Prints Animal because the declared type of b is Animal
c.foo(); // should not be done. Prints Cat because the declared type of c is Cat
d.foo(); // should not be done. Prints Animal because the declared type of d is Animal
Calling static methods on instances rather than classes is a very bad practice, and should never be done.
Compare this with instance methods, which are polymorphic and are thus overridden. The method called depends on the concrete, runtime type of the object:
public class Animal {
public void foo() {
System.out.println("Animal");
}
}
public class Cat extends Animal {
public void foo() { // overrides Animal.foo()
System.out.println("Cat");
}
}
Then the following will happen:
Animal a = new Animal();
Animal b = new Cat();
Cat c = new Cat();
Animal d = null;
a.foo(); // prints Animal
b.foo(); // prints Cat
c.foo(); // prints Cat
d.foo(): // throws NullPointerException
First of all What is meant by method Hiding?
Method hiding means subclass has defined a class method with the same signature as a class method in the superclass. In that case the method of superclass is hidden by the subclass. It signifies that : The version of a method that is executed will NOT be determined by the object that is used to invoke it. In fact it will be determined by the type of reference variable used to invoke the method.
What is meant by method overriding?
Method overriding means subclass had defined an instance method with the same signature and return type( including covariant type) as the instance method in superclass. In that case method of superclass is overridden(replaced) by the subclass. It signifies that: The version of method that is executed will be determined by the object that is used to invoke it. It will not be determined by the type of reference variable used to invoke the method.
Why can't static methods be overridden?
Because, static methods are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object.
How should static methods be accessed?
Static methods should be accessed in static way. i.e. by the name of class itself rather than using an instance.
Here is the short Demo for method overriding and hiding:
class Super
{
public static void foo(){System.out.println("I am foo in Super");}
public void bar(){System.out.println("I am bar in Super");}
}
class Child extends Super
{
public static void foo(){System.out.println("I am foo in Child");}//Hiding
public void bar(){System.out.println("I am bar in Child");}//Overriding
public static void main(String[] args)
{
Super sup = new Child();//Child object is reference by the variable of type Super
Child child = new Child();//Child object is referenced by the variable of type Child
sup.foo();//It will call the method of Super.
child.foo();//It will call the method of Child.
sup.bar();//It will call the method of Child.
child.bar();//It will call the method of Child again.
}
}
Output is
I am foo in Super
I am foo in Child
I am bar in Child
I am bar in Child
Clearly, as specified, since foo is the class method so the version of foo invoked will be determined by the type of reference variable (i.e Super or Child) referencing the object of Child. If it is referenced by Super variable then foo of Super is called. And if it is referenced by Child variable then foo of Child is called.
Whereas,
Since bar is the instance method so the version of bar invoked is solely determined by the object(i.e Child) that is used to invoke it. No matter via which reference variable (Super or Child) it is called , the method which is going to be called is always of Child.
To overwrite a method means that whenever the method is called on an object of the derived class, the new implementation will be called.
To hide a method means that an unqualified call to that name in the scope of this class (i.e. in the body of any of its methods, or when qualified with the name of this class) will now call a completely different function, requiring a qualification to access the static method of the same name from the parent class.
More description Java Inheritance: Overwritten or hidden methods
If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass.
Hidden methods are in Static context, I believe. Static methods are not overridden, per se, because the resolution of method calls done by the compiler at the compile time itself. So, if you define a static method in the base class with the same signature as that one present in the parent class, then the method in the subclass hides the method inherited from super class.
class Foo {
public static void method() {
System.out.println("in Foo");
}
}
class Bar extends Foo {
public static void method() {
System.out.println("in Bar");
}
}
For example you can override instance methods in a super class but not static.
Hiding is Parent class has a static method named Foo and the sub class also has a static method called Foo.
Another scenario is the parent has a static method named Cat and the sub class has an instance method named Cat. (static and instance with the same signature can't intermix).
public class Animal {
public static String getCat() { return "Cat"; }
public boolean isAnimal() { return true; }
}
public class Dog extends Animal {
// Method hiding
public static String getCat() { }
// Not method hiding
#Override
public boolean isAnimal() { return false; }
}
class P
{
public static void m1()
{
System.out.println("Parent");
}
}
class C extends P
{
public static void m1()
{
System.out.println("Child");
}
}
class Test{
public static void main(String args[])
{
Parent p=new Parent();//Parent
Child c=new Child(); //Child
Parent p=new Child(); //Parent
}
}
If the both parent and child class method are static the compiler is responsible for method resolution based on reference type
class Parent
{
public void m1()
{
System.out.println("Parent");
}}
class Child extends Parent
{
public void m1()
{
System.out.println("Child")
}
}
class Test
{
public static void main(String args[])
{
Parent p=new Parent(); //Parent
Child c=new Child(); //Child
Parent p=new Child(); //Child
}
}
If both method are not static jvm is responsible for method resolution based on run time object
When super/parent class and sub/child class contains same static method including same parameters and signature. The method in the super class will be hidden by the method in sub class. This is known as method hiding.
Example:1
class Demo{
public static void staticMethod() {
System.out.println("super class - staticMethod");
}
}
public class Sample extends Demo {
public static void main(String args[] ) {
Sample.staticMethod(); // super class - staticMethod
}
}
Example:2 - Method Hiding
class Demo{
public static void staticMethod() {
System.out.println("super class - staticMethod");
}
}
public class Sample extends Demo {
public static void staticMethod() {
System.out.println("sub class - staticMethod");
}
public static void main(String args[] ) {
Sample.staticMethod(); // sub class - staticMethod
}
}
first of all always class a static method using class name.
if function is static then it is method hiding whereas function is non static then method is overriding.
I'm confused on how overriding differs from hiding in Java. Can anyone provide more details on how these differ? I read the Java Tutorial but the sample code still left me confused.
To be more clear, I understand overriding well. My issue is that I don't see how hiding is any different, except for the fact that one is at the instance level while the other is at the class level.
Looking at the Java tutorial code:
public class Animal {
public static void testClassMethod() {
System.out.println("Class" + " method in Animal.");
}
public void testInstanceMethod() {
System.out.println("Instance " + " method in Animal.");
}
}
Then we have a subclass Cat:
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class 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();
}
}
Then they say:
The output from this program is as follows:
Class method in Animal.
The instance method in Cat.
To me, the fact that calling a class method testClassMethod() directly from the Animal class executes the method in Animal class is pretty obvious, nothing special there. Then they call the testInstanceMethod() from a reference to myCat, so again pretty obvious that the method executed then is the one in the instance of Cat.
From what I see, the call hiding behaves just like overriding, so why make that distinction? If I run this code using the classes above:
Cat.testClassMethod();
I'll get:
The class method in Cat.
But if I remove the testClassMethod() from Cat, then I'll get:
The class method in Animal.
Which shows me that writing a static method, with the same signature as in the parent, in a subclass pretty much does an override.
Hopefully I'm making clear my where I'm confused and someone can shed some light. Thanks very much in advance!
Overriding basically supports late binding. Therefore, it's decided at run time which method will be called. It is for non-static methods.
Hiding is for all other members (static methods, instance members, static members). It is based on the early binding. More clearly, the method or member to be called or used is decided during compile time.
In your example, the first call, Animal.testClassMethod() is a call to a static method, hence it is pretty sure which method is going to be called.
In the second call, myAnimal.testInstanceMethod(), you call a non-static method. This is what you call run-time polymorphism. It is not decided until run time which method is to be called.
For further clarification, read Overriding Vs Hiding.
Static methods are hidden, non-static methods are overriden.
The difference is notable when calls are not qualified "something()" vs "this.something()".
I can't really seem to put it down on words, so here goes an example:
public class Animal {
public static void something() {
System.out.println("animal.something");
}
public void eat() {
System.out.println("animal.eat");
}
public Animal() {
// This will always call Animal.something(), since it can't be overriden, because it is static.
something();
// This will call the eat() defined in overriding classes.
eat();
}
}
public class Dog extends Animal {
public static void something() {
// This method merely hides Animal.something(), making it uncallable, but does not override it, or alter calls to it in any way.
System.out.println("dog.something");
}
public void eat() {
// This method overrides eat(), and will affect calls to eat()
System.out.println("dog.eat");
}
public Dog() {
super();
}
public static void main(String[] args) {
new Dog();
}
}
OUTPUT:
animal.something
dog.eat
This is the difference between overrides and hiding,
If both method in parent class and child class are an instance method, it called overrides.
If both method in parent class and child class are static method, it called hiding.
One method cant be static in parent and as an instance in the child. and visa versa.
If I understand your question properly then the answer is "you already are overriding".
"Which shows me that writing a static method, with the same name as in the parent, in a subclass pretty much does an override."
If you write a method in a subclass with exactly the same name as a method in a superclass it will override the superclass's method. The #Override annotation is not required to override a method. It does however make your code more readable and forces the compiler to check that you are actually overriding a method (and didn't misspell the subclass method for example).
Overriding happens only with instance methods.
When the type of the reference variable is Animal and the object is Cat then the instance method is called from Cat (this is overriding). For the same acat object the class method of Animal is used.
public static void main(String[] args) {
Animal acat = new Cat();
acat.testInstanceMethod();
acat.testClassMethod();
}
Output is:
The instance method in Cat.
Class method in Animal.
public class First {
public void Overriding(int i) { /* will be overridden in class Second */ }
public static void Hiding(int i) { /* will be hidden in class Second
because it's static */ }
}
public class Second extends First {
public void Overriding(int i) { /* overridden here */ }
public static void Hiding(int i) { /* hides method in class First
because it's static */ }
}
The rule for memorizing is simple: a method in an extending class
can't change static to void and
can't change void to static.
It will cause of compile-error.
But if void Name is changed to void Name it's Overriding.
And if static Name is changed to static Name it's Hiding. (Both the static method of the subclass as well as the one of the superclass can be called, depending on the type of the reference used to call the method.)
In this code snippet I use 'private' access modifier instead of 'static' to show you difference between hiding methods and overriding methods.
class Animal {
// Use 'static' or 'private' access modifiers to see how method hiding work.
private void testInstancePrivateMethod(String source) {
System.out.println("\tAnimal: instance Private method calling from "+source);
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Private method.");
testInstancePrivateMethod( Animal.class.getSimpleName() );
}
// Use default, 'protected' or 'public' access modifiers to see how method overriding work.
protected void testInstanceProtectedMethod(String source) {
System.out.println("\tAnimal: instance Protected method calling from "+source);
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("\tAnimal: instance Public method with using of Protected method.");
testInstanceProtectedMethod( Animal.class.getSimpleName() );
}
}
public class Cat extends Animal {
private void testInstancePrivateMethod(String source) {
System.out.println("Cat: instance Private method calling from " + source );
}
public void testInstanceMethodUsingPrivateMethodInside() {
System.out.println("Cat: instance Public method with using of Private method.");
testInstancePrivateMethod( Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingPrivateMethodInside();
}
protected void testInstanceProtectedMethod(String source) {
System.out.println("Cat: instance Protected method calling from "+ source );
}
public void testInstanceMethodUsingProtectedMethodInside() {
System.out.println("Cat: instance Public method with using of Protected method.");
testInstanceProtectedMethod(Cat.class.getSimpleName());
System.out.println("Cat: and calling parent after:");
super.testInstanceMethodUsingProtectedMethodInside();
}
public static void main(String[] args) {
Cat myCat = new Cat();
System.out.println("----- Method hiding -------");
myCat.testInstanceMethodUsingPrivateMethodInside();
System.out.println("\n----- Method overriding -------");
myCat.testInstanceMethodUsingProtectedMethodInside();
}
}
Output:
----- Method hiding -------
Cat: instance Public method with using of Private method.
Cat: instance Private method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Private method.
Animal: instance Private method calling from Animal
----- Method overriding -------
Cat: instance Public method with using of Protected method.
Cat: instance Protected method calling from Cat
Cat: and calling parent after:
Animal: instance Public method with using of Protected method.
Cat: instance Protected method calling from Animal
I think this is not yet fully explained.
Please see the following example.
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) {
Animal myCat = new Cat();
Cat myCat2 = new Cat();
myCat.testClassMethod();
myCat2.testClassMethod();
myCat.testInstanceMethod();
myCat2.testInstanceMethod();
}
}
The output will be as follows.
The static method in Animal
The static method in Cat
The instance method in Cat
The instance method in Cat
Based on my recent Java studies
method overriding, when the subclass have the same method with the same signature in the subclass.
Method hiding, when the subclass have the same method name, but different parameter. In this case, you're not overriding the parent method, but hiding it.
Example from OCP Java 7 book, page 70-71:
public class Point {
private int xPos, yPos;
public Point(int x, int y) {
xPos = x;
yPos = y;
}
public boolean equals(Point other){
.... sexy code here ......
}
public static void main(String []args) {
Point p1 = new Point(10, 20);
Point p2 = new Point(50, 100);
Point p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//point's class equals method get invoked
}
}
but if we write the following main:
public static void main(String []args) {
Object p1 = new Point(10, 20);
Object p2 = new Point(50, 100);
Object p3 = new Point(10, 20);
System.out.println("p1 equals p2 is " + p1.equals(p2));
System.out.println("p1 equals p3 is " + p1.equals(p3));
//Object's class equals method get invoked
}
In the second main, we using the Object class as static type, so when we calling the equal method in Point object, it's waiting a Point class to arrive as a parameter,but Object coming. So the Object class equals method getting run, because we have an equals(Object o) there. In this case, the Point's class equals dosen't overrides, but hides the Object class equals method.
public class Parent {
public static void show(){
System.out.println("Parent");
}
}
public class Child extends Parent{
public static void show(){
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Parent parent=new Child();
parent.show(); // it will call parent show method
}
}
// We can call static method by reference ( as shown above) or by using class name (Parent.show())
The linked java tutorial page explains the concept of overriding and hiding
An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
The distinction between hiding a static method and overriding an instance method has important implications:
The version of the overridden instance method that gets invoked is the one in the subclass.
The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
Coming back to your example:
Animal myAnimal = myCat;
/* invokes static method on Animal, expected. */
Animal.testClassMethod();
/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();
Above statement does not show hiding yet.
Now change the code as below to get different output:
Animal myAnimal = myCat;
/* Even though myAnimal is Cat, Animal class method is invoked instead of Cat method*/
myAnimal.testClassMethod();
/* invokes child class instance method (non-static - it's overriding) */
myAnimal.testInstanceMethod();
In addition to the examples listed above, here is a small sample code to clarify the distinction between hiding and overriding:
public class Parent {
// to be hidden (static)
public static String toBeHidden() {
return "Parent";
}
// to be overridden (non-static)
public String toBeOverridden() {
return "Parent";
}
public void printParent() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}
public class Child extends Parent {
public static String toBeHidden() {
return "Child";
}
public String toBeOverridden() {
return "Child";
}
public void printChild() {
System.out.println("to be hidden: " + toBeHidden());
System.out.println("to be overridden: " + toBeOverridden());
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.printParent();
child.printChild();
}
}
The call of child.printParent() outputs:
to be hidden: Parent
to be overridden: Child
The call of child.printChild() outputs:
to be hidden: Child
to be overridden: Child
As wee can see from the outputs above (especially the bold marked outputs), method hiding behaves differently from overriding.
Java allows both hiding and overriding only for methods. The same rule does not apply to variables. Overriding variables is not permitted, so variables can only be hidden (no difference between static or non-static variable). The example below shows how the method getName() is overriden and the variable name is hidden:
public class Main {
public static void main(String[] args) {
Parent p = new Child();
System.out.println(p.name); // prints Parent (since hiding)
System.out.println(p.getName()); // prints Child (since overriding)
}
}
class Parent {
String name = "Parent";
String getName() {
return name;
}
}
class Child extends Parent {
String name = "Child";
String getName() {
return name;
}
}
At runtime the child version of an overridden method is always executed for an instance
regardless of whether the method call is defi ned in a parent or child class method. In this
manner, the parent method is never used unless an explicit call to the parent method is
referenced, using the syntax
ParentClassName.method().
Alternatively, at runtime the parent
version of a hidden method is always executed if the call to the method is defined in the
parent class.
In method overriding, method resolution is done by the JVM on the basis of runtime object. Whereas in method hiding, method resolution is done by the compiler on the basis of reference.
Thus,
If the code would have been written as,
public static void main(String[] args) {
Animal myCat = new Cat();
myCat.testClassMethod();
}
The Output would be as below:
Class method in Animal.
It is called hiding because the compiler hides the super class method implementation, when subclass has the same static method.
Compiler has no restricted visibility for overridden methods and it’s only during runtime that it’s decided which one is used.
This is the difference between overriding and hiding:
Animal a = new Cat();
a.testClassMethod() will call the method in parent class since it is an example of method hiding. The method to be called is determined by the type of the reference variable and decided at compile time.
a.testInstanceMethod() will call the method in child class since it is an example of method overriding. The method to be called is determined by the object which is used to call the method at runtime.
How is static method hiding happening in java?
Cat class is extending Animal class. So in Cat class will have both static methods (i mean Child class's static method and Parent class's static method)
But how JVM hiding Parent static method? How it's dealing in Heap and Stack?
Please explain the concept of static and dynamic binding in Java.
What I have grasped is that Static binding in Java occurs during compile time while dynamic binding occurs during Runtime, static binding uses Type (Class in Java) information for binding while dynamic binding uses Object to resolve binding.
This is the code for my understanding.
public class StaticBindingTest {
public static void main (String args[]) {
Collection c = new HashSet ();
StaticBindingTest et = new StaticBindingTest();
et.sort (c);
}
//overloaded method takes Collection argument
public Collection sort(Collection c) {
System.out.println ("Inside Collection sort method");
return c;
}
//another overloaded method which takes HashSet argument which is sub class
public Collection sort (HashSet hs){
System.out.println ("Inside HashSet sort method");
return hs;
}
}
and the output of the above program was inside the collection sort method
for dynamic binding...
public class DynamicBindingTest {
public static void main(String args[]) {
Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
vehicle.start(); //Car's start called because start() is overridden method
}
}
class Vehicle {
public void start() {
System.out.println("Inside start method of Vehicle");
}
}
class Car extends Vehicle {
#Override
public void start() {
System.out.println ("Inside start method of Car");
}
}
the output was inside the start method of Car. Please advise: Is this understanding correct and please advise some more examples. Thanks.
Static binding is used at compile time and is usually common with overloaded methods - the sort() methods in your example where the type of the argument(s) is used at compile time to resolve the method.
Dynamic binding (dynamic dispatch) is usually associated with polymorphism and overriding methods - the start() method in your example where the type of the receiver (vehicle) is used at runtime to resolve the method.
I think you have summarized it correctly and shams also correctly added more information for you. Just to add little more information for you first let me step back by stating that the association of method definition to the method call is known as binding. So, static binding as you pointed out correctly, is the binding that can be resolved at compile time by compiler (also known as early binding or static binding). On the other hand, dynamic bidding or late binding it means compiler is not able to resolve the call/binding at compile time (it happens at the run time). See below for some examples:
//static binding example
class Human{
....
}
class Boy extends Human{
public void walk(){
System.out.println("Boy walks");
}
public static void main( String args[]) {
Boy obj1 = new Boy();
obj1.walk();
}
}
//Overriding is a perfect example of Dynamic binding
package beginnersbook.com;
class Human{
public void walk()
{
System.out.println("Human walks");
}
}
class Boy extends Human{
public void walk(){
System.out.println("Boy walks");
}
public static void main( String args[]) {
//Reference is of parent class
Human myobj = new Boy();
myobj.walk();
}
}
source