java.lang.Object is the root class in Java
Class extension is not an optional activity in Java programming. A Java class needs to extend at least and at most one other class. All newly defined classes, by default, extends the java.lang.Object class, unless otherwise overriden by an explicit extends declaration. In Listing 4.5, class Telephone is not explicitly extending any class, hence it is a child class of Object--it is as if we had written;
class Telephone extends Object {
}
class MobilePhone extends Telephone {
}
MobilePhone does not extend Object because we declared explicitly that it extends Telephone. You will realize soon enough that every class in Java descends either directly or indirectly from .
Single rooted class inheritance
I would not advise that you explicitly declare all your classes to extend from Object. Firstly because Java will do that for you implicitly and secondly because you can only extend one class. There is no way to achieve multiple class inheritance. Java follows a single-rooted class inheritance, once you have extended a class, you cannot inherit from another one. That is not to say multiple inheritance cannot be achieved in Java, it can be, using interfaces.
interfaces
Creating a class is one of three ways to define a Type. The second way is to create an interface. An interface is the purest form of a type because it only contains the protocols but not the implementing details of the object. The class on the otherhand contains both the protocol (the Type) and the implementing details (body of the functions).
interface Phone {
void answerCall();
void dialNumber(String args);
}
An interface is declared using the interface keyword followed by a name 6. The opening and closing curly braces comprises the body of the interface.
Methods are variables can be declared in an interface, but they are not the kind of variables and methods that you would declare insie the class. Interfaces can only contain abstract methods 7, which makes sense because interfaces are supposed to contain only the Type information and not the implementation detail. An abstract method declares only the method signature 8 and has a semi-colon immediately right after, in place of the usual method body. If you declare variables inside the interfaces, they will be automatically final, static and public 9.
//PhoneToo.java
interface IPhone {
void answerCall();
void dialNumber(String args);
}
class Phone implements IPhone {
public void answerCall() {
System.out.println("Answering call");
}
public void dialNumber(String args){
System.out.println("Calling " + args);
}
public static void main(String[] args) {
Phone p = new Phone();
p.dialNumber("632444888");
p.answerCall();
}
}
Interfaces are not meant to be instantiated, they are meant for type extensions (inheritance). To use interfaces, they must be implemented by a class. The code sample of listing 4.7 shows a Phone class inheriting the type IPhone using the implements keyword.
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