0% found this document useful (0 votes)
2 views20 pages

Java Seminar

The document explains abstract classes, abstract methods, and interfaces in Java, emphasizing that abstract classes cannot be instantiated and must be implemented by subclasses. It outlines the concept of abstraction, which hides implementation details and focuses on functionality, and provides examples of abstract classes and methods. Additionally, it discusses the differences between abstract classes and interfaces, as well as the rules for defining and implementing them.

Uploaded by

Ananya N G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Java Seminar

The document explains abstract classes, abstract methods, and interfaces in Java, emphasizing that abstract classes cannot be instantiated and must be implemented by subclasses. It outlines the concept of abstraction, which hides implementation details and focuses on functionality, and provides examples of abstract classes and methods. Additionally, it discusses the differences between abstract classes and interfaces, as well as the rules for defining and implementing them.

Uploaded by

Ananya N G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ABSTRACT CLASSES,

ABSTRACT METHODS
AND INTERFACES
ABSTRACT CLASSES
A class that is declared with abstract keyword, is
known as abstract class in java. It can have
abstract and non-abstract methods (method with
body).
Syntax:
1. public abstract class Shape{

public abstract void Draw( ); // Abstract


method without definition
public void Display( ) {
System.out.println(“Display Method
of Shape class”);
}
ABSTRACTION IN JAVA
 Abstraction is a process of hiding the implementation
details and showing only functionality to the user.
 Another way, it shows only important things to the user

and hides the internal details.


for example: sending sms, you just type the text and
send the message. You don't know the internal processing
about the message delivery.
 Abstraction lets you focus on what the object does instead

of how it does it.


Ways to achieve Abstraction
 There are two ways to achieve abstraction in java

 Abstract class (0 to 100%)

 Interface (100%)
ABSTRACT CLASSES
 A class must be declared abstract if any of the following
condition is true

- The class has one or more abstract methods

- The class inherits one or more abstract methods for


which it does not provide implementation

- The class declares that it implements an interface but


does not provide implementation for every method of
that interface
ABSTRACT CLASSES
 An abstract class cannot be instantiated
Abstractclasses defer the
implementation to subclasses
The subclass must provide the
implementation of the abstract method
or declare itself to be abstract in which
case the implementation is deferred
once again
 Youcan declare a variable/reference of an
abstract class
EXAMPLE

public abstract class LivingThing {


public void breath( ){
System.out.println("Living Thing
breathing..."); }
public void eat( ){
System.out.println("Living Thing eating...");
}
public abstract void walk( );
public
} class Human extends LivingThing {

public void walk( ){


System.out.println("Human walks..."); }
}
1. abstract class Shape{
2. abstract void draw();
3. }
4. class Rectangle extends Shape{
5. void draw( ){ System.out.println("drawing rectangle"); }
6. }
7. class Circle1 extends Shape{
8. void draw( ){ System.out.println("drawing circle"); }
9. }
10.

11. class TestAbstraction1{


12. public static void main(String args[]){
13. Shape s=new Circle1( );
14. s.draw( ); If you are extending any abstract class
that have abstract method, you must
15. } either provide the implementation of
16. } the method or make this class abstract.
GeometricObject Abstract class
-color: String
-filled: boolean
-dateCreated: java.util.Date
The # sign indicates
protected modifier #GeometricObject()
#GeometricObject(color: string,
filled: boolean)
+getColor(): String
+setColor(color: String): void
+isFilled(): boolean
+setFilled(filled: boolean): void
+getDateCreated(): java.util.Date
+toString(): String
+getArea(): double
Abstract methods +getPerimeter(): double
are italicized Methods getArea and getPerimeter are overridden in
Circle and Rectangle. Superclass methods are generally
omitted in the UML diagram for subclasses.

Circle Rectangle
-radius: double -width: double

+Circle() -height: double


+Circle(radius: double) +Rectangle()
+Circle(radius: double, color: string, +Rectangle(width: double, height: double)
filled: boolean) +Rectangle(width: double, height: double,
+getRadius(): double color: string, filled: boolean)
+setRadius(radius: double): void +getWidth(): double
8
+getDiameter(): double +setWidth(width: double): void
+getHeight(): double
+setHeight(height: double): void
ABSTRACT METHOD IN ABSTRACT CLASS

• An abstract method cannot be contained in a non-


abstract class.
• If a subclass of an abstract superclass does not
implement all the abstract methods, the subclass must
be defined abstract.
• In other words, in a non-abstract subclass extended
from an abstract class, all the abstract methods must
be implemented, even if they are not used in the
subclass.
OBJECT CANNOT BE CREATED FROM ABSTRACT
CLASS

• An abstract class cannot be instantiated using the new


operator, but you can still define its constructors,
which are invoked in the constructors of its subclasses.

• For instance, the constructors of GeometricObject are


invoked in the Circle class and the Rectangle class.
ABSTRACT CLASS HAVING CONSTRUCTOR,
DATA MEMBER, METHODS ETC.
1. abstract class Bike{
2. Bike( ){
3. System.out.println("bike is created"); }
4. abstract void run();
5. void changeGear( ){
6. System.out.println("gear changed"); }
7. }
8. class Honda extends Bike{
9. void run(){
10. System.out.println("running safely.."); }
11. }
12. class TestAbstraction2{
13. public static void main(String args[ ]){
14. Bike obj = new Honda();
15. obj.run();
16. obj.changeGear();
ABSTRACT CLASS WITHOUT ABSTRACT
METHOD

• A class that contains abstract methods


must be abstract.
• However, it is possible to define an
abstract class that contains no abstract
methods.
• In this case, you cannot create
instances of the class using the new
operator. This class is used as a base
class for defining a new subclass.
SUPERCLASS OF ABSTRACT CLASS MAY
BE CONCRETE

• A subclass can be abstract even if its superclass is


concrete.
• For example, the Object class is concrete, but its
subclasses, such as GeometricObject, may be
abstract.
DEFINING A INTERFACE
IMPLEMENTING A INTERFACE
DIFFERENCE BETWEEN ABSTRACT CLASS AND
INTERFACE
THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy