Scenario #2

Expected Understanding: Interface, Class, static variables, dynamic binding


1) Create an interface called ‘Actor’
– Have variables boolean makeUpRequired
– Assign ‘true’ value for ‘makeUpRequired’
– Have variable String address.
– Assign value as “Chennai” to address.
– Have void methods act(), dance(), sing()

interface Actor
{
boolean makeUpRquired=true;
String address="Chennai";
	
public void act();
public void dance();
public void sing();
}

2) Create class named as ActorSivakumar with main method
– implement interface ‘Actor’ to this class.
– Give your own definitions for methods from interface
– Have static variable String address.
– Assign value to address as “Coimbatore”.
– Have instance method ‘speaking()’ with void return data type.
– Create instance for ActorSivakumar as below
ActorSivakumar as = new ActorSivakumar(65, “Audi Car”)
– Handle with proper Constructor
– Access all the methods from ActorSivakumar class
– Access variable address and print the value
– Create another instance of interface ‘Actor’ using dynamic binding approach
Actor ac = new Sivakumar();
– Handle with proper Constructor
– Access methods in ActorSivakumar class.
– Access variable address using ‘ac’ intance and print the value
– Observe and note down the difference between two instances

class ActorSivakumar implements Actor
{
int age;
String car;
static String address="Coimbatore";
ActorSivakumar()
	{
		System.out.println("Actor Sivakumar");
	}
ActorSivakumar(int age, String car)
{

}
public static void main(String[] ags)
{
 ActorSivakumar as = new ActorSivakumar(65,"Audi Car");
 as.act();
 as.dance();
 as.sing();
 as.speaking();
  System.out.println("The addrsss is "+as.address);
  Actor ac = new ActorSivakumar();
  ac.act();
  ac.dance();
  ac.sing();
  ac.speaking();// nit work only for actor class
}
public void act()
{
 System.out.println("Acting");
}
public void dance()
{
 System.out.println("Dancing");
}
public void sing()
{
 System.out.println("Singing");

}
public void speaking()
{
 System.out.println("Speaking");
}
}

Output

ac object only use in Actor class

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free