2. Can you access a static variable from an instance method? Explain why or why
ID: 3561672 • Letter: 2
Question
2. Can you access a static variable from an instance method? Explain why or why not.
3. 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?
4. 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
A.Is statement b above legal, or is casting required?
B.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.
C.In statement d, a BaseClass reference is trying to access a SomeClass class method that is not part of BaseClass. Is this legal? Explain.
5. Give an example using Java syntax of how some class Y can be setup to inherit from some other class X.
6.What is a significant difference between C++ and Java inheritance?
7.How is the keyword super used? Give two different uses for it and include Java examples.
8.Explain the difference between public, private, and protected access specifiers. Give some guidelines of when each should be used.
9.Explain what package access means and how it is specified.
10.What can you do in Java to prevent a method from being overridden in a derived class? Give example code in your answer.
11.How do you prevent a class from being extended? Give example code in your answer.
12.Explain how dynamic binding works in Java.
Explanation / Answer
1)
No, static method cannot access the instance variables of a class directly.
Reason:
There is a way to access the instance variables in the static methods by creating the instance to the class (class object). Through the class object reference, the private variables or instance variables can be accessed in static methods.
For example,
Consider the main method in the program. The main method is a static method. It is able to access the instance variables by creating the object to the particular class.
The code can be provided as follows:
public class ClassA
{
private int x=10, y=20;
public void add()
{
System.out.println("Sum of x and y is: " +(x+y));
}
public static void main(String args[])
{
ClassA a=new ClassA();
a.add();
System.out.println("The values of instance variable is x="+a.x);
System.out.println("The values of instance variable is y="+a.y);
System.out.println("Sum of the instance variables is: "+(a.x+a.y));
}
}
Sample output:
Sum of x and y is: 30
The values of instance variable is x=10
The values of instance variable is y=20
Sum of the instance variables is: 30
--------------------------------------------------------------------------------------------------------------------------
2)
Yes, static variables can be accessed by the instance methods but either through the class name dot static variable or through the object reference of the class followed by dot and the static variable.
For example,
Consider the ClassB consists of the static variables x and y. Within the class, they are accessed directly in the instance method.
When the static variables are called from the other class, say ClassA here. They need to be called either by class name or through the instance of the ClassB.
Direct accessing of the static variables within the class is shown is as follows:
class ClassB
{
static int x=10, y=20;
public void add()
{
System.out.println("Sum of x and y is: " +(x+y));
}
}
Accessing of the static variables outside the class is shown is as follows:
In the method sub(), the static variables are called through the class name.
In the main method, they are called through the instance of the classB.
public class ClassA
{
public void sub()
{
System.out.println("Difference of x and y is: "+(ClassB.y-ClassB.x));
}
public static void main(String args[])
{
ClassB b=new ClassB();
ClassA a= new ClassA();
b.add();
a.sub();
System.out.println("The values of instance variable is x="+b.x);
System.out.println("The values of instance variable is y="+b.y);
System.out.println("Sum of the instance variables is: "+(ClassB.x+ClassB.y));
}
}
Sample Output:
Sum of x and y is: 30
Difference of x and y is: 10
The values of static variable is x=10
The values of static variable is y=20
Sum of the static variables is: 30
-----------------------------------------------------------------------------------------------------------------------------------------------------
3)
The initialization of the ClassB member variable can be done from ClassA is as follows:
public class ClassB {
String str;
public ClassB(String abc)
{
str=abc;
}
}
public class ClassA {
ClassB myBobj;
public ClassA(String abc)
{
myBobj = new ClassB1(abc);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
4)
A)
The statement b, that is BaseClass bcobj = scobj; is a legal statement and there is no need of casting required.
B)
SomeClass scobj2 = bcobj;
In this statement, typecast the bcobj reference to SomeClass explicitly, so that the BaseClass is converted to appropriate (SomeClass) type. The conversion is shown in below statement as follows:
SomeClass scobj2 = (SomeClass)bcobj;
C)
No, the BaseClass cannot access members and methods of the derived class. Anyway, a derived class can access to public members and methods of the BaseClass.
-----------------------------------------------------------------------------------------------------------------------------------------------------
5)
In inheritance concept, when a class Y wants to inherit the properties of class X, then by using a keyword “extends” the properties of the class X.
In other words, class X becomes super class and class Y becomes subclass. That means,
Subclass can inherit the properties of Superclass. The syntax is as follows:
class X
{
//variables and methods of class X
}
class Y extends X
{
//variables and methods of class Y
}
--------------------------------------------------------------------------------------------------------------------------------------------------
6)
Difference between the C++ and Java inheritance is as shown below:
C++ inheritance
Java inheritance
In this concept, the inheritance is achieved by multiple classes at a time.
In this concept, the inheritance is achieved by single tree process.
In this, if the super class variables or methods are private then they cannot be accessed in the subclass.
In this, the however the superclass private variables or methods cannot be accessed directly.
In this, no key word is used unlike Java.
In this, extends keyword is used to access the super class.
In C++, abstract or interface key words are not used to for respective methods or functions.
In Java, abstract and interface key words are used to mention the methods and functions.
In C++, multiple inheritances are possible.
In Java, multiple inheritances are not possible, but through the interface it is possible.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
7)
In Java, “super” keyword is used to access the super class or base class members from the sub class or derived class. By using the super key word, the instance variables of the super class variables can be modified or initialized.
For example,
super(x, y);
In the above statement, it automatically calls the parameterized constructor of the super class.
It is basically used to call the constructor of the superclass. Whenever the object is created to the subclass and if it calls the constructor of super class, then during the object creation to the subclass, the parameters to the super class are also needed to be passed.
For example,
class ClassB1 {
int a;
ClassB1(int x)
{
a=x;
System.out.println("Super class " +a);
}
}
class ClassAB extends ClassB1
{
String myBobj;
public ClassAB(int x,String abc)
{
super(x);
myBobj = abc;
}
void printAB()
{
System.out.println("Call super class variable "+super.a);
System.out.println("Call Subclass "+myBobj);
}
}
public class ClassA1
{
public static void main(String args[])
{
ClassAB ab=new ClassAB(20,"Hello");
ab.printAB();
}
}
Sample Output:
Super class 20
Call super class variable 20
Call Subclass Hello
Note:
Whenever a super keyword is used, especially calling the super class constructor, the calling of methods or variables using super keyword are meant to be mentioned first in the order.
---------------------------------------------------------------------------------------------------------------------------------------------------------
8)
Access specifiers in Java are public, protected, and private.
The public access specifier: When specified, the respective class or variables or methods can be accessed anywhere in the program/package.
The protected access specifier: When specified, the respective class or variables and methods scope will be within the package only. They cannot be accessed in other packages. But, when a subclass of the other package inherits the particular class of another package, then the access to the methods and variables of the inherited class will be provided.
The protected access specifier: When specified, the respective scope of the variables and methods will be available within the specified class only.
-------------------------------------------------------------------------------------------------------------------------------------------------------
9)
Whenever explicitly modifiers are specified for the class or its members then scope of the private package of such class members are only accessible within a class itself and the package which contains this class.
In the below code, Class A belongs to pack1 package and it is protected. In the class C code it belongs to pack2 package, but still it is able to access the class A because of import statement.
package pack1;
protected class A
{
protected int a;
protected void method(){}
}
package pack1;
class B extends A
{
A a=new A();
a.method();
}
package pack2;
import pack1;
class C extends A
{
A a =new A();
a.method();
}
In the above code the access to the class A is provided even it is protected in both the packages and the respective class A package is imported.
Whereas, in the below code it does not allow the access to the members of the class A is shown as follow:
package pack2;
class C {
A a =new A();
a.method();
}
In the above code it displays an error message.
------------------------------------------------------------------------------------------------------------------------------------------------------
10)
By using the final keyword be specified to the derived class in order to prevent the method of a derived class from overridden.
For example,
class A
{
final void add()
{
//method code
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
11)
A class can also be prevented from extending is through the final keyword. A class is needed to be declared as final.
Syntax:
final class A
{}
For example:
final class A
{
void add() { }
}
In the above code, it is concluded that the class A cannot be overridden.
----------------------------------------------------------------------------------------------------------------------------------------------------
Working of dynamic binding in Java:
The run time execution of code is known as Dynamic binding in java. It is used when object is used to resolve method calls at runtime if there are two methods or variables exist in the class hierarchy.
For example,
public class ClassB
{
String str;
public ClassB(String strng)
{
}
public void printMethod()
{
System.out.println(“Super class”);
}
}
public class ClassA extends ClassB {
public ClassA(int a,String param)
{
super(a);
super.a = “abc”;
}
public void printMethod1()
{
System.out.println(“Derived Class”);
}
public static void main(String[] args) {
ClassA objectA= new ClassA(“object A”);
ClassB objectB = objectA; //Dynamic Binding
objectB.printMethod();
}
}
Sample output:
Super class
C++ inheritance
Java inheritance
In this concept, the inheritance is achieved by multiple classes at a time.
In this concept, the inheritance is achieved by single tree process.
In this, if the super class variables or methods are private then they cannot be accessed in the subclass.
In this, the however the superclass private variables or methods cannot be accessed directly.
In this, no key word is used unlike Java.
In this, extends keyword is used to access the super class.
In C++, abstract or interface key words are not used to for respective methods or functions.
In Java, abstract and interface key words are used to mention the methods and functions.
In C++, multiple inheritances are possible.
In Java, multiple inheritances are not possible, but through the interface it is possible.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.