Which concept (Interface or Inheritance) is more powerful in java?
-------------
suresh
Which concept (Interface or Inheritance) is more powerful in java?
-------------
suresh
hi
I think both are equally imp in java!Still i think may be inheritance simply coz it allows code reusability.interfaces used for multiple inheriance only,so i think
inheritance is powerful.
bye
:)
NEVER SAY DIE.
Interface no doubt!
'both are imporatant no doubt abt it...i am going to explain it clearly tomorrow..becaoz...there is need to refer abt it clearly'
Both the concepts are having its own important,But compare to Inheritance Interface having more powerful....
Inheritence is a time tested, 'globally' accepted feature. So inheritence is a very, very powerfule feature.
Interfaces are powerful only because inheritence is powerful.
Interfaces combine two very important notions in programming.
Inheritence + abstraction = interface
The abstractness part allows us to 'program to interfaces' and is used in atleast a couple of design patterns (I know only two p[atterns ). This provides a common interface making the code extensible and maintanable at the same time.
And you know what inheritence brings to the package.
I still can't decide which is more powerful
Inheritance -->enforces reusability in the design phase (closed to traffic at runtime JRE ).
Interface --> enforces and defines an open standard for any future framework to be build upon. (open to traffic at runtime JRE )
Both are equally important in java..
As the famous GoF book says:
Prefer interfaces over inheritance.
Use interfaces as a design-time resource when you are planning your system. Inheritance should be used almos always to reuse code. If your system is designed based on interfaces, it will be easier to maintain and to evolve. Of course this is not a fixed rule, but it works most of the time.
--------------
Neelima
encapsulation,polymophism and interfaces.
another main concepts:
reusability.
portability.
can we read a xml file through the core java application?if possible provide one example also?
We can read XML files using DOM Model -- as follows:
<?xml version="1.0"?>
<company>
<employee>
<firstname>Tom</firstname>
<lastname>Cruise</lastname>
</employee>
<employee>
<firstname>Paul</firstname>
<lastname>Enderson</lastname>
</employee>
<employee>
<firstname>George</firstname>
<lastname>Bush</lastname>
</employee>
</company>
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLReader {
public static void main(String argv[]) {
try {
File file = new File("c:\\MyXMLFile.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);//This can take a string variable also
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("employee");
System.out.println("Information of all employees");
for (int s = 0; s < nodeLst.getLength(); s++) {
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue());
NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
NodeList lstNm = lstNmElmnt.getChildNodes();
System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Do try this code just by sending the string [thats the contents of the XML-file].
Inheritance is the major force behind any object oriented language.
First inheritance concept comes in picture then any other thing(like interface) which need to be inherited.
So powerful in Inheritence offcourse!!!
interface has to be implemented("extends" in other case), sort of inheritence. So the question is absurd.
interface is useful
inheritance is not useful to inherit from many classes..ie multiple inheritance..
in that situation only inteface came..so interface can do more than inheritance..so interface is best..It all depends ,where u r using it..
One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class; multiple inheritance of classes is not allowed. However, a Java class/interface may implement/extend any number of interfaces.
you know u cannot achieve multiple inheritence widot interfaces.,this is one of the limitations of the Java language.so according 2 me interfaces r more powerful,but we can also say dat both r relatively connected 2 each other....k nw cya if u gt ny rite option den do inform us.gud day.
my point of view inheritance is more pwerful because interface are given to implement multilevel or hirechical inheritance,which is not supported by java classes
Well guys
Without inheritance,interface has least meaningful existence but when both exist Interface is lovely to play with. [ To my understanding the question lays the scenario that both exist,only then we can compare and speak about the power between the two ]
So my vote goes to interface
By
Venky