0% found this document useful (0 votes)
14 views10 pages

Abstract Class

Uploaded by

nikki123456790
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)
14 views10 pages

Abstract Class

Uploaded by

nikki123456790
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/ 10

ABSTRACT CLASS

• “Abstraction” is a process of hiding the


implementation details and showing
only functionality to the user.

INTRODUCTION • It shows only essential things to the user


and hides
the internal details.
• There are two ways to achieve
• Abstractclass
abstraction in java (0 to 100%)
• Interface (100%)
• An abstract class is a class that is declared ”abstract”
it may or may not include abstract methods.
• A method which is declared as “abstract” and
does not have implementationis known as
an abstract method.
• Syntax :
ABSTRACT CLASS &
abstract class className
METHOD {
abstract dataType methodName();
} class childClass extends className
{
dataType methodName(){}
}
• An abstract class must be declared with an
abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
POI NTS • It can have constructors and static methods also.
TO • It can have final methods which will
force the subclass not to change the body
REMEMBER of the method.
• Any class which contains an abstract method
must also be abstract.
abstract class Animal //Abstract Class Declaration
{
public abstract void sound(); //Abstract Method Declaration
}
public class Dog extends Animal //Dog inherits from
{ Animal
public void sound()
{
System.out.println("Woof");
}
public static void main(String args[])
{
Animal obj = new Animal();
obj.sound();
}
}

OUTPUT : Woof
abstract class MyClass
{
p u b l i c void d i s p ( )
{
System.out.println("Concrete method o f parent c l a s s " ) ;
}
abstract p u b l i c void d i s p 2 ( ) ;
}
class Demo extends MyClass
{
p u b l i c void d i s p 2 ( )
{
S y s t e m . o u t . p r i n t l n ( " ab s t r ac t method has
implemented");
}
p u b l i c s t a t i c void main(String a r g s [ ] )
{
Demo obj = new Demo();
obj.disp2();
}
}
OUTPUT : overriding abstract
method
USE OF ABSTRACT
CLASS
• To share code among several closely related classes.
• If classes that extend your abstract class have many common
methods or fields or require access modifiers other than
public (such as protectedand private).
• You want to declare non-static or non-final fields. This
enables you to define methods that can access and modify
the state of the object to which they belong.

You might also like