Rules on how to use objects

  • Consider a class definition:

    public class SomeClass
    {
        public int x = 44;
        
        public void method1() 
        {
            System.out.println("SomeClass.m1()");
        }
        
        public void method2() 
        {
            System.out.println("SomeClass.m2()");
        }
    } 

  • We have learned to create objects with the SomeClass class:

      SomeClass a;   // Defines a reference variable
    
      a = new SomeClass(); // Create a SomeClass object
                           // and make a reference to the new object 

Rules on how to use objects

  • The class definition has another use:

    public class SomeClass
    {
        public int x = 44;
        
        public void method1()
        {
            System.out.println("SomeClass.m1()");
        }
        
        public void method2()
        {
            System.out.println("SomeClass.m2()");
        }
    } 

  • The class definition also specifies that the legal member access using an SomeClass variable are the following:

      a.x
      a.method1()
      a.method2() 

Rules on how to use objects

Rule: a reference variable must reference to an object that contains all members defined in its class

Reason: the referenced object must perform all actions in the class or else we can have an request error !

The normal way to use objects

The simplest way to satisfy the rule is to reference to an object of the same class type:

However: there is another safe way due to the inheritance relationship !!

Review:   a subclass inherits all normal members from its superclass

A subclass inherits all the normal members (excluding constructors) from its superclass:

Review:   a subclass object contains a superclass object

A subclass object contains all normal members found in a superclass object:

Review:   a subclass object contains a superclass object

A subclass object contains all normal members even when we Overriding some methods:

Review + intro to polymorphism:   using an instance variable to access members in an object

An instance variable a of the type SomeClass can be used to access the (public) members in SomeClass:

Example using an instance variable to access members in SomeClass

Rule: a reference variable must reference to an object that contains all members defined in its class

public class myProg
{
    public static void main(String[] args)
    {
        // The normal way to
	// satisfy the above rule:
        SomeClass a = new SomeClass();
            // Make a reference to an object
	    // of the same class

        System.out.println(a.x);
        a.method1();
        a.method2();
    }
}
  
public class SomeClass
{
    public int x = 44;
    
    public void method1()
    {
        System.out.println("SomeClass.m1()");
    }
    
    public void method2()
    {
        System.out.println("SomeClass.m2()");
    }
 
}
  

Review + intro to polymorphism:   using an instance variable to access members in an object

An instance variable a of the type NewClass can be used to access the (public) members in NewClass :

Example using an instance variable to access members in NewClass

Rule: a reference variable must reference to an object that contains all members defined in its class

public class myProg
{
    public static void main(String[] args)
    {
        // The normal way to
	// satisfy the above rule:
        NewClass a = new NewClass();
            // Make a reference to an object
	    // of the same class

        System.out.println(a.x);
        a.method1();
        a.method2();
        a.method3();
    }
} 
public class NewClass extends SomeClass
{
    public void method1() // Override
    {
        System.out.println("**NewClass.m1()");
    }
    
    public void method3()
    {
        System.out.println("**NewClass.m3()");
    }
 
}


  
public class SomeClass
{
    public int x = 44;
    
    public void method1()
    {
        System.out.println("SomeClass.m1()");
    }
    
    public void method2()
    {
        System.out.println("SomeClass.m2()");
    }
 
}
  

A subclass object can perform more actions than a superclass object

Notice:   a subclass object can perform more actions that a superclass object performs:

 

A subclass object can perform more actions than a superclass object

Therefore, it is illegal to use a subclass reference variable to access members in a superclass object:

E.g.:   the action a.method3() is illegal because there is no method3() defined inside a SomeClass object

Example that show the illegal assignment of a superclass object to a subclass variable

 

public class myProg
{
    public static void main(String[] args)
    {
        NewClass a; // Defines a reference variable of
	            // type NewClass
		    // Allowable actions:
		    //     a.x
		    //     a.method1();
		    //     a.method2();
		    //     a.method3();

	a = new SomeClass(); // Make a reference to a SomeClass object

	            // Illegal, because the SomeClass object
		    // cannot perform:
		    //       a.method3();
    }
} 
  

A subclass object can perform all actions of a superclass object

Notice:   a subclass object can perform all actions that a superclass object performs:

 

A subclass object can perform all actions of a superclass object

Therefore, it is safe to use a superclass reference variable to access members in a subclass object:

Java does allow you to access members in a subclass object using a superclass reference variable !

Example that show the legal assignment of a subclass object to a superclass variable

 

public class myProg
{
    public static void main(String[] args)
    {
        SomeClass a; // Defines a reference variable of
	             // type SomeClass
		     // Allowable actions:
		     //     a.x
		     //     a.method1();
		     //     a.method2();

	a = new NewClass(); // Make a reference to a NewClass object

	             // Legal, because the NewClass object
		     // can perform all actions
		     // required by the SomeClass type
    }
} 
  

Example using a superclass instance variable to access members in NewClass

public class myProg
{
    public static void main(String[] args)
    {
        // Normal way to access member in NewClass
        NewClass a = new NewClass();
        
        System.out.println(a.x);
        a.method1();
        a.method2();
        a.method3();

        // New way to access member in NewClass
        SomeClass b = new NewClass(); // Allowed !
        System.out.println(b.x);
        b.method1();
        b.method2();
     // b.method3(); // This is illegal !
    }
} 
public class NewClass extends SomeClass
{
    public void method1() // Override
    {
        System.out.println("**NewClass.m1()");
    }
    
    public void method3()
    {
        System.out.println("**NewClass.m3()");
    }
 
}


  
public class SomeClass
{
    public int x = 44;
    
    public void method1()
    {
        System.out.println("SomeClass.m1()");
    }
    
    public void method2()
    {
        System.out.println("SomeClass.m2()");
    }
 
}
  

A superclass reference variable is allowed to reference to a subclass object !

Polymorphism

  • Polymorphism = the phenomenon that the same expression can result in different actions

    Example: the same expression a.method1() has different results

        public static void main(String[] args)
        {
            SomeClass a; // Superclass reference variable
    
            // Use superclass variable to accces
            // overridden method in superclass
            a = new SomeClass();  // References superclass object
            a.method1();          // Calls SomeClass.method1()
    
            // Use superclass variable to accces
            // overridden method in subclass
            a = new NewClass();   // References subclass object
            a.method1();          // Calls NewClass.method1()
        } 

  • Polymorphism is caused by using a superclass reference variable to access overridden members in the superclass and their subclasses