Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Abstract Classes and Abstract Methods

This document provides an overview of abstract classes and methods in Java. It defines an abstract class as a class declared with the abstract keyword that can contain both abstract and non-abstract methods. Abstract classes cannot be instantiated and must be extended by concrete subclasses that implement the abstract methods. The document includes examples of an abstract Bike class with an abstract run() method implemented by a Honda subclass, and shows how abstract classes allow defining common behaviors without fully implementing them.

Uploaded by

Angel Angel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Abstract Classes and Abstract Methods

This document provides an overview of abstract classes and methods in Java. It defines an abstract class as a class declared with the abstract keyword that can contain both abstract and non-abstract methods. Abstract classes cannot be instantiated and must be extended by concrete subclasses that implement the abstract methods. The document includes examples of an abstract Bike class with an abstract run() method implemented by a Honda subclass, and shows how abstract classes allow defining common behaviors without fully implementing them.

Uploaded by

Angel Angel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Abstract Classes & Methods

Compiled By: Aneeta Siddiqui

Lecture # 11

1
Course Books

 Text Books:
 Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
 Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition

2
Course Instructors

 Aneeta Siddiqui aarshad@ssuet.edu.pk


Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,
Topics Covered

 Abstract class in Java


 Abstraction in java
 Abstract Method

4
Abstract class in Java

 A class which is declared with the abstract


keyword is known as an abstract class in Java.
It can have abstract and non-abstract methods
(method with the body).

 Before learning the Java abstract class, let's


understand the abstraction in Java first.

5
Abstraction in Java
 Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
 Another way, it shows only essential things to
the user and hides the internal details,
 For example, sending SMS where you 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.
6
Ways to achieve Abstraction
 There are two ways to achieve abstraction in
java

 Abstract class (0 to 100%)


 Interface (100%)

7
Abstract class in Java

 A class which is declared as abstract is known


as an abstract class.
 It can have abstract and non-abstract methods.
 It needs to be extended and its method
implemented.
 It cannot be instantiated.

8
Abstract class in Java

 Points to Remember
 An abstract class must be declared with an
abstract keyword.
 It can have abstract and non-abstract methods.
 It cannot be instantiated.
 It can have constructors and static methods also.
 It can have final methods which will force the
subclass not to change the body of the method.

9
Abstract class in Java

10
Abstract class in Java

 Example of abstract class

# Example

abstract class A{}

11
Abstract Method in Java

 A method which is declared as abstract and


does not have implementation is known as an
abstract method.

# Example

abstract void printStatus();//no method body and abstract

12
Example of Abstract class that has an
abstract method
 In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

13
Example of Abstract class that has an
abstract method
 In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{ Output:
abstract void run();
}
class Honda4 extends Bike{ running safely
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

14
Example of Abstract class that has an
abstract method
 In this example, Bike is an abstract class that
contains only one abstract method run. Its
implementation is provided by the Honda class.
# Example
abstract class Bike{ Output:
Hence for such
abstract kind of scenarios we generally declare the
void run();
class} as abstract and later concrete classes extend these
class Honda4 extends Bike{ running safely
classes and override the methods
void run(){System.out.println("running accordingly and can have
safely");}
public static void main(String args[]){
their own methods
Bike obj .
as well
= new Honda4();
obj.run();
}
}

15
Abstract class declaration

 An abstract class outlines the methods but not


necessarily implements all the methods.
# Example
//Declaration using abstract keyword
abstract class A{
//This is abstract method
abstract void myMethod();

//This is concrete method with body


void anotherMethod(){
//Does something
}
}

16
Abstract class declaration

 An abstract class outlines the methods but not


necessarily implements all the methods.

# Example # Example
//Declaration using abstract //Declaration using abstract
keyword keyword
abstract class Instrument abstract class
{ StringedInstrument extends
protected String name; Instrument {
abstract public void play(); Protected int numberOfStrings;
} }

17
Abstract class declaration
# Example
Public class ElectricGuitar extends StringedInstrument {
Public ElectricGuitar() {
Super();
this.name= “Guitar” ;
this.numberOfstrings = 6;
}
Public ElectricGuitar(int numberOfStrings) {
Super();
this.name = “Guitar”;
this.numberOfstrings = numberOfstrings;
}
@override
public void play() {
System.out.println(“An Electric “ + numberOfStrings + “-string” + name + “ is
rocking! ”);}}

18
Abstract class declaration
# Example
Public class ElectricBassGuitar extends StringedInstrument {
Public ElectricBassGuitar() {
Super();
this.name = “Bass Guitar”;
this.numberOfStrings = 4;
}
Public ElectricBassGuitar(int numberOfStrings) {
Super();
this.name = “Bass Guitar”;
this.numberOfStrings = numberOfStrings ;
}
@Override
public void play() {
System.out.println(“An Electric “ + numberOfStrings + “-string” + name + “ is
rocking! ”);
}}

19
Abstract class declaration
# Example
Import main.java.music.ElectricBassGuitar;
Import main.java.music.ElectricGuitar;
public class Execution {
Public static void main(String[] args) {
ElectricGuitar guitar = new ElectricGuitar();
ElectricBassGuitarbassGuitar = new ElectricBassGuitar();
Guitar.play();
bassGuitar.play();
guitar = new ElectricGuitar(7);
bassGuitar = new ElectricBassGuitar(5);
guitar.play();
bassGuitar.play();
}}

20
Rules
 Note 1: As we seen in the above example,
there are cases when it is difficult or often
unnecessary to implement all the methods in
parent class. In these cases, we can declare
the parent class as abstract, which makes it a
special class which is not complete on its own.

 A class derived from the abstract class must


implement all those methods that are declared
as abstract in the parent class.
21
Rules
 Note 2: Abstract class cannot be instantiated
which means you cannot create the object of it.
 To use this class, you need to create another
class that extends this class and provides the
implementation of abstract methods.
 Then you can use the object of that child class
to call non-abstract methods of parent class as
well as implemented methods(those that were
abstract in parent but implemented in child
class).
22
Rules
 Note 3: If a child does not implement all the
abstract methods of abstract parent class, then
the child class must need to be declared
abstract as well.

23
Do you know?
 Since abstract class allows concrete methods
as well, it does not provide 100% abstraction.
 You can say that it provides partial abstraction.
 Abstraction is a process where you show only
“relevant” data and “hide” unnecessary details
of an object from the user.

24
Why can’t we create the object of an
abstract class?
 Because these classes are incomplete.
 They have abstract methods that have no body
 So if java allows you to create object of this
class then if someone calls the abstract method
using that object then

What would happen?

25
Why can’t we create the object of an
abstract class?
 There would be no actual implementation of the
method to invoke.
 Also because an object is concrete. An abstract
class is like a template, so you have to extend it
and build on it before you can use it.

26
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} public static void main(String args[])
abstract public void {
anotherMethod(); //error: You can't create object of
} it
public class Demo extends AbstractDemo obj = new
AbstractDemo{ AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}

27
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} Output: public static void main(String args[])
abstract public void {
anotherMethod(); //error: You can't create object of
} Unresolved compilation problem:
it Cannot instantiate the
publictype
classAbstractDemo
Demo extends AbstractDemo obj = new
AbstractDemo{ AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}

28
Example to demonstrate that object
creation of abstract class is not allowed
abstract class AbstractDemo{
public void myMethod(){
System.out.println("Hello");
} Output: public static void main(String args[])
abstract public void {
Note: The
anotherMethod();class that extends
Unresolved compilation problem:
the abstract
//error: class,
You can't create
Cannot instantiate the
object of
} it
have to implement
publictype
class Demo extends all the abstract
AbstractDemo methods
AbstractDemo of it,
obj = new
else you have to declare that
AbstractDemo{ class abstract as well.
AbstractDemo();
obj.anotherMethod();
public void anotherMethod() { }
System.out.print("Abstract }
method");
}

29
Abstract class vs Concrete class

 A class which is not abstract is referred as


Concrete class.

30
Abstract class vs Concrete class
 Key Points:
 An abstract class has no use until unless it is
extended by some other class.
 If you declare an abstract method in a class then
you must declare the class abstract as well. you
can’t have abstract method in a concrete class. It’s
vice versa is not always true: If a class is not having
any abstract method then also it can be marked as
abstract.
 It can have non-abstract method (concrete) as well.

31
Abstract method in Java
 For now lets just see some basics and example
of abstract method.
 Abstract method has no body.
 Always end the declaration with a semicolon(;).
 It must be overridden. An abstract class must be
extended and in a same way abstract method must
be overridden.
 A class has to be declared abstract to have abstract
methods.

32
Example of Abstract class and
method
class Demo extends MyClass{
/* Must Override this method while
extending
abstract class MyClass{ * MyClas
public void disp(){ */
public void disp2()
{
System.out.println("Concrete
System.out.println("overriding
method of parent class"); abstract method");
} }
abstract public void disp2(); public static void main(String
} args[]){
Demo obj = new Demo();
obj.disp2();
}
}

33
Example of Abstract class and
method
class Demo extends MyClass{
/* Must Override this method while
extending
abstract class MyClass{ * MyClas
public void disp(){ */
Output:
public void disp2()
{
System.out.println("Concrete
overriding abstract method
System.out.println("overriding
method of parent class"); abstract method");
} }
abstract public void disp2(); public static void main(String
} args[]){
Demo obj = new Demo();
obj.disp2();
}
}

34

You might also like