0% found this document useful (0 votes)
4 views18 pages

Chap2.2 Abstractclassandinterface

Chapter 2 discusses the concepts of Abstract Classes and Interfaces in Java, emphasizing their role in achieving abstraction by hiding implementation details. It explains the characteristics and differences between abstract classes and interfaces, including their syntax, methods, and the concept of multiple inheritance. The chapter provides examples to illustrate how to implement these concepts in Java programming.

Uploaded by

Mukesh Periasamy
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)
4 views18 pages

Chap2.2 Abstractclassandinterface

Chapter 2 discusses the concepts of Abstract Classes and Interfaces in Java, emphasizing their role in achieving abstraction by hiding implementation details. It explains the characteristics and differences between abstract classes and interfaces, including their syntax, methods, and the concept of multiple inheritance. The chapter provides examples to illustrate how to implement these concepts in Java programming.

Uploaded by

Mukesh Periasamy
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/ 18

Chapter 2

2.2
Abstract Class
& Interface
Chapter Abstract Class & Interface
2:
Objectives
At the end of this lesson, you will be able to
understand and create:
 Abstract class
 Interface
Abstraction
What is abstraction?

• Abstraction is a process of hiding the implementation details


and showing only functionality to the user.

• The main purpose of abstraction is hiding the unnecessary


details from the users

• The main benefit of using an Abstraction in Programming is


that it allows to group several related classes as
siblings (child classes).

• Abstraction in Object Oriented Programming helps to reduce


the complexity of the design and implementation
process of software.
Abstraction

How to achieve abstraction in


Java?1. Abstract class

2. Interface
Abstract Class & Interface

Abstract Class
 A class which is declared as abstract is
known as an abstract class
 It needs to be extended and its method must
be implemented
 It cannot be instantiated
 Abstract class can have abstract and non-
abstract methods
 It can have constructors and static methods
 It can have final methods which will force the
subclass not to change the body of the method
Abstract Class & Interface

Abstract class
abstract Keyword
abstract is the keyword used to generalized a
class or method.

Syntax
Access_modifier abstract class ClassName {
// variables or fields
// methods

// abstract method
Access_modifier abstract return_type methodName(parameter list);
// abstract method has no method body

}
Abstract Class & Interface
Abstract class
Shape

- color : String

+getArea() : double

Rectangle Triangle
- length : double - base : double
- Width : double - height : double

+getArea() : double +getArea() : double


Abstract Class & Interface
example
abstract class Shape {
private String color;

public abstract double


getArea();
}

class Rectangle extends Shape{


private double length, width;

public double getArea() {


return length * width;
}
}

class Triangle extends Shape{


private double base, height;

public double getArea() {


return 0.5* base *
height;
}
}
abstract class Shape {
example
public static String color=“Biru”;
class Main {
public abstract double getArea();
public static void main(String args[]){
}
Rectangle petak = new Rectangle(4,5);
class Rectangle extends Shape{
Triangle segi = new Triangle(2,5);
private double length, width;
System.out.println("luas segiempat:“
Rectangle (double l, double w){
+Rectangle.color+“:”+petak.getArea());
length=l; width=w;
System.out.println("luas segiempat:“
}
+Triangle.color+“:”+segi.getArea());
}
public double getArea() {
return length * width;
}
}
}

class Triangle extends Shape{


private double base, height;

Triangle (double b, double h){


base=b; height=h;
}

public double getArea() {


return (0.5 * base * height);
}
}
Abstract Class & Interface

Interface
 An interface in java is a blueprint of a class.
 Interface can have static constants and
abstract methods
 There can be only abstract methods in the
Java interface, not method body.
 It is used to achieve abstraction and multiple
inheritance in Java
 Interface cannot be instantiated
 A class that implements an interface must
implement all the methods declared in the
interface
Abstract Class & Interface

Interface
interface Keyword
interface is the keyword used to provide total
abstraction.

Syntax
Access_modifier interface interfaceName {
// declare constant fields
// declare methods header
// method in interface has no method body
}
Abstract Class & Interface
Difference between Class and Interface
Class Interface

In class, you can instantiate variable In an interface, you can't instantiate


and create an object. variable and create an object.

Class can contain concrete(with The interface cannot contain


implementation) methods concrete(with implementation)
methods

The access specifiers used with In Interface only one specifier is


classes are private, protected and used- Public.
public.
Abstract Class & Interface

Interface
Multiple inheritance in Java by interface
Abstract Class & Interface
Interface

<<interface>> <<interface>>
Behavior Animal

+ family : String
+ eat() : void + name() : void
+ move() : void + leg() : void
+ sound() : void + body() : void

Cat Fish

+ eat() : void + name() : void


+ move() : void + leg() : void
+ sound() : void + body() : void
+ name() : void
+ leg() : void
+ body() : void
Abstract Class & Interface
example
interface Animal{
public static final String family = "Animals";

public abstract void name();


public abstract void leg();
public abstract void body();
}
class Fish implements Animal{

public void name() {


System.out.println(family+":I am a fish");
}

public void leg() {


System.out.println("I don't have legs");
}

public void body() {


System.out.println("I have scales");
}
}
interface Behavior{
example
public abstract void eat(); class Cat implements Animal, Behavior{
public abstract void move();
public abstract void sound(); public void name() {
} System.out.println(family+":I am a
cat");
interface Animal{ }
public String family = "Animals"; public void leg() {
System.out.println("Cat has 4
public abstract void name(); legs");
public abstract void leg(); }
public abstract void body();
} public void body() {
System.out.println("Cat have fur");
class Fish implements Animal{ }
public void name() {
System.out.println(family+":I am a public void eat() {
fish"); System.out.println("Cat eat fish");
} }

public void leg() { public void move() {


System.out.println("I don't have System.out.println("Cat walk");
legs"); }
}
public void sound() {
public void body() { System.out.println("meow...meow..");
System.out.println("I have scales"); }
} }
}
example

public class Main {

public static void main(String[] args) {


Cat a = new Cat();
Fish b = new Fish();
b.name();
b.body();
a.name();
a.body();
a.sound();
}

}
Abstract Class & Interface
Difference between Abstract Class and
Interface
Abstraction Interface

An abstract class can have both The interface can have only abstract
abstract and non-abstract methods. methods.

It does not support multiple It supports multiple inheritances.


inheritances.

It can provide the implementation It can not provide the


of the interface. implementation of the abstract
class.
An abstract class can have An interface can have only public
protected and abstract public abstract methods.
methods.
An abstract class can have final, The interface can only have a public
static, or static final variable with static final variable.
any access specifier.

You might also like