SlideShare a Scribd company logo
Interfaces vs Abstract Classes
Abstraction vs Encapsulation
Interfaces and Abstraction
Software University
http://softuni.bg
SoftUni Team
http://softuni.bg
1. Abstraction
 Abstraction vs Encapsulation
2. Interfaces
 Default Methods
 Static Methods
3. Abstract Classes
4. Interfaces vs Abstract Classes
Table of Contents
2
sli.do
#java-advanced
Have a Question?
Working with Abstraction
Abstraction
 Latin origin
 Preserving information that is relevant in a context
 Forgetting information that is irrelevant in that
context
What is Abstraction?
5
Abs
(away from)
Trahere
(to draw)
Abstraction
 Abstraction means ignoring irrelevant features, properties, or
functions and emphasizing the relevant ones …
 ... relevant to the context of the project we develop
 Abstraction helps managing complexity
 Abstraction lets you focus on what the object does instead of
how it does it
Abstraction in OOP
6
"Relevant" to
what?
 There are 2 ways to achieve abstraction in Java
 Interfaces (100% abstraction)
 Abstract class (0% - 100% abstraction)
Achieving Abstraction
7
public interface Animal {}
public abstract class Mammal {}
public class Person extends Mammal implements Animal {}
Abstraction vs. Encapsulation
 Abstraction
 Process of hiding the
implementation details
and showing only
functionality to the user
 Achieved with interfaces
and abstract classes
 Encapsulation
 Used to hide the code
and data inside a single
unit to protect the data
from the outside world
 Achieved with access
modifiers (private,
protected, public)
8
Interfaces
Working with Interfaces
 Internal addition by compiler
Interface
10
public interface Printable {
int MIN = 5;
void print();
}
interface Printable {
public static final int MIN = 5;
public abstract void print();
}
compiler
"public abstract"
before methods
"public static final"
before fields
Public or default
modifier
Keyword
Name
 Relationship between classes and interfaces
 Multiple inheritance
implements vs extends
11
Interface
implementsextends extends
Interface
Interface
ClassClass
Class
Class
InterfaceInterface Interface Interface
Interface
extendsimplements
 Implementation of print() is provided in class Document
Interface Example
12
public interface Printable {
void print();
}
class Document implements Printable {
public void print() { System.out.println("Hello"); }
public static void main(String args[]) {
Printable doc = new Document();
doc.print(); // Hello
}
}
Polymorphism
Problem: Car Shop
13
Seat
-countryProduced: String
+toString(): String<<interface>>
<<Car>>
+TIRES: Integer
+getModel(): String
+getColor(): String
+getHorsePower(): Integer
Serializable
Check your solution here :https://judge.softuni.bg/Contests/1581/Interfaces-and-Abstraction-Lab
Solution: Car Shop (1)
14
public interface Car {
int TIRES = 4;
String getModel();
String getColor();
Integer getHorsePower();
String countryProduced();
}
Solution: Car Shop (2)
15
public class Seat implements Car, Serializable {
// TODO: Add fields, constructor and private methods
@Override
public String getModel() { return this.model; }
@Override
public String getColor() { return this.color; }
@Override
public Integer getHorsePower() { return this.horsePower; }
}
 Interface can extend another interface
Extend Interface
16
public interface Showable {
void show();
}
public interface Printable extends Showable {
void print();
}
 Class which implements child interface must provide
implementation for parent interface too
Extend Interface
17
class Circle implements Printable
public void print() {
System.out.println("Hello");
}
public void show() {
System.out.println("Welcome");
}
 Refactor your first problem code
 Add for rentable cars
 Add class Cinterface for sellable cars
 Add interface arImpl
 Add class Audi, which extends CarImpl and implements
rentable
 Refactor class Seat to extends CarImpl and implements rentable
Problem: Car Shop Extended
18
Solution: Car Shop Extended (1)
19
public interface Sellable extends Car {
Double getPrice();
}
public interface Rentable extends Car {
Integer getMinRentDay();
Double getPricePerDay();
}
Solution: Car Shop Extended (2)
20
public class Audi extends CarImpl implements Rentable {
public Integer getMinRentDay() {
return this.minDaysForRent; }
public Double getPricePerDay() {
return this.pricePerDay; }
// TODO: Add fields, toString() and Constructor
}
 Since Java 8 we can have method body in the interface
 If you need to override default method think about your design
Default Method
21
public interface Drawable {
void draw();
default void msg() {
System.out.println("default method:");
}
}
 Implementation is not needed for default methods
Default Method
22
class TestInterfaceDefault {
public static void main(String args[]) {
Drawable d = new Rectangle();
d.draw(); // drawing rectangle
d.msg(); // default method
}
}
 Since Java 11, we can have static method in interface
Static Method
23
public interface Drawable {
void draw();
static int cube(int x) { return x*x*x; }
}
public static void main(String args[]) {
Drawable d = new Rectangle();
d.draw();
System.out.println(Drawable.cube(3)); } // 27
 Design a project, which has
 Interface for Person
 3 implementations
for different nationalities
 Override where needed
Problem: Say Hello
24
<<Person>>
European
-name: String
<<interface>>
<<Person>>
+getName(): String
+sayHello():String
<<Person>>
Bulgarian
-name: String
+sayHello(): String
<<Person>>
Chinese
-name: String
+sayHello(): String
Solution: Say Hello (1)
25
public interface Person {
String getName();
default String sayHello() { return "Hello"; }
}
public class European implements Person {
private String name;
public European(String name) { this.name = name; }
public String getName() { return this.name; }
}
Solution: Say Hello (2)
26
public class Bulgarian implements Person {
private String name;
public Bulgarian(String name) {
this.name = name;
}
public String getName() { return this.name; }
public String sayHello() { return "Здравей"; }
}
// TODO: implement class Chinese
Abstract Classes
Abstract Classes and Methods
27
 Cannot be instantiated
 May contain abstract methods
 Must provide implementation for all inherited
interface members
 Implementing an interface might map the interface
methods onto abstract methods
Abstract Class
28
public abstract class Animal {
}
Abstract Methods
 Declarations are only permitted in abstract classes
 Bodies must be empty (no curly braces)
 An abstract method declaration provides no actual
implementation:
29
public abstract void build();
Interfaces vs Abstract Classes
30
Interface vs Abstract Class (1)
 Interface
 A class may implement
several interfaces
 Cannot have access
modifiers, everything is
assumed as public
 Abstract Class (AC)
 May inherit only
one abstract class
 Provides implementation
and/or just the signature
that has to be overridden
 Can contain access
modifiers for the fields,
functions, properties
31
Interface vs Abstract Class (2)
 Interface
 If we add a new method
we must track down
all the implementations of
the interface and
define implementation
for the new method
 Abstract Class
 Fields and constants
can be defined
 If we add a new method
we have the option of
providing default
implementation
32
 Refactor the code from the last problem
 Add BasePerson abstract class
 In which move all code duplication from European, Bulgarian,
Chinese
Problem: Say Hello Extended
33
BasePerson
#BasePerson(name)
-name: String
-setName(): void
Solution: Say Hello Extended
34
public abstract class BasePerson implements Person {
private String name;
protected BasePerson(String name) {
this.setName(name);
}
private void setName(String name) { this.name = name; }
@Override
public String getName() {
return this.name;
}
}
 …
 …
 …
Summary
35
 Abstraction – hiding implementation and
showing functionality
 Interfaces
 implements vs extends
 Default and Static methods
 Abstract classes
 Interfaces vs Abstract Classes
 https://softuni.bg/modules/59/java-advanced
SoftUni Diamond Partners
SoftUni Organizational Partners
 Software University – High-Quality Education and
Employment Opportunities
 softuni.bg
 Software University Foundation
 http://softuni.foundation/
 Software University @ Facebook
 facebook.com/SoftwareUniversity
 Software University Forums
 forum.softuni.bg
Trainings @ Software University (SoftUni)
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International" license
License
40

More Related Content

What's hot (20)

PPT
Inheritance C#
Raghuveer Guthikonda
 
PPT
Exception Handling
Sunil OS
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PPT
Java Basics
Sunil OS
 
PPTX
C# Inheritance
Prem Kumar Badri
 
PPTX
Inheritance
Sapna Sharma
 
PPS
Wrapper class
kamal kotecha
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Java Foundations: Data Types and Type Conversion
Svetlin Nakov
 
PPT
Java Input Output and File Handling
Sunil OS
 
PPT
Java Basics V3
Sunil OS
 
PPTX
Arrays in java language
Hareem Naz
 
PPTX
COLLECTIONS.pptx
SruthyPJ
 
PDF
Java collections
Hamid Ghorbani
 
PPT
Java IO Streams V4
Sunil OS
 
PDF
Polymorphism
Raffaele Doti
 
PPTX
09. Java Methods
Intro C# Book
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Inheritance C#
Raghuveer Guthikonda
 
Exception Handling
Sunil OS
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Java Basics
Sunil OS
 
C# Inheritance
Prem Kumar Badri
 
Inheritance
Sapna Sharma
 
Wrapper class
kamal kotecha
 
Method overloading
Lovely Professional University
 
Java Foundations: Data Types and Type Conversion
Svetlin Nakov
 
Java Input Output and File Handling
Sunil OS
 
Java Basics V3
Sunil OS
 
Arrays in java language
Hareem Naz
 
COLLECTIONS.pptx
SruthyPJ
 
Java collections
Hamid Ghorbani
 
Java IO Streams V4
Sunil OS
 
Polymorphism
Raffaele Doti
 
09. Java Methods
Intro C# Book
 
Java threads
Prabhakaran V M
 
Classes, objects in JAVA
Abhilash Nair
 
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 

Similar to 20.4 Java interfaces and abstraction (20)

PPT
Major Java 8 features
Sanjoy Kumar Roy
 
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
PDF
Design patters java_meetup_slideshare [compatibility mode]
Dimitris Dranidis
 
PPTX
abstract,final,interface (1).pptx upload
dashpayal697
 
PPT
20 Object-oriented programming principles
maznabili
 
PPT
C++ oop
Sunil OS
 
PPT
Design Patterns
Rafael Coutinho
 
PPTX
SoCal Code Camp 2015: An introduction to Java 8
Chaitanya Ganoo
 
PDF
Design Patterns
adil raja
 
PPTX
Interface in java
Kavitha713564
 
PPT
Implementation of interface9 cm604.30
myrajendra
 
PDF
Exception handling and packages.pdf
Kp Sharma
 
PPT
Java interface
Arati Gadgil
 
PPTX
.Net december 2017 updates - Tamir Dresher
Tamir Dresher
 
PDF
02.adt
Aditya Asmara
 
PPT
object oriented concepts and understand the oop
nandinigaikwad487
 
PPT
polymorphism.ppt
Shubham79233
 
PDF
Presentation on design pattern software project lll
Uchiha Shahin
 
PPT
Software Design Patterns
Pankhuree Srivastava
 
PPT
OOP Core Concept
Rays Technologies
 
Major Java 8 features
Sanjoy Kumar Roy
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Design patters java_meetup_slideshare [compatibility mode]
Dimitris Dranidis
 
abstract,final,interface (1).pptx upload
dashpayal697
 
20 Object-oriented programming principles
maznabili
 
C++ oop
Sunil OS
 
Design Patterns
Rafael Coutinho
 
SoCal Code Camp 2015: An introduction to Java 8
Chaitanya Ganoo
 
Design Patterns
adil raja
 
Interface in java
Kavitha713564
 
Implementation of interface9 cm604.30
myrajendra
 
Exception handling and packages.pdf
Kp Sharma
 
Java interface
Arati Gadgil
 
.Net december 2017 updates - Tamir Dresher
Tamir Dresher
 
object oriented concepts and understand the oop
nandinigaikwad487
 
polymorphism.ppt
Shubham79233
 
Presentation on design pattern software project lll
Uchiha Shahin
 
Software Design Patterns
Pankhuree Srivastava
 
OOP Core Concept
Rays Technologies
 
Ad

More from Intro C# Book (20)

PPTX
Java Problem solving
Intro C# Book
 
PPTX
21. Java High Quality Programming Code
Intro C# Book
 
PPTX
20.5 Java polymorphism
Intro C# Book
 
PPTX
20.1 Java working with abstraction
Intro C# Book
 
PPTX
19. Java data structures algorithms and complexity
Intro C# Book
 
PPTX
18. Java associative arrays
Intro C# Book
 
PPTX
16. Java stacks and queues
Intro C# Book
 
PPTX
14. Java defining classes
Intro C# Book
 
PPTX
13. Java text processing
Intro C# Book
 
PPTX
12. Java Exceptions and error handling
Intro C# Book
 
PPTX
11. Java Objects and classes
Intro C# Book
 
PPTX
05. Java Loops Methods and Classes
Intro C# Book
 
PPTX
07. Java Array, Set and Maps
Intro C# Book
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPTX
02. Data Types and variables
Intro C# Book
 
PPTX
01. Introduction to programming with java
Intro C# Book
 
PPTX
23. Methodology of Problem Solving
Intro C# Book
 
PPTX
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
PPTX
21. High-Quality Programming Code
Intro C# Book
 
PPTX
19. Data Structures and Algorithm Complexity
Intro C# Book
 
Java Problem solving
Intro C# Book
 
21. Java High Quality Programming Code
Intro C# Book
 
20.5 Java polymorphism
Intro C# Book
 
20.1 Java working with abstraction
Intro C# Book
 
19. Java data structures algorithms and complexity
Intro C# Book
 
18. Java associative arrays
Intro C# Book
 
16. Java stacks and queues
Intro C# Book
 
14. Java defining classes
Intro C# Book
 
13. Java text processing
Intro C# Book
 
12. Java Exceptions and error handling
Intro C# Book
 
11. Java Objects and classes
Intro C# Book
 
05. Java Loops Methods and Classes
Intro C# Book
 
07. Java Array, Set and Maps
Intro C# Book
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
02. Data Types and variables
Intro C# Book
 
01. Introduction to programming with java
Intro C# Book
 
23. Methodology of Problem Solving
Intro C# Book
 
Chapter 22. Lambda Expressions and LINQ
Intro C# Book
 
21. High-Quality Programming Code
Intro C# Book
 
19. Data Structures and Algorithm Complexity
Intro C# Book
 
Ad

Recently uploaded (20)

PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PDF
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PDF
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
PPTX
internet básico presentacion es una red global
70965857
 
PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPT
introductio to computers by arthur janry
RamananMuthukrishnan
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
PDF
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PPTX
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PPTX
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
AI_MOD_1.pdf artificial intelligence notes
shreyarrce
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
Apple_Environmental_Progress_Report_2025.pdf
yiukwong
 
internet básico presentacion es una red global
70965857
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
introductio to computers by arthur janry
RamananMuthukrishnan
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
The-Hidden-Dangers-of-Skipping-Penetration-Testing.pdf.pdf
naksh4thra
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
L1A Season 1 Guide made by A hegy Eng Grammar fixed
toszolder91
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PE introd.pptxfrgfgfdgfdgfgrtretrt44t444
nepmithibai2024
 
Orchestrating things in Angular application
Peter Abraham
 

20.4 Java interfaces and abstraction

  • 1. Interfaces vs Abstract Classes Abstraction vs Encapsulation Interfaces and Abstraction Software University http://softuni.bg SoftUni Team http://softuni.bg
  • 2. 1. Abstraction  Abstraction vs Encapsulation 2. Interfaces  Default Methods  Static Methods 3. Abstract Classes 4. Interfaces vs Abstract Classes Table of Contents 2
  • 5.  Latin origin  Preserving information that is relevant in a context  Forgetting information that is irrelevant in that context What is Abstraction? 5 Abs (away from) Trahere (to draw) Abstraction
  • 6.  Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones …  ... relevant to the context of the project we develop  Abstraction helps managing complexity  Abstraction lets you focus on what the object does instead of how it does it Abstraction in OOP 6 "Relevant" to what?
  • 7.  There are 2 ways to achieve abstraction in Java  Interfaces (100% abstraction)  Abstract class (0% - 100% abstraction) Achieving Abstraction 7 public interface Animal {} public abstract class Mammal {} public class Person extends Mammal implements Animal {}
  • 8. Abstraction vs. Encapsulation  Abstraction  Process of hiding the implementation details and showing only functionality to the user  Achieved with interfaces and abstract classes  Encapsulation  Used to hide the code and data inside a single unit to protect the data from the outside world  Achieved with access modifiers (private, protected, public) 8
  • 10.  Internal addition by compiler Interface 10 public interface Printable { int MIN = 5; void print(); } interface Printable { public static final int MIN = 5; public abstract void print(); } compiler "public abstract" before methods "public static final" before fields Public or default modifier Keyword Name
  • 11.  Relationship between classes and interfaces  Multiple inheritance implements vs extends 11 Interface implementsextends extends Interface Interface ClassClass Class Class InterfaceInterface Interface Interface Interface extendsimplements
  • 12.  Implementation of print() is provided in class Document Interface Example 12 public interface Printable { void print(); } class Document implements Printable { public void print() { System.out.println("Hello"); } public static void main(String args[]) { Printable doc = new Document(); doc.print(); // Hello } } Polymorphism
  • 13. Problem: Car Shop 13 Seat -countryProduced: String +toString(): String<<interface>> <<Car>> +TIRES: Integer +getModel(): String +getColor(): String +getHorsePower(): Integer Serializable Check your solution here :https://judge.softuni.bg/Contests/1581/Interfaces-and-Abstraction-Lab
  • 14. Solution: Car Shop (1) 14 public interface Car { int TIRES = 4; String getModel(); String getColor(); Integer getHorsePower(); String countryProduced(); }
  • 15. Solution: Car Shop (2) 15 public class Seat implements Car, Serializable { // TODO: Add fields, constructor and private methods @Override public String getModel() { return this.model; } @Override public String getColor() { return this.color; } @Override public Integer getHorsePower() { return this.horsePower; } }
  • 16.  Interface can extend another interface Extend Interface 16 public interface Showable { void show(); } public interface Printable extends Showable { void print(); }
  • 17.  Class which implements child interface must provide implementation for parent interface too Extend Interface 17 class Circle implements Printable public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); }
  • 18.  Refactor your first problem code  Add for rentable cars  Add class Cinterface for sellable cars  Add interface arImpl  Add class Audi, which extends CarImpl and implements rentable  Refactor class Seat to extends CarImpl and implements rentable Problem: Car Shop Extended 18
  • 19. Solution: Car Shop Extended (1) 19 public interface Sellable extends Car { Double getPrice(); } public interface Rentable extends Car { Integer getMinRentDay(); Double getPricePerDay(); }
  • 20. Solution: Car Shop Extended (2) 20 public class Audi extends CarImpl implements Rentable { public Integer getMinRentDay() { return this.minDaysForRent; } public Double getPricePerDay() { return this.pricePerDay; } // TODO: Add fields, toString() and Constructor }
  • 21.  Since Java 8 we can have method body in the interface  If you need to override default method think about your design Default Method 21 public interface Drawable { void draw(); default void msg() { System.out.println("default method:"); } }
  • 22.  Implementation is not needed for default methods Default Method 22 class TestInterfaceDefault { public static void main(String args[]) { Drawable d = new Rectangle(); d.draw(); // drawing rectangle d.msg(); // default method } }
  • 23.  Since Java 11, we can have static method in interface Static Method 23 public interface Drawable { void draw(); static int cube(int x) { return x*x*x; } } public static void main(String args[]) { Drawable d = new Rectangle(); d.draw(); System.out.println(Drawable.cube(3)); } // 27
  • 24.  Design a project, which has  Interface for Person  3 implementations for different nationalities  Override where needed Problem: Say Hello 24 <<Person>> European -name: String <<interface>> <<Person>> +getName(): String +sayHello():String <<Person>> Bulgarian -name: String +sayHello(): String <<Person>> Chinese -name: String +sayHello(): String
  • 25. Solution: Say Hello (1) 25 public interface Person { String getName(); default String sayHello() { return "Hello"; } } public class European implements Person { private String name; public European(String name) { this.name = name; } public String getName() { return this.name; } }
  • 26. Solution: Say Hello (2) 26 public class Bulgarian implements Person { private String name; public Bulgarian(String name) { this.name = name; } public String getName() { return this.name; } public String sayHello() { return "Здравей"; } } // TODO: implement class Chinese
  • 28.  Cannot be instantiated  May contain abstract methods  Must provide implementation for all inherited interface members  Implementing an interface might map the interface methods onto abstract methods Abstract Class 28 public abstract class Animal { }
  • 29. Abstract Methods  Declarations are only permitted in abstract classes  Bodies must be empty (no curly braces)  An abstract method declaration provides no actual implementation: 29 public abstract void build();
  • 31. Interface vs Abstract Class (1)  Interface  A class may implement several interfaces  Cannot have access modifiers, everything is assumed as public  Abstract Class (AC)  May inherit only one abstract class  Provides implementation and/or just the signature that has to be overridden  Can contain access modifiers for the fields, functions, properties 31
  • 32. Interface vs Abstract Class (2)  Interface  If we add a new method we must track down all the implementations of the interface and define implementation for the new method  Abstract Class  Fields and constants can be defined  If we add a new method we have the option of providing default implementation 32
  • 33.  Refactor the code from the last problem  Add BasePerson abstract class  In which move all code duplication from European, Bulgarian, Chinese Problem: Say Hello Extended 33 BasePerson #BasePerson(name) -name: String -setName(): void
  • 34. Solution: Say Hello Extended 34 public abstract class BasePerson implements Person { private String name; protected BasePerson(String name) { this.setName(name); } private void setName(String name) { this.name = name; } @Override public String getName() { return this.name; } }
  • 35.  …  …  … Summary 35  Abstraction – hiding implementation and showing functionality  Interfaces  implements vs extends  Default and Static methods  Abstract classes  Interfaces vs Abstract Classes
  • 39.  Software University – High-Quality Education and Employment Opportunities  softuni.bg  Software University Foundation  http://softuni.foundation/  Software University @ Facebook  facebook.com/SoftwareUniversity  Software University Forums  forum.softuni.bg Trainings @ Software University (SoftUni)
  • 40.  This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license License 40

Editor's Notes

  • #3: (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  • #8: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #11: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #12: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #13: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #14: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #15: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #16: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #17: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #18: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #19: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #20: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #21: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #22: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #23: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #24: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #25: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #26: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #27: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #29: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces
  • #34: Java interfaces are like purely abstract classes, but: - All interface methods are abstract - Interface members do not have scope modifiers - Their scope is assumed public - But public is not specified explicitly - Cannot define fields, inner types and constructors
  • #35: There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces