2011-12-16

Need of method overriding in java ?

If a super class method logic is not fulfilling sub class business requirements, sub class should override that method with required business logic. Usually in super class, methods are defined with generic logic which is common for all sub classes.
For instance if we take Shape type of classes all Shape classes like Rectangle,Square,Circle etc...should have methods area() and perimeter(). Usually these methods are defined in super class Shape with generic logic but in sub classes Rectangle,Square,Circle etc...these methods must have logic based on their specific logic. Hence in Shape sub classes, these two methods must be overridden with sub class specific logic.

2011-12-14

Java method overriding rules?

A method is said to be overriding method if it satisfies the below rules.

1.return type should be same as super class method.

In the below program we should not place return type as int we will get a compile time error, in order to override that method we should place the below method return type as void.

2011-12-11

What is method overriding in java ?

Redefining super class method in sub class is called method overriding. Method overriding, in object oriented programming, is a language feature that allows a sub class to provide a specific implementation of a method that is already provided by one of its super classes. The implementation in the sub class overrides (replaces) the implementation in the super class. If a method in sub class contains signature same as super class non private method, sub class method is treated as overriding method and super class method is treated as overridden method.

Can main method is executed from super classes ?

Yes,if subclass doesn't have main method JVM will not through exception directly instead it search in super class complete hierarchy till object class. If main method is found in any of the super class it stops searching and executes main method from that class. Otherwise it throws an exception.Check below program.

Differences between this and super keyword in java

The following are the similarities and differences between this and super keyword. One thing keep in mind that we cannot print 'super' like 'this' why because super class non-static members memory doesn't have hashcode. And also keep in mind that super method is different from super keyword. super method is used to call super class constructor where as super keyword is used to access super class members.

2011-12-10

Use of super keyword in java ?

The super is a java keyword,used to access the members of the super class.Use of keyword super is to access the hidden data variables of the super class hidden by subclass.
Ex:Suppose Example class is the super class in the below program that has two instance variables as int x and int y. Sample class is the subclass that also contains its own data members named x and y,then we can access the super class (Example class) variables x and y inside the subclass (Sample class) just by calling the following command.
super.member;
Here member can either be an instance variable or a method. super keyword most useful to handle situations where the local members of the subclass hide the members of the super class having the same name.

Loading

Enter your Email here: