I have two scenarios
Without Extending:
class A { public void print() { System.out.println("Class A"); } }
class B { B() { A obj=new A(); obj.print(); } }
public class Main { public static void main(String[] args) { B obj=new B(); } }
With inheritance:
class A { public void print() { System.out.println("Class A"); } }
class B extends A { B() { A obj=new A(); obj.print(); } }
public class Main { public static void main(String[] args) { B obj=new B(); } }
As both work as same then why do we need to extend another class? And what is the key difference between both methods?
CodePudding user response:
Advantages of inheritance:
1) Reuse of programs
When inheriting behavior from another class, the program code is not rewritten. This is important. Many programmers spend a lot of time reworking already written code. When using OOT, previously written code can be reused.
Another advantage of reusable code is its reliability (the more situations the code is used in, the more error detection possibilities) and low cost, since it is shared by all users of the code.
2) Using common code
When using OOT, the use of common code occurs at several levels. Firstly, customers can use the same classes. Another form of using shared code occurs when two or more classes developed by the same programmer for some project inherit from a single parent class. For example, the classes "Set" and "Array" can be considered as varieties of the data set "Collection". In this case, two or more types of objects share the inherited code. It is written once and is included in the program only in one place.
3) Interface negotiation
If two classes inherit the same ancestor, the inherited behavior will be the same in all cases. That is, objects that are similar in interface should actually be similar. Otherwise, the user will get almost identical objects with completely different behavior.
4) Software components
Inheritance provides programmers with the ability to create reusable software components. Goal: to ensure the development of new applications with minimal writing of new code. For example, libraries.
5) Quick layout
When software is constructed primarily from reusable components, most of the time required for development can be devoted to understanding new unusual parts of the system. In this way, software complexes can be created faster and easier, leading to a programming style known as rapid prototyping or exploratory programming. A prototype system (mock-up) is created, users experiment with it, then a second system is created based on these experiments, experiments are conducted with it, etc.
Such programming is especially beneficial when the goals and requirements for the system are presented very vaguely at the beginning of development.
6) Masking information
A programmer using a software component should only understand its purpose and interface. Information about the technical means used in the implementation of the component is not required at all. For example, working in the NC shell, few people are interested in what it is implemented on. Thus, the need for internal communications between software systems is reduced.
7) Polymorphism and structure
Software has traditionally been created from the bottom up. That is, first lower–level programs were written, more abstract elements were built on their basis, then even more abstract ones. This process is similar to building a house.
Usually, code mobility decreases with increasing abstraction, that is, lower-level programs can be used in several different projects, and even, perhaps, next-level abstractions can be reused, but top-level programs are closely related to a specific application.
Lower-level components can be transferred to a new system, and it often makes sense for them to exist independently.
Top-level components usually make sense (because of their functionality or data dependency) only when they are built on certain lower-level elements.
Polymorphism allows a programmer to create reusable high-level components that can be redesigned for various applications by changing the lower level.