Question

In: Computer Science

c. Why declaring data as protected doesn’t serve any meaningful purpose? e. How does constructor calling...

c. Why declaring data as protected doesn’t serve any meaningful purpose?

e. How does constructor calling works in an inheritance hierarchy?

In Java. Please answer give thorough answers 200-300 words

Solutions

Expert Solution

c: While declaring data as private can only be used by the class in which they are declared whereas protected data can be used in the subclass and also in the same package.So the thing is Protected acts as public within the class and its subclasses, but is hidden outside of those two cases. I'd say the disadvantage to protected is that people can subclass your class and create public getters/setters for it, therefore removing the intention of the access modifier.

e. Calling a constructor from the another constructor is known as Constructor chaining.The end of the chain will always be the Object's class constructor because every class is inherited from the Object class by default.In Java, constructor of base class with no argument gets automatically called in derived class constructor.For example:

class A

{

A()

{

System.out.println("Base class constructor");

}

}

class B extends A{

B(){

System.out.println("Derived class constructor");

}

}

public class abcmain{

public static void main(String args[])

{

B b=new B();

}

}

Here the output will be Base Class constructor and then Derived class constructor So it shows that base class constructor gets automatically called.But if we want to call parameterized constructor of base class then we have to use super() in derived class constructor and point to note is that super() command must be the first line in derived class constructor else it won't work.We can take an example to understand this,For example:

class A

{

A(int a)

{

System.out.println("Base class parameterized constructor");

}

}

class B extends A{

B(int x, int y){

int a=x;

super(a);

System.out.println("Derived class parameterized constructor");

}

}

public class abcmain{

public static void main(String args[])

{

B b=new B(5,4);

}

}

Now here the output will be Base class parameterized constructore and then Derived class parameterized constructor.

I hope you understood the concept.Thank you


Related Solutions

Why is it important to develop hypotheses for a study? What purpose does a hypothesis serve...
Why is it important to develop hypotheses for a study? What purpose does a hypothesis serve and how could it be tested?
Define big data and data mining. What purpose does collecting huge amounts of data serve? Consider...
Define big data and data mining. What purpose does collecting huge amounts of data serve? Consider Twitter. Do you believe big data is accurate and reliable? Why or why not? What type(s) of sampling methods could be used with big data? What sampling errors could occur and how could they be avoided? Has collecting big data been helpful for businesses? Why or why not? What do you see happening in the future with big data?
Question Define big data and data mining. What purpose does collecting huge amounts of data serve?...
Question Define big data and data mining. What purpose does collecting huge amounts of data serve? Consider Twitter. Do you believe big data is accurate and reliable? Why or why not? What type(s) of sampling methods could be used with big data? What sampling errors could occur and how could they be avoided? Has collecting big data been helpful for businesses? Why or why not? What do you see happening in the future with big data?
What is data fragmentation? What purpose does data fragmentation serve? What is a fat client? What...
What is data fragmentation? What purpose does data fragmentation serve? What is a fat client? What is a thin client? What is a three-tier architecture? What is data warehouse? Who in a company should benefit the most from the data warehouse?
"How is Strategic HR planning part of leadership and what purpose does it serve?"
"How is Strategic HR planning part of leadership and what purpose does it serve?"
1. Describe how is the CPI derived and what purpose does it serve? 2. What is...
1. Describe how is the CPI derived and what purpose does it serve? 2. What is the difference between the CPI and the GDP Deflator? 4. How does inflation affect society and who are the losers and gainers from inflation? 5. Define demand pull inflation and cost push inflation. 6. The salary of the president of the United States in 2000 was $400,000. In 1940, the president's salary was $75,000. If the Consumer Price Index was 8.1 in 1940 and...
What is a pilot cell? What purpose does it serve? How do you set up a...
What is a pilot cell? What purpose does it serve? How do you set up a pilot cell?
What is a shelf registration? What purpose does it serve for bond issuers? How have shelf...
What is a shelf registration? What purpose does it serve for bond issuers? How have shelf registrations impacted investment banks? How have shelf registrations impacted bond issuers? (6 points)
What is centripetal acceleration? What is centripetal force? What purpose does centripetal force serve, and how...
What is centripetal acceleration? What is centripetal force? What purpose does centripetal force serve, and how can it be provided?
Are there any controversies or issues H-E-B as a company has faced? How does H-E-B give...
Are there any controversies or issues H-E-B as a company has faced? How does H-E-B give back to the community ? What Career oppotunities does Heb offer?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT