Question

what is abstract class in java? for example, abstract class animal and subclass cat which extends...

what is abstract class in java?

for example, abstract class animal and subclass cat which extends abstract class, and the test class which will call cat class to show if it yawns, scratch and whatnot.

I used to create class animal but it was not abstract, so I am not sure the purpose of it.

Another question, I created interface, by right clicking the package, I think the teacher saying interface is acts independent? and not part of hierarchy like super class and subclass, but still wants to apply (implment) inside subclass so it can be called from Test class, something like that. What is the interface exactly?

simple explanation works the best! thnx in advance

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Abstract Class:

An abstract class is just a simple class in java, except the following:

  • an abstract class cannot be instantiated, i.e., object of an abstract class cannot be created;
  • an abstract class can also contain abstract methods (abstract methods are those methods which do not have their definition and the child class of that abstract class have to override those abstract methods).

The keyword 'abstract' is used to create an abstract class. Example:

abstract class Animal

{

abstract void speak( );

}

Now, we can create a child class of Animal and can implement the speak( ) method in child class, as below:

class Cat

{

//--overridden speak method

public void speak( )

{

   System.out.println("yawns");

}

}

  • Don't get confused between an abstract class and interface.
  • Abstract class can also contain constructors, data members and member functions, while interface is referred to as a blue print of a class in java.

Interface:

  • Interface is used for full abstraction.
  • It is a blue print of class.
  • By default, the data members of an interface are public, static and final.
  • By default, the member functions of an interface are public and abstract, i.e., the class which will implement an interface have to override the abstract method of interface in it.
  • Interface cannot be instantiated.
  • They cannot have constructors.

To create an interface, following is the syntax:

interface Test

{

   void testMethod( );

}

Now, any class can implement the Test interface and have to override the testMethod() of the interface. Example:

class TestImplementation implements Test

{

   public void testMethod( )

   {

      System.out.println("Override interface method in class");

   }

}

Note that, one interface can extend one or more interface(s).

Add a comment
Know the answer?
Add Answer to:
what is abstract class in java? for example, abstract class animal and subclass cat which extends...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For Java, 1. What is an abstract method? How is an abstract method created? 2. What...

    For Java, 1. What is an abstract method? How is an abstract method created? 2. What is an abstract class? 3. Can an object of an abstract class be instantiated? 4. Does a superclass have access to the members of subclass? Does a subclass have access to the members of the superclass? 5. How do you prevent a subclass from having access to a member of a superclass? 6. Given the following hierarchy: class Alpha{ … class Beta extends Alpha...

  • Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() {...

    Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() { System.out.println("A new animal has been created!"); } public void sleep() { System.out.println("An animal sleeps..."); } public void eat() { System.out.println("An animal eats..."); } } public class Cat extends Animal { public Cat() { super(); System.out.println("A new cat has been created!"); } public void sleep() { System.out.println("A cat sleeps..."); } public void purr() { System.out.println("A cat purrs..."); } public void eat() { System.out.println("An cat eats...");...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    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...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • Examples: Example 1-Runtime Polymorphism: Lets say we have a class Animal that has a method sound()....

    Examples: Example 1-Runtime Polymorphism: Lets say we have a class Animal that has a method sound(). Since this is a generic class so we can't give it a implementation like: Roar, Meow, Oink etc. We had to give a generic message public class Animal public void sound System.out.println("Animal is making a sound"); Now lets say we a subclass of Animal class. Horse that extends Animal class. We can provide the implementation to the same method like this: class Horse extends...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract...

    JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract method in the NoGo.java file. The purpose of this exercise is to get familiar with abstract class and its object creation 4. Follow the steps below: (be sure common on each to score points) 1) 2) 3 points) Create a subclass Go1 with Nogo1 as its super class and create Go1() constructor 3) (2 points) Now inside the NoGo class create an instance of...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • 1.     When a sub class object is created, when is the call to the super class...

    1.     When a sub class object is created, when is the call to the super class constructor made? How does a programmer call the super class constructor from the sub class? What do all classes indirectly extend? What methods does every class inherit from the Object class? 2.     When writing methods in a sub class, how can those methods call the methods from the parent class? 3.     Which class is more specific, a super class or a sub class? 4.    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT