public class BaseClass { // methods of BaseClass } public SomeClass extends Base
ID: 3584365 • Letter: P
Question
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.
Explanation / Answer
1 & 2.
Static methods can refer to instance variables and methods, as long as they are static as well. The reason you cannot mix nonstatic and static class members is because they have different scope. Static members are independent of any particular object, whereas nonstatic members are unique for each object that is instantiated. To clarify, consider the following class:
class A
{
int x=0;
static int y=1;
}
If three instances of A are created, 3 instances of x will also be created. Changing one of those x's has no effect on the others. However, only one instance of y will be created, regardless of how many A's are ever created. A change in y will be reflected in every A.
Now, if you were to call A.y+=x, which x would you be referring to? 3 A's have been created, each with possibly different values of x. Because of the ambiguity of this, you will get a compiler error whenever you mix static and nonstatic members.
5. http://www.informit.com/articles/article.aspx?p=1021579
6. Differences between the C++ and Java programming languages
The differences between the C++ and Java programming languages can be traced to their heritage, as they have different design goals.
* C++ was designed mainly for systems programming, extending the C programming language. To this procedural programming language designed for efficient execution, C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library which includes generic containers and algorithms.
* Java was created initially to support network computing. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to C, but is not compatible with it. It was designed from scratch, with the goal of being easy to use and accessible to a wider audience.
8.
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:
1. Public
2. Protected
3. Default and
4. Private
Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.
Public Members:
If a variable or method is declared public, it means that it can be accessed by anyone or any other class (irrespective of which package they are in). Of course, it is mandatory that the class inside which the public method is placed is visible in the first place for the method or variable to be visible. You can check out the code example in the previous paragraph for an example. The method from Dad class is visible and available for usage inside the son class.
For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.
Ex:
package pack1;
public class Parent {
public String getName() {
return "Parent";
}
}
Package pack2;
Import pack1.Parent;
Public class Child extends Parent{
Public String getParentsName() {
return getName();
}
}
If you see the example above, the child class is able to access the parent class's method getName() even without instantiating an object of the parent because it inherits the Parent class and all its method as part of the inheritance (extends) feature.
Protected and Default Members:
The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
Take a look at the following two classes:
package certification;
public class ClassOne {
void testIt() { // No modifier means method has default access
System.out.println("ClassOne");
}
}
In another source code file you have the following:
package otherCertification;
import certification.ClassOne;
class ClassTwo {
static public void main(String[] args) {
ClassOne o = new ClassOne();
o.testIt();
}
}
As you can see, the testIt() method in the first file has default (think: package-level) access. Notice also that class OtherClass is in a different package from the AccessClass. When you compile the ClassTwo.java file you will get an error like below:
No method matching testIt() found in class
certification.ClassOne.o.testIt();
From the preceding results, you can see that AccessClass can't use the OtherClass method testIt() because testIt() has default access, and AccessClass is not in the same package as OtherClass. So AccessClass can't see it, the compiler complains.
Default and protected behavior differs only when we talk about subclasses. If the protected keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance. It doesn't matter if the superclass and subclass are in different packages, the protected superclass member is still visible to the subclass. This is in contrast to the default behavior, which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass. (See the example above)
Whereas default access doesn't extend any special consideration to subclasses, the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think of package restrictions. No exceptions at all. But when you think protected, think package + kids. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package.
12. http://geekexplains.blogspot.in/2008/06/dynamic-binding-vs-static-binding-in.html
Note :- please ask question in smaller parts so that we can help you more easily.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.