0

Why, using same String name variable in every class gives the name null except the last child class ( cinematographer)?

public class Filmmaker { String name; public void direct() { System.out.println(" Director " +name+ " is Directing A Clockwork Orange \n"); } public void write() { System.out.println(" writer " +name+ " Is writing The Royal Tenenbaums \n"); } public void produce() { System.out.println(" Director " +name+ " produced his own films \n"); } } public class Composer extends Filmmaker { String name; public void compose() { System.out.println(" Legend " + name+ " is composing Love theme from Cinema Paradiso \n"); } } public class Cinematographer extends Composer { String name; public void visualise() { System.out.println(" Legend " +name+ " is filming Y tu Mama Tambien \n"); } } public class Legends extends Cinematographer { public static void main(String[] args){ Legends l=new Legends(); l.name=" Stanley Kubrick "; l.direct(); l.name=" Wes Anderson "; l.write(); l.name=" Stanley Kubrick "; } }

12th Mar 2020, 2:50 PM
Rohan c
Rohan c - avatar
4 Réponses
0
In your program, l.name assignment assigns only it's super class Cinematographer class variable name only.. All other in Filmmaker class names are still unassigned so will give null value only...
12th Mar 2020, 3:01 PM
Jayakrishna 🇮🇳
0
It is assigned. See the filmmaker class i have assigned String name.
12th Mar 2020, 3:04 PM
Rohan c
Rohan c - avatar
0
Which line? You can post your code by copying in code playground and share,, that helps to identify errors easily... Edit: String name; is declaration only.. Not assigned so value null only... name="john" ; is assignment.
12th Mar 2020, 3:07 PM
Jayakrishna 🇮🇳
0
Let name declaration only in Filmmaker, delete declarations in other classes public class Filmmaker { String name;
13th Mar 2020, 3:24 AM
zemiak