编辑: 颜大大i2 2019-07-10

t Java support multiple inheritance? diamond problem What is wrong with multiple inheritance? Static Binding vs Dynamic Binding (还是有点不太明白 再查查) Static and dynamic binding in java When should inner classes be used in Java? When to use inner class in Java? Are objects of the same type as the inteface implemented What is printed out in the Java code below C true or false? Java基础知识

8 Java Core 面试题 public interface Animal{ public void sleeps( );

} public class Dog implements Animal{ /* } public class SomeClass { public static void main(String [ ] args){ Dog d = new Dog( ) ;

// this checks to see if d is of type Animal if ( d instanceof Animal) System.out.println( true );

else System.out.println( false );

} } In order to understand the code above, you must understand that any time a class implements an interface, this means that when an object of that class is created, it will also have the interface type. This means that in the code above, the d object will be of both the Dog type and the Animal type. This means that the code above will print out true . What is the diamond problem? Does it exist in Java? If so, how can it be avoided? Can an interface extend another interface in Java? Yes, an interface can extend another interface in Java. This is what the code for something like that would look like: // this interface extends from the Body interface: public interface FourLegs extends Body{ public void walkWithFourLegs( );

} Remember that any class that implements an interface must implement the method headings that are declared in that interface. And, if that interface extends from other interfaces, then the implementing class must also implement the methods in the interfaces that are being extended or derived from. So, in the example above, if we have a class that implements the FourLegs interface, then that class must have Java基础知识

9 Java Core 面试题 definitions for any method headings in both the FourLegs interface and the Body interface. Upcasting and Downcasting in Java? Upcasting is casting to a supertype, while downcasting is casting to a subtype. Supercasting is always allowed, but subcasting involves a type check and can throw a ClassCastException. In your case, a cast from from Dog to an Animal is a upcast, because a Dog is-a Animal. In general, you can upcast whenever there is an is-a relationship between two classes. Downcasting would be something like this: Animal animal = new Dog();

Dog castedDog = (Dog) animal;

Basically what you'

re doing is telling the program that you know what the runtime type of the object really is. The program will do the conversion, but will still do a sanity check to make sure that it'

s possible. In this case, the cast is possible because at runtime animal is actually a Dog even though the static type of animal is Animal. However, if you were to do this: Animal animal = new Animal();

Dog dog = (Dog) animal;

You'

d get a ClassCastException. The reason why is because animal'

s runtime type is Animal, and so when you tell the runtime to perform the cast it sees that animal isn'

t really a Dog and so throws a ClassCastException. To call a superclass'

s method you can do super.method() or be performing the upcast. To call a subclass'

s method you have to do a downcast and risk the ClassCastException . What is the differences between references in Java and pointers in C++? References store an address. That address is the address in memory of the object. So, when a class is declared like so: PersonClass y = new PersonClass();

下载(注:源文件不在本站服务器,都将跳转到源网站下载)
备用下载
发帖评论
相关话题
发布一个新话题