Why can a superclass be initialized as a child class in Java?

In short, how and why is this possible:

Object obj=new MyClass(); 

An object is a superclass of all objects, so MyClass is a child of the Object class. In general, in Java, Why can I use the constructor of a child class in a parent class?


I understand how this can happen on the contrary, since the child has all the variables / methods of the parent class, so when you initialize them, you simply initialize the variables specified in the parent constructor that exist by definition in the child. The problem is that when you go the other way around, this is not necessarily the case. A child can have variables that the parent does not have , since you can use a child constructor with a parent when the parent has no variables at all?


What does this feature use in development? I would think that if you need an instance of class B, you declare it as B thing = new B (), not A thing = new B (). This is probably my inexperience, so I would like to get an enlightenment about why and how the parent class can be initialized as one of its children.

+7
java inheritance oop
source share
7 answers

Why can I use the constructor of the child class in the parent class?

This is not true. When you do

 Object obj = new MyClass(); 

Object obj; declares a link of type Object and new MyClass(); returns a link to the created object.

So, you create an instance of MyClass and assign a reference to an object created for a reference of type Object , and this is possible because MyClass is an Object .

How do you say,

A child may have variables that the parent does not have.

This is called an extension of parental functionality ( inheritance ).

For your second question, think of the classic Animal example: suppose you create an Animal class and you create the makeSound() method on it.

Now you create two subclasses of Animal , Dog and Cat , which overrides the makeSound() method of Animal (a Dog barks and a Cat meats).

Imagine you imagine a room full of Animal ( Dog and Cat s) using List , and you want to make all of them makeSound() . Your list will be declared as List<Animal> because you do not know the type of Animal that you will be storing.

And then you makeSound() over the List to call makeSound() for each Animal . It doesn't matter if Animal is Dog or Cat , it will sound.

And then imagine that you want to add Bird to the List . Easy, right?

+6
source share

You think about the semantics of C ++, but this is Java. In Java, all non-primitive type variables are references, not instances.

In C ++, when you say

 Object obj; 

you allocate a new instance of Object on the stack or in static memory.

When you speak

 Object obj = new MyObject; 

you call the constructor of the Object class, which takes a MyObject pointer (or maybe something else that MyObject can be converted to).

In Java,

 Object obj; 

does not instantiate Object . It simply creates a variable that can refer to an instance of Object , but currently does not refer to any. It is initialized to null .

 Object obj = new MyObject(); 

MyObject instance of MyObject . It does not allocate a new instance of Object . It simply sets a variable to refer to a new instance. In C ++ terms, this is much more like

 Object *obj = new MyObject(); 

So, we are not creating the parent instance from the child instance. We change the value to which the variable is set, from zero to a new child instance.

+2
source share

Every class in Java comes from Object . So, MyClass is an Object , by definition, but a more specialized version. Think of it this way: every living thing is an Animal . A Cat is a special kind of animal; a certain type. Since Cat is Animal , you can still just call it Animal :

 Animal a = new Cat(); 

But at the same time, you cannot do something specific for Cat , for example meow() or purr() , but you can call methods that are valid for all Animal s, such as breathe() .

NTN

+1
source share

First, you should get a clear idea of ​​things. Expression of your example: Object obj = new MyClass(); actually a combination of two elementary operations.

The first creates an instance of MyClass: new MyClass() . The new keyword is basically the only way to get an instance of the class (allowing you to ignore reflection at runtime to make it simple), and you literally call what you want to create (MyClass) here as your constructor. It is impossible to create anything other than what you literally called a new keyword. The result of the new is an (implicit) instance of MyClass, but the explicit result of new X is a reference of type X (the reference refers to a newly created instance).

Now the second operation assigns a link to your (new) MyObject to another link of type Object. And this is true because MyObject is an object (due to inheritance).

Why do you need this?

This is an important function that actually uses polymorphism. The ability to refer to any child class as its superclass is what makes polymorphism so powerful. You will mainly use it wherever there is a common aspect for the two classes, but there are differences.

An example of the real world is graphical user interfaces. The window has buttons, lists, tables and panels that are elements of the user interface, but each one does a different thing. To present them neatly organized in a window, these elements are often inserted into panels that talk more abstractly into containers. Now the container does not care what elements are included in it, if they are components. But for the proper handling of the container, some basic information about these components is needed, mainly how much space they take and how to draw them. So this is modeled something like:

 public abstract class Component { public int getWidth() { ... } public int getHeight() { ... } public void paint(Graphics g) { ... } } public class Container extends Component { public void add(Component child) { ... } public void paint(Graphics g) { for (Component child : children) { child.paint(g); } } } 

What came almost directly from the JDK, the fact is that if you need to refer to each component as its specific type, it would be inappropriate to create a container, this will require additional code for each component that you decide to make (for example, addButton, addTable, etc.). Therefore, instead, Container only works with reference to Component. Regardless of which component is created (e.g. Button, CheckBox, RadioButton, etc.), since the Container just relies on them to be components, they can handle them.

+1
source share

Because MyClass is an Object . Note that java is special because Object is a superclass of any other type of class (in C ++ there is no equivalent).

A more interesting example might be if you have a class or interface and one or more subclasses. It appears all the time in OOD. Consider, for example, the java jdbc API : a common set of interfaces for connecting and querying a database, which can be implemented by various specific classes. You only need to code the API, and then at runtime use the implementation for your database.

0
source share

enter image description here

 http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html 

The Object class is the root of the class hierarchy. Each class has an object as a superclass. All objects, including arrays, implement the methods of this class.

i.e. every Java class is an Object . That's why.

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

The Object class, defined in the java.lang package, defines and implements the behavior common to all classes, including the ones you write. On the Java platform, many classes are derived directly from Object, other classes are derived from some of these classes, etc., forming a class hierarchy.

0
source share

Here you have two things:

  • Building a new instance
  • The purpose of this instance is variable.

Since your MyClass instance is also an Object instance, this works well.

Consider the following general situation:

 class A extends B implements C,D { } 

Since your A is B , as well as C and D and Object , once you have created the instance, you can (directly or indirectly) assign it to the variables of all these types:

 A a = new A(); B b = a; C c = a; D d = a; Object o = a; 

Your representation in fields or methods is limited by the type of the variable (iE as a variable of type C, you only see methods declared by C). However, your instance always has the type that you created using the constructor, regardless of the type of variable.

0
source share

All Articles