Details of this Paper

COMP274 Week 2 Homework ? Guaranteed 100% score




COMP 274;Week 2 Homework Assignment;Reading;Lecture;Chapter 8: Objects and Classes;Sections 8.3, 8.5, 8.7, 8.8, 8.9, 8.10, 8.11;Chapter 10: Thinking in Objects;Sections 10.4, 10.5, 10.7, 10.11;Chapter 11: Inheritance and Polymorphism;Sections 11.2, 11.3, 11.4, 11.7, 11.8, 11.9, 11.13, 11.14;Create a Word document containing the answers to all the problems given below.;1. Can you access an instance variable from a static method? Explain why or why not.;No. we cannot access an instance variable from a static method because static method does not have any instance to work with.;2. Can you access a static variable from an instance method? Explain why or why not.;Yes we can access static variables from an instance method because a static variable exists for every instance and if it is changed in any instance, it is changed for all other instances accessing it.;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?;This is the code for both classes;public class ClassB;String a;public ClassB(String param);a=param;public class ClassA;ClassB myBobj;public ClassA(String param);myBobj = new ClassB(param);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?;The above statement is legal and no casting is 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.;We need to explicitely cast the bcobj to SomeClass type so that the base class is converted to appropriate type;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.;No, the base class cannot access members and methods of the derived class however a derived class has access to public members and methods of the base class.;5. Give an example using Java syntax of how some class Y can be setup to inherit from some other class X.;public class ClassA extends ClassB;//Code;6. What is a significant difference between C++ and Java inheritance?;We cannot provide inheritance specifier in Java inheritance like public,protected or private which is possible in C++. If a member is public or protected in base class then it will remain public or protected in derived class.;7. How is the keyword super used? Give two different uses for it and include Java examples.;This keyword is used to access base class object. We can use super keyword to call constructor of a base class as follows;super(param);or we can use this keyword to set some member of a base class as follows;super.a = ?abc?;8. Explain the difference between public, private, and protected access specifiers. Give some guidelines of when each should be used.;Public members of a class are directly accessible from the outside world and any object of the class can access these members. Moreover, the class itself can access these members and any subclass can access these members too.;Protected members are only accessible from the class itself, the package which contains the class, and the subclasses. They are not accessible from the outside world.;Private members are only accessible from the class itself and no one else can access them.;9. Explain what package access means and how it is specified.;When no explicit modifier is specified for the class or its members then it is package private which means such class member are only accessible within a class itself and the package which contains this class.;10. What can you do in Java to prevent a method from being overridden in a derived class? Give example code in your answer.;We can use the final keyword before method declaration to stop its overriding as follows;final public void print ();System.out.println(?baseclass?);11. How do you prevent a class from being extended? Give example code in your answer.;We can use a final keyword before class declaration to prevent its inheritance as follows;public final class ClassB;String a;public ClassB(String param);// TODO Auto-generated constructor stub;12. Explain how dynamic binding works in Java.;Dynamic binding in java is done an runtime and it is used when object is used to resolve method calls at runtime. For example, let?s assume we have a base class as follows;public class ClassB;String a;public ClassB(String param);// TODO Auto-generated constructor stub;public void print ();System.out.println(?base class?);Now we create another class, which is inherited from base class;public class ClassA extends ClassB;public ClassA(String param);super(param);super.a = ?abc?;public void print1();System.out.println(?Derived Class?);public static void main(String[] args);ClassA aObj = new ClassA(?aobj?);ClassB bobj = aObj, //Dynamic Binding;In the last line in the main function, an object of derived class is being assigned to an object of a base class. This is resolved during runtime because type of the object is ClassB but its value will be of ClassA.


Paper#68279 | Written in 18-Jul-2015

Price : $22