Program to demonstrate super() in java



class CD
{
String title;           // name of the item
int length;             // number of minutes
boolean avail;          // is the tape in the store?

CD(CD vt)
{
title=vt.title;
length=vt.length;
avail=vt.avail;
}

CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}

CD()
{
title=null;
length=0;
avail=false;
}


}


class Movie extends CD
{

String director;          // name of the director
String rating;            // G, PG, R, or X

Movie(Movie m)
{
super(m);
director=m.director;
rating=m.rating;
}

Movie(String t, int l,boolean a,String d,String r)
{
super(t,l,a);
director=d;
rating=r;
}

Movie()
{
super();
director=null;
rating=null;
}

public void show()
{
System.out.println("Title : "+title + "\nLength : " + length + "\nAvail : "+avail );
System.out.println("Director : "+director+"\nRating : "+rating);

}
}




class SuperObjRef
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
CD cd=new CD();
m.show();

cd=m;      //assign movie reference to CD reference

cd.title="Titanic";        //legal
cd.length=120;             //legal
cd.avail=true;             //legal
cd.director="Spielberg";   //illegal
cd.rating="PG";            //illegal
cd.show();                 //illegal
}
}

/*
Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG

Title : null
Length : 0
Avail : false
Director : null
Rating : null

Title : Jaws
Length : 120
Avail : true
Director : Spielberg
Rating : PG
*/

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|