Geeks Talk

Prepare for your Next Interview




Clarification regarding Java Interface and Inheritance

This is a discussion on Clarification regarding Java Interface and Inheritance within the JSP forums, part of the Web Development category; 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. ...


Go Back   Geeks Talk > Web Development > JSP

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-05-2007
Expert Member
 
Join Date: Feb 2007
Posts: 1,279
Thanks: 0
Thanked 161 Times in 137 Posts
Geek_Guest is on a distinguished roadGeek_Guest is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Mar 2007
Location: Bangalore
Posts: 10
Thanks: 0
Thanked 2 Times in 2 Posts
eswar_ambati is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to eswar_ambati For This Useful Post:
  #3 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Jun 2007
Location: Noida
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
parvesh is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 06-08-2007
Junior Member
 
Join Date: Jun 2007
Location: Noida
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
parvesh is on a distinguished road
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.
Reply With Quote
The Following User Says Thank You to parvesh For This Useful Post:
  #5 (permalink)  
Old 06-15-2007
Junior Member
 
Join Date: Apr 2007
Location: Chennai
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
vinotha is on a distinguished road
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
Reply With Quote
Reply

  Geeks Talk > Web Development > JSP


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Types of Inheritance scott OOPS 13 1 Week Ago 02:45 AM
How to test java project in QTP without java addin Geek_Guest QTP 7 09-18-2007 06:53 AM
multiple inheritance in java... psuresh1982 Java 20 07-23-2007 01:33 PM
inheritance sumad C# 4 06-06-2007 12:36 AM
multiple inheritance.... psuresh1982 Java 2 01-21-2007 11:57 PM


All times are GMT -4. The time now is 04:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved