Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you access an instance variable from a static method? Explain why or why not

ID: 3662173 • Letter: C

Question

Can you access an instance variable from a static method? Explain why or why not.

Can you access a static variable from an instance method? Explain why or why not.

Consider the case where you have two classes, ClassA and ClassB, such that ClassA contains a member variable defined as ClassB myBobj. ClassB contains a constructor which takes a String parameter. ClassA provides a constructor which accepts a String parameter which is to be provided to myBobj. What needs to be done to initialize the ClassB member variable with the value from the ClassA constructor?

Here are some class definitions with questions that follow.

public class BaseClass { // methods of BaseClass }

public SomeClass extends BaseClass

{ // methods of SomeClass }

a. SomeClass scobj = new SomeClass( );

b. BaseClass bcobj = scobj;

c. SomeClass scobj2 = bcobj;

d. bcobj.aMethodOfSomeClass( ); // does not exist in BaseClass

Is statement b above legal, or is casting required?

Statement c is trying to assign the SomeClass object referred to by bcobj to a SomeClass reference variable scobj2. What is wrong with this statement and how would you fix it.

In statement d, a BaseClass reference is trying to access a SomeClass class method that is not part of BaseClass. Is this legal? Explain.

Give an example using Java syntax of how some class Y can be setup to inherit from some other class X.

What is a significant difference between C++ and Java inheritance?

How is the keyword super used? Give two different uses for it and include Java examples.

Explain the difference between public, private, and protected access specifiers. Give some guidelines of when each should be used.

Explain what package access means and how it is specified.

What can you do in Java to prevent a method from being overridden in a derived class? Give example code in your answer.

How do you prevent a class from being extended? Give example code in your answer.

Explain how dynamic binding works in Java.

Explanation / Answer

1. Can you access an instance variable from a static method? Explain why or why not.
Ans. No,Static means "fixed" or "stationary"
When executing static method expectation is that it will produce the same result if you supply the same input.

2. Can you access a static variable from an instance method? Explain why or why not.
Ans. Yes, it is allowed by instance method. The value of static variable will be changed in instance method.

Consider the case where you have two classes, ClassA and ClassB, such that ClassA contains a member variable defined as ClassB myBobj. ClassB contains a constructor which takes a String parameter. ClassA provides a constructor which accepts a String parameter which is to be provided to myBobj. What needs to be done to initialize the ClassB member variable with the value from the ClassA constructor?
Here are some class definitions with questions that follow.
public class BaseClass { // methods of BaseClass }
public SomeClass extends BaseClass
{ // methods of SomeClass }
a. SomeClass scobj = new SomeClass( );
b. BaseClass bcobj = scobj;
c. SomeClass scobj2 = bcobj;
d. bcobj.aMethodOfSomeClass( ); // does not exist in BaseClass
3. Is statement b above legal, or is casting required?
Ans. It is legal. but It is not working correctly. So casting is required.

4. Statement c is trying to assign the SomeClass object referred to by bcobj to a SomeClass reference variable scobj2. What is wrong with this statement and how would you fix it.
Ans. Here problem is type mismatch.
Here we are creating object of Derived class and providing refrence of class Base class. we have typecast it before providing to object of derived class object.
SomeClass scobj2=(SomeClass)bcobj;
5. In statement d, a BaseClass reference is trying to access a SomeClass class method that is not part of BaseClass. Is this legal? Explain.
Ans. It is not legal because aMethodOfSomeClass( ) is not defined in BaseClass(). sp that BaseClass object is not able to access SomeClass() member function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote