Skip to main content

Posts

Showing posts from May, 2013

Java - Overriding

In the previous chapter we talked about super classes and sub classes. If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that's specific to the sub class type. Which means a subclass can implement a parent class method based on its requirement. In object oriented terms, overriding means to override the functionality of any existing method. Example: Let us look at an example. class Animal { public void move (){ System . out . println ( "Animals can move" ); } } class Dog extends Animal { public void move (){ System . out . println ( "Dogs can walk and run" ); } } public class TestDog { public static void main ( String args []){ Animal a = new Animal (); // Animal reference and object Animal b = new Dog (); // Animal reference but Dog object a . move (); //...

Java - Overriding

In the previous chapter we talked about super classes and sub classes. If a class inherits a method from its super class, then there is a chance to override the method provided that it is not marked final. The benefit of overriding is: ability to define a behavior that's specific to the sub class type. Which means a subclass can implement a parent class method based on its requirement. In object oriented terms, overriding means to override the functionality of any existing method. Example: Let us look at an example. class Animal { public void move (){ System . out . println ( "Animals can move" ); } } class Dog extends Animal { public void move (){ System . out . println ( "Dogs can walk and run" ); } } public class TestDog { public static void main ( String args []){ Animal a = new Animal (); // Animal reference and object Animal b = new Dog (); // Animal reference but Dog object a . move (); //...

Java - Exceptions Handling

An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following: A user has entered invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications, or the JVM has run out of memory. Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner. To understand how exception handling works in Java, you need to understand the three categories of exceptions: Checked exceptions:  A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation. Runtime exceptions:  A runtime exception is an exception that occurs that probably could have been...