answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Is it perfectly legal to assign a subclass object to superclass?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you call an abstract method from a non abstract method in java?

We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}


Can a super class varialble can reference a subclass object in java?

No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


Java programming final test?

Can you answer in this java programming quiz? just 1 or 2 answers are fine.. 1. The != operator is used to evaluate _______ between two values. Non-equality !! =! Equality 2. The interface part of a class is created using package access protected visibility private visibility 3. User defined data type or objects (like a Car class object) are passed by Reference Value Copy Placement 5 . If you declare constructors private, they ________ be called by all classes and methods to create objects. Can can privately can virtually Can t 6. Components are placed in a container by calling the __________ method. registerComponent () addActionListener () setLayout () add () 7. If an abstract method is not defined in a subclass, the subclass is Abstract Overridden Promoted to the superclass Hidden 8. Subclass objects of different type that inherit from the same superclass Can be treated as superclass objects Can't share private superclass data Don't share the same superclass data Don't share any data 9. Polymorphism Promotes and allows sharing of data and methods between classes Allows development of general solutions and the runtime selection of methods Prevents methods outside a class from directly accessing instance variables Allows classes to contain/combine both data (properties) and methods (behaviors) 10. Inheritance relationships form _________. Hierarchical Structures Peer to Peer Networks Loosely bounded sets Network Structures 11. Your program class that inherits from JPanel ____________. Uses a constructor to initialize objects Inherits the behavior of a JObject program Is unable to override paint_component () Knows nothing of drawing 12. The paint_component method ___________. Must be private Expects a Graphics object argument Never calls super.paint_component (g) Must be called by you 13. Successful use of polymorphism depends on Identifying generic methods Identifying an inheritance class composition Identifying abstract objects Deciding on how to use subclass references 14. A superclass method can be overridden in a subclass in order to Change the name Make the method private Provide new behavior appropriate to the subclass Allow hidden access 16 . In order to place subclass objects of different type in an array They must inherit from the same subclass The array reference itself must be of superclass type You must cast and add-in missing superclass members They must be of the same type 17. The name of the event handler method of the ActionListener interface is ____. itemStateChanged mouseMoved actionPerformed + keyPressed 18. Downcasting Converts a subclass reference to a superclass reference Converts a subclass object to a superclass object Converts a superclass reference to a subclass reference Converts a superclass object to a subclass object 19. A simple layout manager is the ____ manager. GridLayout BoxLayout FlowLayout PanelLayout 20. An individual array element is accessed using its _________. Type Subscript/Index Value Object 21. Since it generates byte codes, the Java compiler is _________. An interpreter Not a true compiler Object code Windows instructions 22. When GUI components are displayed on the screen, a/an ___________ positions and displays those components. Layout manager Container object Event handler Interface class 23. An executing program class must contain a/an Body Main method Instance variable Comment 24. Referring to a superclass object through a subclass reference Can cause a runtime error Requires that the superclass reference refers to the same subclass object as the cast Requires an explicit object to avoid the compilation error 25. Polymorphism works for Unrelated classes Overloaded classes An inheritance hierarchy of classes Has-a classes 26. Given that a=4, b=10, c=2, d=5, and x=3, the expression "(a + c) / c * d + a" evaluates to 20 4 128 19+ 27. When dynamic binding takes place, the Java system Determines the type of the object at compile time Binds a method name to its address at runtime Moves up the inheritance hierarchy Makes a list of generic event handlers 28. When dynamic binding takes place, the Java system Determines the type of the object at compile time Binds a method name to its address at runtime Moves up the inheritance hierarchy Makes a list of generic event handlers 29. If a subclass constructor explicitly calls a superclass constructor It overrides the superclass constructor It must call the noarg constructor It cannot access protected data The call must be the first line of code 30. The composition relationship can be identified as a/an _________ relationship. Enduring Has/a Is/a Overridden 31. When declared with a static lifetime, Variables exist during the execution of a method Variables exist from beginning to end of program execution Methods and instance variables are visible Variables declared outside the {} of a block 32. When an array is passed as an argument to a method, _____. The array is passed by reference Only the first value is passed The called method can't change array values It is passed by value 33. When the expression "result = total + input * 2;" is evaluated the __________ is placed in the location of the left hand side. Left side result Assignment Right side result Operator 34. When invoked, a subclass constructor Automatically calls the noarg constructor of the superclass (if one is not explicitly called) Cannot access superclass data even if protected Returns a superclass object Does not use the superclass constructor 35. When declared with automatic scope, Variables exist during the execution of a method Variables exist from beginning to end of program execution Methods and instance variables are visible Variables declared outside the {} of a block 36. The inheritance class relationship Promotes and allows sharing of data and methods between classes Allows development of general solutions and the runtime selection of methods Prevents methods outside a class from directly accessing instance variables Allows classes to contain/combine both data (properties) and methods (behaviors) 37. When a subclass method has the same name and type as a superclass method but accepts different arguments, that's an example of Protected inheritance Overriding Polymorphism Overloading 38. When you want to declare constant values, you would use the ________ keyword. objects final const Static variables 39. The toString method Is not provided by class Object Is called implicitly to convert an object to a String Can be overloaded Can't be overridden 40. A switch statement tests a variable or expression against __________. Constant, integral values Expressions Decimal numbers Strings 41. Reffering to a superclass object through a subclass reference can cause a runtime error Requires that the superclass reference refers to the same subclass object Requires an explicit object to avoid the compilation error 48. Because all variables must be declared before their use, Java Is an object oriented language Is a strong variable language Is a strongly typed language Is a typing language 49. Variables declared/defined inside a method are General variables Visible everywhere Local variables Instance variables 50. The while statement Is the most specific form of repetition Requires initialization, test, and change of a control variable Will never skip execution of the loop body Doesn't use a control variable 51. The do-while statement Does not require a test of cond Is the most specific form of repetition Executes at least once Is intended to replace for statements 52. The use of superclass references to refer to subclass objects has been found useful For container objects When placing elements of subclass objects in a superclass array When placing elements of superclass objects in a subclass array For Composition classes 53. Overloaded constructors are used to Create different types of objects Create two dimensional arrays Create primitive data types Initialize objects of the same class in different ways 54. Java's mechanism for placing related classes in a subdirectory uses the ________ keyword. package import extensible extends 55. this is a reference to an object Can t be used to access object data Is passed to every class Is hidden and private 56. An anonymous inner class ____. Has a class name Typically is used to define an event handler class Has no scope Contains anonymous methods 57. The new operator is used to __________. Allocate memory for an object or array Call an object destructor Call the garbage collector Promote visibility 58. The drawOval method takes the same four arguments as ________. drawLine () drawPoly () drawRect () fillArc () 59. In order for a class method to access the private data of a class object (not of its class) The data can only be read It can't The data can be changed directly get/set methods must be used Question 62 62. Class methods ________ access all class variables, public or private. Can Can't 64. A reference in a Java program Causes object initialization Refers or points to a runtime data type Causes memory allocation Refers (or points to) to an object 65. The listener interface for radio buttons is the ____ interface. ActionListener MouseListener KeyListener ItemListener 66. implementation part of a class is created using 67. What types of superclass methods/variables can be directly accessed from a sub class? public private provoked 68. The statement(s) in the body of an if structure should be indented in order to Compile properly They shouldn t Stand out and enhance readability Allow repetition 69. The sdk program that is the JVM and runs programs is java javac appletviewer


How super class variable can be used to refer the sub class object?

The super variable is not a reference to a subclass. It is a reference to the superclass. class MyClass { void printType() { System.out.println("This is a MyClass"); } // MySubClass is a subclass of MyClass. Within this class, the super keyword // refers to MyClass. static class MySubClass extends MyClass { void printType() { // Tell Java we also want to call the printType method of the super class super.printType(); System.out.println("This is a MySubClass"); } } }

Related questions

What is heritance in java?

With inheritance, you can use methods and fields from the superclass in a subclass. So for example when I have a class Person with fields age and gender, I can make a subclass Student. a Student object has always the fields from its superclass Person (age and gender), but you can make extra fields for a Student object. The same is true for methods: a method defined in the Person class can also be used on a Student object because Student is a subclass from Person. Got it? ;)


Can you call an abstract method from a non abstract method in java?

We can't call (i.e, execute) an abstract method in java because these methods don't contain any code to execute!In some special cases like when an abstract method is overridden in a subclass, and when we are using super class reference variable( which is referring that subclass object), it appears that we are calling abstract method in super class. But actually the code in the subclass method is being executed.Example:abstract class SuperClass{abstract void show(); //abstract method in super class}class SubClass extends SuperClass{void show(){ //show() of SuperClass overridden in SubClassSystem.out.println("SubClass Method");}}class Example{public static void main(String... args){SuperClass sup=new SubClass();sup.show(); //SubClass show() will be executed !!!}}


Can a super class varialble can reference a subclass object in java?

No. Because, what is the guarantee that when the super class code is being executed there will always be a sub class? But, the other way round - sub class object accessing a super class variable is possible because, if a sub class uses inheritance to extend from another class, then it is 100% sure that the parent class is going to be around. So a sub class can access the super class variable.


Why casting a super class reference to subclass reference is potentially dangerous?

Casting a superclass reference to a subclass reference can be dangerous because the subclass may have additional methods or attributes that are not present in the superclass. If you then try to access these subclass-specific features using the superclass reference, it can cause runtime errors like ClassCastException or NullPointerException. It's generally safer to use polymorphism and dynamic binding to work with subclass objects using superclass references.


Is object of superclass created when object of derived class is created?

Not as a separate instance. The derived class instance can be used anywhere a superclass instance is expected, so it is an instance of the superclass in that respect, and a superclass constructor will be called at creation time. However, it will always act like an instance of the derived class, even if it is explicitly cast to the superclass. So overridden methods will always call the derived class method, regardless of how an outside caller refers to the instance.


Java programming final test?

Can you answer in this java programming quiz? just 1 or 2 answers are fine.. 1. The != operator is used to evaluate _______ between two values. Non-equality !! =! Equality 2. The interface part of a class is created using package access protected visibility private visibility 3. User defined data type or objects (like a Car class object) are passed by Reference Value Copy Placement 5 . If you declare constructors private, they ________ be called by all classes and methods to create objects. Can can privately can virtually Can t 6. Components are placed in a container by calling the __________ method. registerComponent () addActionListener () setLayout () add () 7. If an abstract method is not defined in a subclass, the subclass is Abstract Overridden Promoted to the superclass Hidden 8. Subclass objects of different type that inherit from the same superclass Can be treated as superclass objects Can't share private superclass data Don't share the same superclass data Don't share any data 9. Polymorphism Promotes and allows sharing of data and methods between classes Allows development of general solutions and the runtime selection of methods Prevents methods outside a class from directly accessing instance variables Allows classes to contain/combine both data (properties) and methods (behaviors) 10. Inheritance relationships form _________. Hierarchical Structures Peer to Peer Networks Loosely bounded sets Network Structures 11. Your program class that inherits from JPanel ____________. Uses a constructor to initialize objects Inherits the behavior of a JObject program Is unable to override paint_component () Knows nothing of drawing 12. The paint_component method ___________. Must be private Expects a Graphics object argument Never calls super.paint_component (g) Must be called by you 13. Successful use of polymorphism depends on Identifying generic methods Identifying an inheritance class composition Identifying abstract objects Deciding on how to use subclass references 14. A superclass method can be overridden in a subclass in order to Change the name Make the method private Provide new behavior appropriate to the subclass Allow hidden access 16 . In order to place subclass objects of different type in an array They must inherit from the same subclass The array reference itself must be of superclass type You must cast and add-in missing superclass members They must be of the same type 17. The name of the event handler method of the ActionListener interface is ____. itemStateChanged mouseMoved actionPerformed + keyPressed 18. Downcasting Converts a subclass reference to a superclass reference Converts a subclass object to a superclass object Converts a superclass reference to a subclass reference Converts a superclass object to a subclass object 19. A simple layout manager is the ____ manager. GridLayout BoxLayout FlowLayout PanelLayout 20. An individual array element is accessed using its _________. Type Subscript/Index Value Object 21. Since it generates byte codes, the Java compiler is _________. An interpreter Not a true compiler Object code Windows instructions 22. When GUI components are displayed on the screen, a/an ___________ positions and displays those components. Layout manager Container object Event handler Interface class 23. An executing program class must contain a/an Body Main method Instance variable Comment 24. Referring to a superclass object through a subclass reference Can cause a runtime error Requires that the superclass reference refers to the same subclass object as the cast Requires an explicit object to avoid the compilation error 25. Polymorphism works for Unrelated classes Overloaded classes An inheritance hierarchy of classes Has-a classes 26. Given that a=4, b=10, c=2, d=5, and x=3, the expression "(a + c) / c * d + a" evaluates to 20 4 128 19+ 27. When dynamic binding takes place, the Java system Determines the type of the object at compile time Binds a method name to its address at runtime Moves up the inheritance hierarchy Makes a list of generic event handlers 28. When dynamic binding takes place, the Java system Determines the type of the object at compile time Binds a method name to its address at runtime Moves up the inheritance hierarchy Makes a list of generic event handlers 29. If a subclass constructor explicitly calls a superclass constructor It overrides the superclass constructor It must call the noarg constructor It cannot access protected data The call must be the first line of code 30. The composition relationship can be identified as a/an _________ relationship. Enduring Has/a Is/a Overridden 31. When declared with a static lifetime, Variables exist during the execution of a method Variables exist from beginning to end of program execution Methods and instance variables are visible Variables declared outside the {} of a block 32. When an array is passed as an argument to a method, _____. The array is passed by reference Only the first value is passed The called method can't change array values It is passed by value 33. When the expression "result = total + input * 2;" is evaluated the __________ is placed in the location of the left hand side. Left side result Assignment Right side result Operator 34. When invoked, a subclass constructor Automatically calls the noarg constructor of the superclass (if one is not explicitly called) Cannot access superclass data even if protected Returns a superclass object Does not use the superclass constructor 35. When declared with automatic scope, Variables exist during the execution of a method Variables exist from beginning to end of program execution Methods and instance variables are visible Variables declared outside the {} of a block 36. The inheritance class relationship Promotes and allows sharing of data and methods between classes Allows development of general solutions and the runtime selection of methods Prevents methods outside a class from directly accessing instance variables Allows classes to contain/combine both data (properties) and methods (behaviors) 37. When a subclass method has the same name and type as a superclass method but accepts different arguments, that's an example of Protected inheritance Overriding Polymorphism Overloading 38. When you want to declare constant values, you would use the ________ keyword. objects final const Static variables 39. The toString method Is not provided by class Object Is called implicitly to convert an object to a String Can be overloaded Can't be overridden 40. A switch statement tests a variable or expression against __________. Constant, integral values Expressions Decimal numbers Strings 41. Reffering to a superclass object through a subclass reference can cause a runtime error Requires that the superclass reference refers to the same subclass object Requires an explicit object to avoid the compilation error 48. Because all variables must be declared before their use, Java Is an object oriented language Is a strong variable language Is a strongly typed language Is a typing language 49. Variables declared/defined inside a method are General variables Visible everywhere Local variables Instance variables 50. The while statement Is the most specific form of repetition Requires initialization, test, and change of a control variable Will never skip execution of the loop body Doesn't use a control variable 51. The do-while statement Does not require a test of cond Is the most specific form of repetition Executes at least once Is intended to replace for statements 52. The use of superclass references to refer to subclass objects has been found useful For container objects When placing elements of subclass objects in a superclass array When placing elements of superclass objects in a subclass array For Composition classes 53. Overloaded constructors are used to Create different types of objects Create two dimensional arrays Create primitive data types Initialize objects of the same class in different ways 54. Java's mechanism for placing related classes in a subdirectory uses the ________ keyword. package import extensible extends 55. this is a reference to an object Can t be used to access object data Is passed to every class Is hidden and private 56. An anonymous inner class ____. Has a class name Typically is used to define an event handler class Has no scope Contains anonymous methods 57. The new operator is used to __________. Allocate memory for an object or array Call an object destructor Call the garbage collector Promote visibility 58. The drawOval method takes the same four arguments as ________. drawLine () drawPoly () drawRect () fillArc () 59. In order for a class method to access the private data of a class object (not of its class) The data can only be read It can't The data can be changed directly get/set methods must be used Question 62 62. Class methods ________ access all class variables, public or private. Can Can't 64. A reference in a Java program Causes object initialization Refers or points to a runtime data type Causes memory allocation Refers (or points to) to an object 65. The listener interface for radio buttons is the ____ interface. ActionListener MouseListener KeyListener ItemListener 66. implementation part of a class is created using 67. What types of superclass methods/variables can be directly accessed from a sub class? public private provoked 68. The statement(s) in the body of an if structure should be indented in order to Compile properly They shouldn t Stand out and enhance readability Allow repetition 69. The sdk program that is the JVM and runs programs is java javac appletviewer


How super class variable can be used to refer the sub class object?

The super variable is not a reference to a subclass. It is a reference to the superclass. class MyClass { void printType() { System.out.println("This is a MyClass"); } // MySubClass is a subclass of MyClass. Within this class, the super keyword // refers to MyClass. static class MySubClass extends MyClass { void printType() { // Tell Java we also want to call the printType method of the super class super.printType(); System.out.println("This is a MySubClass"); } } }


When a subclass can call a parent's class constructor?

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.


Which class includes all the others?

The superclass or parent class includes all the other classes. All other classes inherit properties and methods from the superclass.


Which object-oriented concept defines the relationship between a subclass and a super class?

Inheritance


Why there is the need of the constructor if you can assign the value to variables by the help of the methods ..?

A constructor is what allocates memory for an object. If you didn't call a constructor, you would have no object in which to assign values.


When an object is plumb it is?

perfectly vertical