• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Need Clarification

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
public class interface Forefather
{
public int i = 5;
static float = 2.3f;
final int k = 3;
}

public class Father
{
}
public class Son extends Father implements Forefather
{
public static void main(String args[])
{
Forefather ff = new Forefather();
Father f = new Father();
Son s = new Son();
ff = s;
System.out.println("" +ff);
f = s;
System.out.println("f " +f);
}
}


As we know that "if we assign 'existing object reference' to a 'reference variable' both 'reference variables' refer to 'same object'.Also it is legal to assign a 'child object' to a 'parent object'but 'not viceversa'
Further we can also assign an 'object reference' to an 'interface reference' variable if the 'object' implements the 'interface'

so if above is true why cant the above program doesnt compile and run?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a few errors:
Forefather can't be both a class and an interface.
There can't be more than one public class or interface in a source file.
The variable initialized to 2.3f has no name.
You can't instantiate an interface.
 
reply
    Bookmark Topic Watch Topic
  • New Topic