Generics And Inheritance - Specialized Methods
Mar 6, 2014
I've an interface with generic methods in it. I would like to have specialized methods in the sub types. While doing that I'm seeing the following warnings in eclipse.
class Sorter {
<E> void sort(E[] elements);
};
class StringSorter {
// This gives me a warning 'hiding' to 'sort'
<String> void sort(String[] elements) {
}
// Gives me an error "The method someCrap(String[]) in the type StringSorter is not applicable for the arguments (String[])"
void someCrap(String[] elements) {
}
};
I would like to understand why eclipse gives the above warnings and errors.
View Replies
ADVERTISEMENT
Jul 6, 2014
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
[code]....
View Replies
View Related
Jul 14, 2014
So I'm just a little unclear about this, but how would I call methods from the 'top' of an inheritance chain? I say 'top' because Object is the top... E.g.:
public class AClass {
public void myMethod() { ... }
}
public class BClass extends AClass {
public void myMethod() { ... }
}
public class CClass extends AClass {
public void myMethod() { ... }
}
Assuming that BClass.myMethod() completely overrides AClass.myMethod() (so that there is no call to super.myMethod() in BClass.myMethod()) How can I call AClass.myMethod() from CClass.myMethod()?
View Replies
View Related
Apr 23, 2014
What I have done wrong
public class BankAccount
{
String name;
int accountID;
double balance;
public void setAccount( String username, int ID, Boolean isJoint)
[Code] ....
View Replies
View Related
Mar 1, 2015
Does child class gets a copy of the methods and variables of parent class?
public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();
[Code] ....
why is the output 1?
View Replies
View Related
Feb 10, 2015
In this code, I have to do a series of tasks to change a String using specialized codes that are inputted. The one that I am having trouble with is as follows:
MC-SLXD: Circulates the sub-string in position S with a length of L, rotate the string X characters over in the direction of D. All the arguments (S,L,X and D will be one character in length. The direction will be either L or R for Left or Right.Example: MC-332R/COMPUTER = COPUMTER.
View Replies
View Related
Mar 20, 2015
What is wrong in this
class Gen<k,v>
{
K a;
V b;
Gen(K s, V f) {
a=s;
b=f;
[Code] .....
a is object of type k,then how can I retrieve content of a?
View Replies
View Related
Jan 29, 2014
I have studied that Generics are used to shift the Class Cast Exception into Compile time errors , So that we get errors at compile time error and we do correct them before executing ,but Here is a program in which i am getting Class Cast Exception
class Animal
{
}
class Dog extends Animal
{
}
class Cat extends Animal
[code]..
Getting Exception at line no 29 which i know why it occurs but just wanna ask that isn't it should be caught at compile time According to Generics ?
View Replies
View Related
Jun 19, 2014
I just want to clarify generics for classes.
Referring to [URL] ....
Generics means all methods in class Box will return an Integer if their parameters are defined as T when the instantiation is :
Box<Integer> integerBox = new Box<Integer>();
Are those Boxes only can contain Integers? Can I write
Box <Toys> toybox = new Box <> ();
To show that the Box only contain toys?
View Replies
View Related
Jun 17, 2014
Set<? super TreeMap> s = new HashSet<SortedMap>();
SortedMap<String,String> sm = new TreeMap<String,String>();
TreeMap<String,String> tm = new TreeMap<String,String>();
s.add(sm); //This fails
s.add(tm);
Why does adding sorted map to a Set that allows ? super TreeMap and instantiated as such fail?
View Replies
View Related
Apr 10, 2014
public int[] allIndicesOf(E itemSought) {
ArrayList<Integer> toUse = new ArrayList<>();
for (E anArray : container) {
if (anArray.equals(itemSought)) {
toUse.add(container.indexOf(itemSought));
[Code] ....
I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.
View Replies
View Related
Feb 14, 2014
Suppose I have
class A {
public void speak() {
System.out.println("I am class A");
}
}
class B extends A{
public void speak() {
System.out.println("I am class B");
}
}
class C extends B{
public void speak() {
System.out.println("I am class C");
}
}
Why this doesn't work while A is a super type of B ?
public static void insertElements(List<? super B> list){
list.add(new A()); //not OK, why?
}
View Replies
View Related
Feb 22, 2015
Why java uses the keyword extends when setting the bound of a type parameter(Generic) to an interface. I think using the keyword implements is more intuitive.
public static <T extends Comparable<T>>
why use extends? and not implements.
int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
I know if I want to set multiple bounds I will use extends keyword, and I will concatenate the bounds using & operator.
Is this a design decision to always use extends keyword to set bounds?
View Replies
View Related
Jul 27, 2014
An array of references to a specific generic type is not allowed in Java.
e.g.,
ArrSpec<String> arrs[] = new ArrSpec<String>[10];
is not allowed though the type checkng and memory allocation can be done at the compile time itself.
Instead of this, Java allows to use Wildcard type array of references to a generic type.
e.g.,
ArrSpec<?> arrs[] = new ArrSpec<?>[10];
is allowed though the type checking, memory allocation and any type of values to be stored would be decided at the runtime.
View Replies
View Related
Jun 5, 2015
My book defines this generic method
Java Code:
public static <E extends Comparable<E>> void sort(E[] list... mh_sh_highlight_all('java');
Comparable is an interface and from how i look at this piece of code is that I can only use a class that implements the Comparable interface; however, this is the context my book uses when explaining the following code
First, it specifies that E is a subtype of Comparable.
Second, it specifies that the elements to be compared are of the E type as well.
What does it mean when it says E is a subtype.
View Replies
View Related
Dec 15, 2014
is the Java upcating and downcasting rules are same for general object type or generics types?
1) Dog dog = new Animal();
Type mismatch can't covert Dog to Animal - complie time error
2) Animal animal = new Animal();
Dog dog = (Dog) animal;
java.lang.ClassCastException: Animal cannot be cast to Dog at runtime.
View Replies
View Related
Oct 25, 2014
I'm working with Doubly Linked Lists and using Java Generics..
My nodes looks like this:
class DNode<E> {
DNode<E> previous;
DNode<E> next;
E element;
//and all methods inside
}
My list of Nodes looks like this:
class DLL<E>{
private DNode<E> head;
private DNode<E> tail;
private int size;
[code]....
As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?
public static void main(String[] args) throws IOException {
DLL<Integer> lista=new DLL<Integer>();
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String s = stdin.readLine();
int N = Integer.parseInt(s);
s = stdin.readLine();
String[] pomniza = s.split(" ");
for (int i = 0; i < N; i++) {
lista.instLast(Integer.parseInt(pomniza[i]));
}
}
View Replies
View Related
Aug 27, 2014
i am interested to add integer objects and String objects into any collection object ..... while iterating the collection object i am not interested to do any type cast in java
View Replies
View Related
Aug 29, 2014
I am trying to make a generic method that will replace the data type T with those number types usable with a Scanner object. However, whenever I try to compile, I get errors saying that a Byte/Integer/Double etc are found when only a type T is allowed. This is the beginning of my method. I can;t understand what is wrong with it.
Java Code:
public <T extends Number> T nextRanged(T lowerBound, T upperBound, boolean inclusive, String errorMessage){
// Holds program execution until user inputs a numeric value between the bounds. Prevents all other input without exception.
// Output data type determined by the type of the bounds.
T input = null;
try{
if(input instanceof Byte){
input = new Byte(internalScanner.nextByte());
[Code] ....
The purpose of the method, in the end, will be to provide the nextXXX() functionality of a Scanner object but with built in validation procedures. I could easily do this by making a nextIntRanged(), nextDoubleRanged() etc methods, but this seems wasteful to me.
I meant "incompatible type errors"!
Error example:
ValidatedScanner.java:57: error: incompatible types
input = new Byte(internalScanner.nextByte());
^
required: T
found: Byte
where T is a type-variable:
T extends Number declared in method <T>nextRanged(T,T,boolean,String)
View Replies
View Related
Jun 14, 2014
The erasures of all constituent types of a bound must be pairwise different, or a compile-time error occurs.
Well I know what type erasure is, and I think I kind got what this statement means. My understanding from it is that if your type parameter has more than one bound and those bounds occurs to be the same type after erasure that is a compile-time error. Is that it?
The only thing I could found related is something like this:
class A<T extends List<Integer> & List<Integer>>{
}
Which as you might know gives the Duplicated bound error.
View Replies
View Related
Feb 21, 2014
I have doubt in generics,
List<int> c=new ArrayList<int>();
why we cannot use primitive data type like int,double.
View Replies
View Related
Jul 11, 2014
I'm learning about inheritance and part of my problem is to create an Order with methods, then an UpdateOrder where the total price is changed by adding four dollars to it, and then a main method displaying a few orders. I've copied all three below in order. My question is when I run the program it will display the totalprice() first for the second order followed by name, number, etc.what you override always displayed first regardless of the order you put them in? (The issue is at line 31 on the third code.)
import javax.swing.JOptionPane;
public class Order { //superclass
private String customerName;
private int customerNumber;
protected int quantityOrdered;
protected double unitPrice;
protected double totalPrice;
[code]....
View Replies
View Related
Dec 16, 2014
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
View Replies
View Related
Jun 10, 2014
how inheritence and exception work together ?? what are the rules ??
View Replies
View Related
Apr 17, 2014
If I define a class which contains a few static fields, and then have a few classes who inherit this class, then all these classes would have the static field as well. Now my question is the following: would all those sub classes (and the base class itself) share the same object, or would each class have one object for all it's instances?
View Replies
View Related
Feb 24, 2014
I've tried to write a package and two classes this way:
<path>/pack/Aclass.java
Java Code: package pack;
public class Aclass<T> {
private T t;
public void set(T t) {
this.t = t;
[code]....
View Replies
View Related