Upcasting And Downcasting Rule For Object And Generics?

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


ADVERTISEMENT

Compiler Error Even After Downcasting

Apr 3, 2014

An example from the SCJP(OCP) book:

Java Code:

class Animal {
void makeNoise() {System.out.println("generic noise"); }
}
class Dog extends Animal {
void makeNoise() {System.out.println("bark"); }
void playDead() { System.out.println("roll over"); }

[Code] .....

The book states that the above code will compile if there is a downcast in the line 14 . But there is a compiler error saying playDead method is not defined for type animal even after downcasting.

View Replies View Related

JRE :: Deployment Rule Set Seems To Be Ignored For Particular App

Nov 19, 2014

I have implemented a deployment rule set for our JRE enterprise deployments and it is working fine. We are currently deploying JRE version 1.8.0_25. I have stumbled upon a Java App (a webcam) on an internet site that I cannot get to run with the deployment rule set.
 
The rule set works with other java apps, but it’s as though it is ignored for this one. The rule set section in the ruleset.xml with the intention of allowing the webcam to work is as follows:

  <rule>
    <id location="IP Address of I assume the webcam, address specified in error when launching app" />
    <action permission="run" />
  </rule>
  <rule>
    <id location="URL of website hosting the webcam" />
    <action permission="run" />
  </rule>
 
I pulled it out of the deployment rule set and added it to the exception list without any luck getting it to work that way. How I can get this app to work with my Deployment Rule Set?

View Replies View Related

Password Rule Using Wrapper Class

Oct 26, 2014

I have to use wrapper class for this assignment. I need to know if my code is more like object oriented or not and am I using the wrapper class properly here? Let me know if I can make this code better.

I also need to know if I can mask this password input (STRING) using NetBeans/Eclipse IDE?

Password rule is as followed : Length of Password 8-12, with no special characters, including minimum one uppercase letter, and only one numeric value.

Java Code:

import java.util.Scanner;
public class Assignment1
{
private static String password=null;
private static void password_input()

[Code] .....

View Replies View Related

Trapezoidal Rule - Numerical Methods

Dec 2, 2014

I tried running the code for trapezoidal rule. It's my project in Numerical Methods, here's the code:

static double trapezoidRule (int size, double[] x, double[] y)
{ double sum = 0.0,
increment;
for ( int k = 1; k < size; k++ )
{//Trapezoid rule: 1/2 h * (f0 + f1)
increment = 0.5 * (x[k]-x[k-1]) * (y[k]+y[k-1]);
sum += increment;

[Code] ....

It cannot be compiled and I always get FileNotFoundException. I found on Javadocs that this will be thrown when a file with the pathname does not exist.

View Replies View Related

JRE :: How To Create Proper Deployment Rule Set

Dec 2, 2014

I have a signed certificate from Entrust which I used to sign a DeploymentRuleSet.xml file.  I placed the DeploymentRuleSet.jar in the proper location C:WindowsSunJavaDeployment, afterward the java control panel's security tab shows a link to "show the active deployment rule set" which did not exist prior to coping the file to the directory.  When I click on the link a new window opens and says "Rule Set not found" ....

View Replies View Related

Checking Password Rule Using Wrapper Class

Oct 26, 2014

I have to use wrapper class for this assignment. I need to know if my code is more like object oriented or not and am I using the wrapper class properly here? Let me know if I can make this code better.

I also need to know if I can mask this password input (STRING) using NetBeans/Eclipse IDE?

Password rule is as followed : Length of Password 8-12, with no special characters, including minimum one uppercase letter, and only one numeric value.

Here is the code :

import java.util.Scanner;
public class Assignment1
{
private static String password=null;
private static void password_input() {
Scanner input = new Scanner(System.in);
password = input.nextLine();

[Code] .....

View Replies View Related

Check Password Rule Using Wrapper Class

Oct 26, 2014

I have to use wrapper class for this assignment. I need to know if my code is more like object oriented or not and am I using the wrapper class properly here? Let me know if I can make this code better.

I also need to know if I can mask this password input (STRING) using NetBeans/Eclipse IDE? I did search, but I found the console method.

Password rule is as followed : Length of Password 8-12, with no special characters, including minimum one uppercase letter, and only one numeric value.

Here is the code :

import java.util.*;
import java.lang.String;
import java.lang.Character;

public class CheckingPassword {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Password: ");
String password = input.next();

[Code] .....

View Replies View Related

Check Password Rule Using Wrapper Class

Oct 26, 2014

I have to use wrapper class for this assignment. I need to know if my code is more like object oriented or not and am I using the wrapper class properly here? Let me know if I can make this code better.

I also need to know if I can mask this password input (STRING) using NetBeans/Eclipse IDE?

Password rule is as followed : Length of Password 8-12, with no special characters, including minimum one uppercase letter, and only one numeric value.

Here is the code :

import java.util.Scanner;
public class Assignment1
{
private static String password=null;
private static void password_input()

[Code] ....

View Replies View Related

Create Password Rule - Valid / Invalid

Oct 31, 2014

Make a password rule, and take input of the password from the user and compare the password with your rule, if the user had entered valid password then print the message, password valid, else the password is invalid for (the whatever) reason.

Rules:-
-Length 8-12
-Should have one numeric character
-No Special Character
-Atleast one UPPERCASE letter

PHP Code:

import java.util.*;
import java.lang.String;
import java.lang.Character;
public class PasswordSpence {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a Password: ");
String password = input.next();
if (isValid(password)) {

[Code] ....

View Replies View Related

Integer Sum In Generics

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

Casting Exception In Generics

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

Clarify Generics For Classes

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

Java Generics Super And Ext

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

Returning Int Array Using Generics

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

Generics Super Keyword

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

Why Use Extends Keyword Instead Of Implements In Generics

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

Generics Wildcard Array Of References?

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

Generics In Java - E Is A Subtype Of Comparable

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

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 View Related

Doubly Linked Lists And Using Java Generics

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

Add Only String And Integer To ArrayList By Using JAVA GENERICS

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

First Time Using Generics - Incomparable Type Errors

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

Erasure Feature From Generics - Duplicated Bound Error

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

Java Generics - Why Cannot Use Primitive Data Type Like Int / Double

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

Equals Method - Take Object Reference And Return True If Given Object Equals This Object

Mar 28, 2014

Create an equals method that takes an object reference and returns true if the given object equals this object.

Hint: You'll need 'instanceof' and cast to a (Geocache)

So far I have:

public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}

else return false;
}

I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved