Lecture Guides about Java Abstraction -Abstract methods, Lecture notes of Java Programming

Lecture Guides about Java Abstraction - Abstract methods

Typology: Lecture notes

2022/2023

Uploaded on 06/20/2023

emy-alinsod
emy-alinsod ๐Ÿ‡ต๐Ÿ‡ญ

2 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
TOPIC 6:
Abstraction in Java : Abstract Class and
Interface
ctto:
Afsara Tasneem Misha
Department of CSE
Daffodil International University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Lecture Guides about Java Abstraction -Abstract methods and more Lecture notes Java Programming in PDF only on Docsity!

TOPIC 6: Abstraction in Java : Abstract Class and Interface

ctto:Afsara Tasneem Misha Department of CSEDaffodil International University

Contents

  • 1. Abstraction in Java
  • 2. Ways to achieve Abstraction
    • 2.1. Abstract Classes and Abstract Methods
    • 2.2. Interface
  • 3. Advantage of Abstraction
  • 4. Useful Links
  • 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.
  • An Abstraction is a process of exposing all the necessary details and hiding the rest. In Java, Data Abstraction is defined as the process of reducing the object to its essence so that only the necessary characteristics are exposed to the users.

Part 1 : Abstraction

Part 2 : Ways to achieve Abstraction

Part โ€“ 2.1 : Abstract Class in Java

  • A class which is declared as abstract is known as

an abstract class.

  • An Abstract class is created through the use of the

abstract keyword.

  • It is used to represent a concept.
  • Object Can not be created of an Abstract Class.
  • Does not Support multiple inheritance.

2.1: Abstract Class

  • To declare an abstract class we use the keyword abstract. The syntax is given below:

access-specifier abstract class ClassName

//class body

Syntax of declaring an abstract class:

  • A method which is declared as abstract and does not have implementation is known as an abstract method.
  • The declaration of an abstract method must end with a semicolon ;
  • The child classes which inherit the abstract class must provide the implementation of these inherited abstract methods.
  • Syntax of declaring abstract methods:

access-specifier abstract return-type method_name();

  • Example:

public abstract void printStatus();

Abstract Methods in Java

An abstract class that has abstract and non-abstract methods

Example โ€“ 2

13

public class Car extends Vehicle { public void move() { System.out.println("Car moves faster."); } public static void main(String[] args) { Car c1 = new Car(); c1.move(); c1.carry(); } } // Outputs: Car moves faster. All Vehicle carry loads

public abstract class Vehicle { public abstract void move(); public void carry() { System.out.println("All Vehicle carry loads"); } }

An abstract class that has abstract , non-abstract methods and Constructor

Example โ€“ 3

public class Car extends Vehicle { public void move() { System.out.println("Car moves faster."); } public static void main(String[] args) { Car c1 = new Car(); c1.move(); c1.carry(); } } // Outputs: Vehicle is Created. Car moves faster.All Vehicle carry loads

public abstract class Vehicle { public Vehicle() { System.out.println("Vehicle is Created."); } public abstract void move(); public void carry() { System.out.println("All Vehicle carry loads"); } }

Vehicle

  • Vehicle() + move() : void
  • carry () : void

Car

  • move() : void
  • main(String[]): void

Example โ€“ 4 : Home Work

Boat

  • move() : void
  • main(String[]): void

Animal

_+ eat() : void

  • move() : void_
  • life() : void

Human

  • eat() : void
  • move() : void
  • talk () : void

Example โ€“ 5 :

Home Work

Lion

  • eat() : void
  • move() : void
  • hunt () : void

Bird

  • eat() : void
  • move() : void
  • fly () : void

Main

+main(String [] ) : void

  • An interface is a blueprint or template of a class.
  • It is much similar to the class but the only difference is that it has abstract methods and static constants.
  • Object can not be created of an interface.
  • All the methods in an interface are by default abstract.
  • All variables are declared as both static and final.
  • An interface does not contain any constructors.
  • An interface can not be extended or inherited by a class; it is implemented by a class.
  • Multiple-Inheritance supported.

2.2: Interface in Java

Internal addition by the compiler