java inheritance

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is dynamic binding?

A method may be implemented in several classes along the inheritance chain. The JVM decides which method is invoked at runtime. This is known as dynamic binding.

How does a subclass invoke its superclass's constructor?

A subclass can explicitly invoke a suplerclass's constructor using the super keyword.

Show the output of following program: public class Test { public static void main(String[] args) { Apple a = new Apple(); System.out.println(a); System.out.println("---------------"); GoldenDelicious g = new GoldenDelicious(7); System.out.println(g); System.out.println("---------------"); Apple c = new GoldenDelicious(8); System.out.println(c); } } class Apple { double weight; public Apple() { this(1); System.out.println("Apple no-arg constructor"); } public Apple(double weight) { this.weight = weight; System.out.println("Apple constructor with weight"); } @Override public String toString() { return "Apple: " + weight; } } class GoldenDelicious extends Apple { public GoldenDelicious() { this(5); System.out.println("GoldenDelicious non-arg constructor"); } public GoldenDelicious(double weight) { super(weight); this.weight = weight; System.out.println("GoldenDelicious constructor with weight"); } @Override public String toString() { return "GoldenDelicious: " + weight; } }

Apple constructor with weight Apple no-arg constructor Apple: 1.0 --------------- Apple constructor with weight GoldenDelicious constructor with weight GoldenDelicious: 7.0 --------------- Apple constructor with weight GoldenDelicious constructor with weight GoldenDelicious: 8.0

Show the output of following program: 1 public class Test { 2 public static void main(String[] args) { 3 A a = new A(3); 4 } 5 } 6 7 class A extends B { 8 public A(int t) { 9 System.out.println("A's constructor is invoked"); 10 } 11 } 12 13 class B { 14 public B() { 15 System.out.println("B's constructor is invoked"); 16 } 17 }

B's constructor is invoked A's constructor is invoked

What are the three pillars of object-oriented programming? What is polymorphism?

Encapsulation, inheritance, and polymorphism. polymorphism means that a variable of a supertype can refer to a subtype object.

Identify the errors in the following code. ArrayList<String> list = new ArrayList<>(); list.add("Denver"); list.add("Austin"); list.add(new java.util.Date()); String city = list.get(0); list.set(3, "Dallas"); System.out.println(list.get(3));

Error 1: list.add(new java.util.Date()); is wrong, because it is an array list of strings. You cannot add Date objects to this list. Error 2: list.set(3, "Dallas"); is wrong because there is no element at index 3 in the list. Error 3: list.get(3) is wrong because there is no element at index 3 in the list.

A final class can be extended.

False

A final method can be overridden.

False

A protected datum or method can be accessed by any class in different packages.

False

True or false? When invoking a constructor from a subclass, its superclass's no-arg constructor is always invoked.

False. If a subclass's constructor explicitly invoke a superclass's constructor, the superclass's no-arg constructor is not invoked.

True or false? You can override a private method defined in a superclass.

False. You can only override accessible instance methods.

True or false? You can override a static method defined in a superclass.

False. You can only override accessible instance methods.

True or false You can always successfully cast an instance of a superclass to a subclass.

False. You cannot always successfully cast an instance of a superclass to a subclass. For example, casting fruit to apple is not always succeful unless a fruit is an apple.

True or false? A subclass is a subset of a superclass.

False. A subclass is an extension of a superclass and normally contains more details information than its superclass.

(a) package p1; public class A { ? int i; ? void m() { ... } } (b) package p2; public class B extends A { public void m1(String[] args) { System.out.println(i); m(); } }

If the question marks are replaced by blanks, can class B be compiled? No. If the question marks are replaced by private, can class B be compiled? No. If the question marks are replaced by protected, can class B be compiled? Yes.

(a) package p1; public class A { ? int i; ? void m() { ... } } (b) package p1; public class B extends A { public void m1(String[] args) { System.out.println(i); m(); } }

If the question marks are replaced by blanks, can class B be compiled? Yes. If the question marks are replaced by private, can class B be compiled? No. If the question marks are replaced by protected, can class B be compiled? Yes.

What is the benefit of using the @Override annotation? It forces the compiler to check the signature of the overridden method to ensure that the method is defined correctly.

It forces the compiler to check the signature of the overridden method to ensure that the method is defined correctly.

If a method in a subclass has the same signature as a method in its superclass with the same return type, is the method overridden or overloaded?

It is method overridden.

If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem?

It will be a problem if the return type is not a compatible return type.

What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 Integer[] list1 = {12, 24, 55, 1}; 4 Double[] list2 = {12.4, 24.0, 55.2, 1.0}; 5 int[] list3 = {1, 2, 3}; 6 printArray(list1); 7 printArray(list2); 8 printArray(list3); 9 } 10 11 public static void printArray(Object[] list) { 12 for (Object o: list) 13 System.out.print(o + " "); 14 System.out.println(); 15 } 16 }

Line 8 causes a compile error, because int[] cannot be passed to Object[].

Explain the difference between method overloading and method overriding.

Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses.

What is wrong in the following code? 1 public class Test { 2 public static void main(String[] args) { 3 Object fruit = new Fruit(); 4 Object apple = (Apple)fruit; 5 } 6 } 7 8 class Apple extends Fruit { 9 } 10 11 class Fruit { 12 }

Object apple = (Apple)fruit; Causes a runtime ClassCastingException.

Show the output of the following code: public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { private String getInfo() { return "Student"; } } class Person { private String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

Person Person

. Show the output of the following code: (a) public class Test { public static void main(String[] args) { new Person().printPerson(); new Student().printPerson(); } } class Student extends Person { @Override public String getInfo() { return "Student"; } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } }

Person Student

What is single inheritance? What is multiple inheritance? Does Java support multiple inheritance?

Single inheritance allows a subclass to extend only one superclass. Multiple inheritance allows a subclass to extend multiple classes. Java does not allow multiple inheritance.

Describe the difference between method matching and method binding.

The declared type of the reference variable decides which method to match at compile time. The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compile time. A method may be implemented in several subclasses. The JVM dynamically binds the implementation of the method at runtime, decided by the actual class of the object referenced by the variable.

What problem arises in compiling the program in (b)? class A { public A(int x) { } } class B extends A { public B() { } } public class C { public static void main(String[] args) { B b = new B(); } }

The default constructor of B attempts to invoke the default of constructor of A, but class A's default constructor is not defined.

What keyword do you use to define a subclass?

The extends keyword is used to define a subclass that extends a superclass.

What is the output of running the class C in (a)? class A { public A() { System.out.println("A's no-arg constructor is invoked"); } } class B extends A { } public class C { public static void main(String[] args) { B b = new B(); } }

The output is A's no-arg constructor is invoked

A final class can have instances.

True

A protected datum or method can be accessed by any class in the same package.

True

A protected datum or method can be accessed by its subclasses in any package.

True

True or false You can always successfully cast an instance of a subclass to a superclass.

True. You can always successfully cast an instance of a subclass to a superclass. for example, casting apple to fruit is fine.

How do you explicitly invoke a superclass's constructor from a subclass?

Use super() or super(args). This statement must be the first in the constructor in the subclass.

How do you invoke an overridden superclass method from a subclass?

Use super.method() or super.method(args).

How do you prevent a class from being extended? How do you prevent a method from being overridden?

Use the final keyword.

In the following code, the classes A, B, and Main are in the same package. Can the Main class be compiled? class A { protected void m() { } } class B extends A { } class Main { public void p() { B b = new B(); b.m(); } }

Yes

Does every object have a toString method and an equals method? Where do they come from? How are they used? Is it appropriate to override these methods?

Yes, because these two methods are defined in the Object class; therefore, they are available to all Java classes. The subclasses usually override these methods to provide specific information for these methods.

What modifier should you use on a class so that a class in the same package can access it, but a class in a different package cannot access it?

default visibility modifier.

Show the output of following program: public class Test { public static void main(String[] args) { new A(); new B(); } } class A { int i = 7; public A() { setI(20); System.out.println("i from A is " + i); } public void setI(int i) { this.i = 2 * i; } } class B extends A { public B() { System.out.println("i from B is " + i); } public void setI(int i) { this.i = 3 * i; }

i from A is 40 i from A is 60 i from B is 60

If a method in a subclass has the same name as a method in its superclass with different parameter types, is the method overridden or overloaded?

method overloading

Can you assign new int[50], new Integer[50], new String[50], or new Object[50], into a variable of Object[] type?

new int[50] cannot be assigned to into a variable of Object[] type, but new Integer[50], new String[50], or new Object[50] are fine.

What modifier should you use so that a class in a different package cannot access the class, but its subclasses in any package can access it?

protected

. Identify the problems in the following code: public class Circle { private double radius; public Circle(double radius) { radius = radius; } public double getRadius() { return radius; } public double getArea() { return radius * radius * Math.PI; } } class B extends Circle { private double length; B(double radius, double length) { Circle(radius); length = length; } @Override public double getArea() { return getArea() * length; } }

{ radius = radius; // Must use this.radius = radius } class B extends Circle { Circle(radius); // Must use super(radius) length = length; // Must use this.length = length } public double getArea() { return getArea() * length; // super.getArea() }


Set pelajaran terkait

microecon - chapter 8 (Utility & demand)

View Set

MKTG Research exam 2 ch. 12 quiz

View Set

Psych 111 - Chapter 1 Practice Quizzes

View Set

Chapter 4: Intro to Criminal Justice

View Set

Unit 3, Lesson 1: What is matter made of?

View Set