
Can you explain please? Also, what does parantheses mean, for example printName ((Parakeet) parrot1);
The parantheses in printName((Parakeet)parrot1) is used for casting where parrot type is being down casted to the type parakeet. To understand this first lets understand what is casting
Casting is converting one type to another type. It doesnot change the objects but only manipulates references. It is of 2 types :
1. Up casting is casting a child type to a parent type i.e upwards in inheritance. eg casting parakeet to parrot , Parror parrot = (Parrot) new Parakeet();.Here parrot is a refernce type and Parakeet() is a contructor which is making an object of type Parakeet. Up cating is safe and results in no exceptions as a child will always have all the behaviors of its parents.
2. Down casting is opposite to up casting where the parent type is casted to child type i.e downwards in inheritance, Bird bird = new Parrot(); Parrot parrot = (Parrot) bird;
This operation is not safe as this can result in ClassCastException in java. for eg when Bird bird = new Parrot(); Owl owl= (Owl) bird; as owl is not parrot even though both parrot and owl are birds. If you are going to do downcasting always use instanceof operator to check the type of object being casted.
Option A is correct.
Explanation of answer
option A> printName(parrot2) ..parrot2 reference is pointing to parakeet object. parakeet is also a bird hence calling this method won;'t create any exceptions due to upcating parrot2 to bird. similarly printBirdCall((parrot)bird2) is also safe. Hence Right
option B > printBirdCall(bird2) will give error as it needs to be casted to type parrot. Hence wrong.
option C > same reason as option B . Hence wrong
option D> printName((Parakeet) parrot1) will gove classcastexception as parrot1 cannot be casted to Parakeet
option E> printName((Owl) parrot2) will give error as both are in compatible types . hence wrong
Can you explain please? Also, what does parantheses mean, for example printName ((Parakeet) parrot1); Consider the...
What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...
Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...
Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...
This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...
can someone explain to me what does the dot mean ? and what would be the output and explain it? import java.io.*; public class Green { private int a; private int b; public Green(int aa,int bb) { a=aa; b=bb; } public void equals(Green c) { this.a=c.a;this.b=c.b;} public void fn(Green c) { this.a=3*c.b-c.a; this.b=2*c.a-this.b;} public void gg() { this.b=this.b-1; this.a=this.b-2; } public static void main(String args[]) { Green x=new Green(2,2); Green y=new Green(2,1); Green z=new Green(1,4); int xx=1,yy=2,zz=3; x.fn(y); z.gg(); System.out.println("...
1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...
Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() { super(); array = new Integer[0]; } Array(Array other) { super(); array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) { ...
Please read my question if you post copy and pasted code you will get a negative review. This needs to be done in java In this program, you are going to read in a file, and you are going to parse it onto a stack and determine if it is syntactically correct. You will check for all open and close curly braces, open and closed parentheses, and you will check for all double quotes. You should also allow the user...
PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1. What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** * Program: PRG/421 Week 1 Analyze Assignment * Purpose: Analyze the coding for...
1. What is output by the following code: ArrayList< Integer > a = new ArrayList< Integer >(); ArrayList b = a; a.add(new Integer(4)); b.add(new Integer(5)); a.add(new Integer(6)); a.add(new Integer(7)); System.out.println(b.size()); A)1 B)2 C)3 D)4 E)5 2. Assume the Student and Employee classes each extend the Person class. The Student class overrides the getMoney method in the Person class. Consider the following code: Person p1, p2, p3; int m1, m2, m3; p1 = new Person(); m1 = p1.getMoney(); // assignment 1...