Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a java program that runs on the Eclipse. Java Programming 2-2: Java Clas

ID: 3849525 • Letter: T

Question

This is a java program that runs on the Eclipse.

Java Programming 2-2: Java Class Design Abstract Classes Practice Activities Lesson objectives Use Abstract Classes se the instanceof operator to compare object types Use virtual method invocation Use upward and downward casts Vocabulary: Identify the vocabulary word for each definition below. The type of casting that changes a generalized object to a more specialized object type. The process of a call to a generalized method and actually calls the instantiated subclass method, or appropriate subclass method. The operator that allows you to compare a class instance against a class type The process of explicitly changing one data type to another data type. A class with an abstract constructor and at least one method that is defined but not implemented. This type of casting changes a specialized object instance into a generalized instance. It doesn't lose any of its detail but you can't access them without downcasting the object to access specialized methods. A constructor without implementation that makes the class restricted in that it cannot create instances. Try It/Solve lt: 1. Give one reason why you might use an Abstract class rather than an Interface 2. Given the following classes public class Animal public void make Noise System out.println talk

Explanation / Answer

Vocabulary:-->

(1)downcasting

(2)overriding

(3)instanceof

(4)explicit typecasting

(5) abstract class

(6)upcasting

(7) abstract class constructor

Q:--> Give one reason why you might use an abstract class rather than an interface.

Ans:--> Abstract class is declared using abstact keyword and it may contain pure abstract method. An abstract class can not be initiated.

with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.

(1)

//In this example Animal is super class and Dog is sub class which inherit the property of Animal class.

(2)

In this line of code we are creating object of Animal class which call the makeNoise() version of Animal class.

So output will be --> talk

In this line of code we are creating the object of Dog class which inherit the Animal class. When we call the makeNoise() method using dog object , It override the makeNoise() method of super class Animal.

So the output will be --> Bark

In this line of code we are implementing typecasting.when we are assigning smaller type to a larger type, there is no requirement of explicit casting.

It convert the animaldog to Dog.

So it calls the makeNoise() version of Dog class.

We can get the Type information using this line of code.

//Output of following lines is

animal is Animal
dog is Animal
animaldog is Animal
animal is Dog

(5) Describe casting

Ans:-->using typecasting we convert one type into another type.

There is a rule in Java Language that classes or interface which shares the same type hierrachy only can be typecasted. If there is no relationship between then Java will throw ClassCastException. Type casting are of two types they are

(6)

Downcast:-->

We are converting a supertype to subtype and there is also relationship .

upcast:-->

Dog dog = new Dog();

Animal anim = (Animal) dog;

It is casting a subtype to supertype.

      Animal animal= new Animal();  animal.makeNoise();  

In this line of code we are creating object of Animal class which call the makeNoise() version of Animal class.

So output will be --> talk

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote