开发者

What is type casting in java?

开发者 https://www.devze.com 2023-03-19 03:17 出处:网络
How is type casting implemented in java in case 开发者_如何学运维of multiple inheritance ? how to access the data member of super class using this keyword.I know type casting is used

How is type casting implemented in java in case 开发者_如何学运维of multiple inheritance ?

how to access the data member of super class using this keyword.I know type casting is used

but don't know how to implement it.Help me!!


There is no multiple inheritance in Java, except for interfaces.

Type casting just consists in saying when using a variable of type A: "I know that this A instance is in fact a B instance. Let me use it as a B". And the type cast only succeeds if the variable effectively point to an instance of B. So multiple inheritance doesn't cause any problem.

There is no need to cast to access a member of a superclass, since an instance of a subclass always is an instance of a superclass.


Java has simple inheritance. So you will never have a problem here... You can solve multiple inheritance issues with the use of interfaces.


In Java you do not have multiple inheritance, just straight inheritance.

So if 'C' extends 'B' which extends 'A' you can cast 'C' to 'B' and 'A', and 'B' to 'A', but not the other way around.


In Java, object typecasting means one object reference can be type cast into another object reference. The cast can be to its own class type or to one of its subclass or superclass types or interfaces. There are compile-time rules and runtime rules for casting in java.

Having said this,

How is type casting implemented in java in case of multiple inheritance ?

In Java, we don't have multiple inheritance, atleast in case of class. We can achieve multiple inheritance through interfaces.

how to access the data member of super class using this keyword.I know type casting is used.

I don't think its necessary to type cast to superclass. Remember, the method to be called depends on the actual subtype of an object.

The "this" keyword is simply a reference to the current object. "this" is useful when you need to refer to instance of the class from its method.

From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation, the invocation of another constructor must be the first line in the constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消