-
Clarification regarding Java Interface and Inheritance
Java Specification says that we cannot create an object for an Interface but in the init (ServletConfig config) Syntax we are passing an object of ServleConfig which is an interface. Similarly in the service method we pass ServletRequest, ServletResponse objects.
Please clarify Java says that Multiple inheritance is not possible but every class extends Object class and we can extend a class explictly. It means that a class is extending 2 classes please clarify
Question asked by visitor suman
-
Junior Member
Re: Clarification regarding Java Interface and Inheritance
Hi Suman,
We cant create an object to Interface but we can assign an object reference.
For example:
A -> Interface
B -> Class implements A
We can set a reference to Interface object likethis:
A newObject = new B();
In init method we are passing ServeltConfig object reference not creating new object. The object will be created by webserver which contains Implementation classes of ServeltConfig Interface. Same case for service() also.
Java doesn't support multiple inheritance for Classes. Every class extends java.lang.Object class if it doesn't extend any other class. Try the following example you can know the difference.
public class A { }
public class B extends A { }
compile those two classes.
run the following commands:
javap A
javap B
check the output and find the difference
Regards,
Eswar
-
Junior Member
Re: Clarification regarding Java Interface and Inheritance
1. Although we can't create object of interfaces but can create reference of it which we are sending.
2. what u r considering is not multiple inheritence but multi level inheritence
in multiple inher.. two classes/ entites are simultaneously extending by third one.
-
Junior Member
Re: Clarification regarding Java Interface and Inheritance
1. Although we can't create object of interfaces but can create reference of it which we are sending.
2. what u r considering is not multiple inheritence but multi level inheritence
in multiple inher.. two classes/ entites are simultaneously extending by third one.
-
Junior Member
Re: Clarification regarding Java Interface and Inheritance
Hi,
for your first doubt, since u r passing the arguments (ServletRequest request),
the object is not created.it'll be created only when ServletRequest request=new ServletRequest();
for your second doubt,
if a class extends a class, then it won't extend the Object class directly,
it will extend through that super class only
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules