Wednesday, June 3, 2015

Overriding Methods





// A subclass methods Overrides a superclass method if it has the same name and parameter types as a superclass method
// When such a method is applied to a subclass object, the overriding method, and not the orignal method, is executed.

// If a superclass declare a method to be publicly accessible, you cannot override it to be more private.
// Compiler doesnot allow this, because the increased privacy would conflict with polymorphism
// The compiler reports an error if you override a public method and make it private or give it package access

public class OverridingDemo {

public static void main(String[] args) {

Bycycle b = new Bycycle();
b.printInfo();
}
}


class cycle
{
int weight = 20;

public void printInfo()
{
System.out.println("PrintInfo from cycle class: "+weight);
}
}



class Bycycle extends cycle
{
String brand= "hero";

public void printInfo()
{
System.out.println("PrintInfo from subClass Bicycle: "+ brand);
}
}



No comments:

Post a Comment