I have the following code in two separate files.

package animal;

public class Frog
{
    protected void ribbit()
    {
        System.out.println("In Frog class!");
    }
}





package other;

import animal.*;

public class Tadpole extends Frog

{
    protected void ribbit()
    {
        System.out.println("In Tadpole class!");
    }

    public static void main(String[] args)
    {
        Tadpole t = new Tadpole();
        t.ribbit();

        Frog f = new Tadpole();
        f.ribbit(); // Does not compile
    }
}


The first Tadpole object assigned to Tadpole type obviously compiles fine and the call to ribbit() will be to the Tadpole's ribbit() implementation. The second Tadpole object that is created and assigned to a Frog reference. However, the call to ribbit() results in a compiler error. 

I know that if you create a subclass object in the subclass and assign to a superclass reference that is outside of the subclass's package and try to call a superclass method, this is not allowed. But in this case, shouldn't polymorphism make the object reference "f" call the Tadpole's ribbit() method since a Tadpole object was assigned to it? Why does this cause a compiler error and why is this is not allowed?


Question: DataContractSerializer ignore by default
Answer: O
that's  not true  try to  take a look at this  code from  msdn  and try to  uncomment and comment the datamember attribute  in  class  person for  the property  LastName for example  an yot  will  see  that's not serialized as  @Nair  saied  . I think that you are trying to write to the same file  but the file it's locked  so  you will think that all properties are serialized.  try to change  the  name  every  time  and you will see  that property not marked with  datamember attribute will  not be serialized  

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

    // You must apply a DataContractAttribute or SerializableAttribute 
// to a class to have it serialized by the DataContractSerializer.
[DataContract()]
class Person : IExtensibleDataObject
{
private string LastNameValue;
// Apply the DataMemberAttribute to fields (or properties)  
// that must be serialized.
[DataMember()]
public string FirstName;

//[DataMember()]
public string LastName
{
    get { return LastNameValue; }
    set { LastNameValue = value; }
}

[DataMember(Name = "ID")]
public int IdNumber;

// Note that you can apply the DataMemberAttribute to  
// a private field as well.
[DataMember]
private string Secret;

public Person(string newfName, string newLName, int newIdNumber)
{
    FirstName = newfName;
    LastName = newLName;
    IdNumber = newIdNumber;
    Secret = newfName + newLName + newIdNumber;
}

// The extensionDataValue field holds data from future versions  
// of the type.  This enables this type to be compatible with  
// future versions. The field is required to implement the  
// IExtensibleDataObject interface. 

private ExtensionDataObject extensionDatavalue;

public ExtensionDataObject ExtensionData
{
    get
    {
        return extensionDatavalue;
    }
    set
    {
        extensionDatavalue = value;
    }
}
}

  public class Test
  {
public static void Main(string[] args)
{
    try
    {
        WriteObject(@"DataMemberAttributeExample.xml");
        ReadObject(@"DataMemberAttributeExample.xml");
    }
    catch (Exception exc)
    {
        Console.WriteLine(
        "The serialization operation failed: {0} StackTrace: {1}",
        exc.Message, exc.StackTrace);
    }
    finally
    {
        Console.WriteLine("Press &lt;Enter&gt; to exit....");
        Console.ReadLine();
    }
}

public static void WriteObject(string filename)
{
    // Create a new instance of the Person class.
    Person p1 = new Person("Zighetti", "Barbara", 101);
    FileStream writer = new FileStream(filename,
    FileMode.OpenOrCreate);
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    ser.WriteObject(writer, p1);
    writer.Close();
}

public static void ReadObject(string filename)
{
    // Deserialize an instance of the Person class  
    // from an XML file.
    FileStream fs = new FileStream(filename,
    FileMode.OpenOrCreate);
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    // Deserialize the data and read it from the instance.
    Person deserializedPerson = (Person)ser.ReadObject(fs);
    fs.Close();
    Console.WriteLine(String.Format("{0} {1}, ID: {2}",
    deserializedPerson.FirstName, deserializedPerson.LastName,
    deserializedPerson.IdNumber));
    }

}