Friday, January 25, 2013

Inheritance and Polymorphism


A class can re-use all the codes and functionalities of another class by means of extension. The class being extended is typically called the parent class 4 and the extending class is called a child class. Class extension grants the child class all the inheritable methods and variables of the super class 5. The instant functionality gained from class extension impacts the productivity of the programmer in a positive way. If the abstractions are done carefully and rationally, it becomes possible to write a piece of code that can be used in other applications.
import static java.lang.System.out;

class Telephone {

    String phonenumber;

    void init() {
        out.println("connecting to carrier via land line");
    }

    void dialNumber(String args) {
        out.println("Dialling " + args);
    }

    void answerCall() {
        out.println("Answering call...");
    }   

}

class MobilePhone extends Telephone {

}

class TelephoneTest {

    public static void main(String[] args) {

        MobilePhone mp = new MobilePhone();

        mp.init();
        mp.dialNumber("6327775566");
        mp.answerCall();

    }
}

extends keyword

In the sample code, the MobilePhone type was created by defining a class. The class block does not contain any definition and yet we were able to call the initdialNumber and answerCall methods against it---these methods, though not explicitly defined inside the MobilePhone class has been inherited from the Telephone class. The extends keyword is used to denote that one class is inheriting from another class.

Call me and you will get the knowledge and skills you need instantly.
For inquiries contact me on my mobile number or email me at inquiry@eglobiotraining.com. View my multiply site http://erwinglobio.multiply.com
 
 
Prof. Erwin M. Globio, MSIT
Senior IT Lecturer
Far Eastern University
Email Address: inquiry@eglobiotraining.com
Skype: erwinglobio
 
 
Call Now:
 
SMART: 09393741359
SUN: 09323956678



No comments:

Post a Comment