Question

In: Computer Science

7. What are abstract classes and interfaces in Java? What are they used for, and what...

7. What are abstract classes and interfaces in Java? What are they used for, and what are they comprised of? Clearly explain the difference between the two.

Solutions

Expert Solution

Abstract classes in Java

  • They are so many modifiers in java abstarct is one of the access modifier in java and also a keyword
  • "Abstract" means no implementation and no code nothing it just declares method .
  • If the keyword abstract is used infront of class then that class is called as abstract class
  • It provides abstraction but not 100% bcz it may contains the concrete method

Synatax: abstract class classname{}

  • If you want to use it you need to be extends by other class otherwise it has no use.
  • A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended
  • Abstract class can have abstract methods and concrete methods it may or maynot contains the abstract method but any class having abstract method then it must be declared as abstract

AbstractMethod

  • IF a method that doesnot contains any body or if we simply declares the method and not defined it is abstract method and it can be implemented by extending it in subclass

Syntax:

Public abstract returntype methodname();

Example

abstract class Abex{

public void NM(){

System.out.println("Normal method");

}

abstract public void AM();

}

class example extends Abex

public void AM()

{

System.out.println("Implementing Abstarct method in subclass");

}

public static void main(String args[])

{

example obj = new example();

obj.AM();

}

}

Output :   Implementing Abstarct method in subclass

Interface in Java

  • It is the another way to provide abstraction in java and by default interfaces are abstract and provides 100% abstraction
  • It looks like a class but it exactly not ,it can have methods and variables just like a classbut here methods are by default abstract methods and variables in this interface are public static final by default
  • It can bee declared by a keyword "interface"

Syntax interface

interface-name{
void m1();
void m2();
}

Purpose

  • As it provides 100% abstraction all method in that interface doesnt have body
  • If we want to acess the method in interface they have to be implemented in another class
  • A class can implements interface by extending another interface


Example

interface Inter
{
public void m1();
public void m2();
}

class sam implements Inter
{

public void m1()
{
system.out.println("implementation of m1");
}
public void m2()
{
system.out.println("implementation of m2");
}
public static void main(String args[])
{
Inter obj = new sam();
obj.m2();
}
}
Output: implementation of m2

Difference between the two

Abstract Class Interface
It can have both abstract methods and normal methods By default all methods here are abstract
It can be declared by using keyword abstract It can be declared by using keyword interface
It can have final,static,non-final,non-static variables it can only have final and static variables
A class can extend only one abstract class An class can implements by any number of inerfaces

Related Solutions

What are abstract classes? What are interfaces? What is the difference?
What are abstract classes? What are interfaces? What is the difference?
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
What are similarities and differences between abstract classes and interfaces? What are the default modifiers of...
What are similarities and differences between abstract classes and interfaces? What are the default modifiers of their methods and variables? What are allowed modifiers of their methods and variables? Essay Question
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes,...
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes, major attributes, major methods, class associations and multiplicities. The details of the project and its functional requirements are: Develop a sentiment analysis system for product rating. It is an e-commerce web application. The main objective of this sentiment analysis system is to understand the hidden sentiments of customers in feedback and comments and analyze their product rating patterns. When registered customers (they can create...
What are some of the advantages of creating abstract classes? Are there any potential downsides to...
What are some of the advantages of creating abstract classes? Are there any potential downsides to using parent classes and subclasses?
Java code. You will need to make classes that could be used to stage a battle...
Java code. You will need to make classes that could be used to stage a battle as described below. You have been tasked to create a number of items that can be used in an online game. All items can be used on a game character who will have a name, a number of health-points and a bag to hold the items.   Here are examples of items that can be made: Item type of item effectOnHealth other info Widow's Cloak...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and...
(Implementing Interfaces) Implement the following interface and classes: a. Interface IsCompetition, which has no constants and two methods (see the classes below for details about what these methods should do): i. getSummary(), ii. getCompetitors() b. Class Game: A game object is associated with a home team, away team, home score, away score, and a date of competition. It should only be created using the following constructor: Game(String HTeam, String ATeam, int HScore, int AScore, LocalDate date). A game is also...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT