In: Computer Science
data structure class:
a. syntax for generics
b. comparable interface
c.how do you implement interface
answer for all of them please.
answer for all of them please
`Hey,
Note: If you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
1)
Like C++, we use <> to specify parameter types in generic
class creation. To create objects of generic class, we use
following syntax.
// To create an instance of generic class BaseType <Type> obj = new BaseType <Type>() Note: In Parameter type we can not use primitives like 'int','char' or 'double'.
2)
Comparable interface is mainly used to sort the
arrays (or lists) of custom objects.
Lists (and arrays) of objects that implement Comparable interface
can be sorted automatically by Collections.sort (and
Arrays.sort).
For example
public int compareTo(Author au){ /* * Sorting by last name. compareTo should return < 0 if this(keyword) * is supposed to be less than au, > 0 if this is supposed to be * greater than object au and 0 if they are supposed to be equal. */ int last = this.lastName.compareTo(au.lastName); //Sorting by first name if last name is same d return last == 0 ? this.firstName.compareTo(au.firstName) : last; } }
3)
import java.io.*;
// A simple interface
interface In1
{
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class TestClass implements In1
{
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}
// Driver Code
public static void main (String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Kindly revert for any queries
Thanks.