Creating New class, Extending class and Implementing interface

Discuss the differences between creating a new class, extending a class and implementing an interface and when each would be appropriate.

Answer:

*Creating a new class is simply creating a class with no extensions and no implementations. The signature is as follows

public class MyClass()

{

}
 

*Extending a class is when you want to use the functionality of another class or classes. The extended class inherits all of the functionality of the previous class. 

An example of this when you create your own applet class and extend from

java.applet.Applet. This gives you all of the functionality of the java.applet.Applet class.

The signature would look like this

public class MyClass extends MyBaseClass

{
}
 

*Implementing an interface simply forces you to use the methods of the interface implemented. This gives you two advantages. This forces you to follow a standard (forces you to use certain methods) and in doing so gives you a channel for polymorphism. 

This isn’t the only way you can do polymorphism but this is one of the ways.

public class Fish implements Animal
{

}
 

Name four methods every Java class will have. 

public String toString();
public Object clone(); 
public boolean equals(); 
public int hashCode();

Java Tips

See also
Strong and Weak Reference

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.