Abstraction, Interface, Inheritance, Polymorphism, Override / Overload

Slides:



Advertisements
Similar presentations
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Advertisements

JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
Peyman Dodangeh Sharif University of Technology Fall 2014.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Static Members Static Variables & Methods SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Auto Mapping Objects SoftUni Team Database Applications
Working with Fluent API Inheritance Strategies
Static Members and Namespaces
Databases basics Course Introduction SoftUni Team Databases basics
Abstract Classes, Abstract Methods, Override Methods
Sets, Hash table, Dictionaries
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
OCP and Liskov Principles
Services & Dependency Injection
Advanced Programming in Java
C# Basic Syntax, Visual Studio, Console Input / Output
Classes and Class Members
Introduction to MVC SoftUni Team Introduction to MVC
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
Web Design & Development Lecture 9
Object-Oriented Programming Concepts
Classes, Properties, Constructors, Objects, Namespaces
Mocking tools for easier unit testing
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
C# Databases Advanced with Microsoft SQL Server
OOP Course - Virtual Trip
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
EF Relations Object Composition
Inheritance Extending Classes SoftUni Team Technical Trainers Java OOP
Entity Framework: Code First
Repeating Code Multiple Times
Inheritance ITI1121 Nour El Kadri.
Inheritance Class Hierarchies SoftUni Team Technical Trainers C# OOP
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Entity Framework DB From Code, OOP Introduction
Databases advanced Course Introduction SoftUni Team Databases advanced
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Array and List Algorithms
Functional Programming
Classes and Class Members
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Best practices and architecture
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Manual Mapping and AutoMapper Library
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Introduction to TypeScript & Angular
CSS Transitions and Animations
Iterators and Comparators
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Hibernate (JPA) Code First Entity Relations
Spring Data Advanced Querying
Inheritance and Prototypes
Polymorphism, Interfaces, Abstract Classes
First-Class Functions
CSS Transitions and Animations
What is Encapsulation, Benefits, Implementation in Java
Multidimensional Arrays
OOP’S Concepts in C#.Net
Interfaces.
CS 200 More Classes Jim Williams, PhD.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Abstraction, Interface, Inheritance, Polymorphism, Override / Overload Java OOP Principles Abstraction, Interface, Inheritance, Polymorphism, Override / Overload Java OOP Principles SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Abstraction Interface Inheritance Polymorphism Override / Overload © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Questions sli.do #db-advanced

Abstraction © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

What is Abstraction? Abs Trahere From the Latin (away from) (to draw) Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics. © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Abstraction in OOP "Relevant" to what? Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones ... ... relevant to the project we develop Abstraction helps managing complexity "Relevant" to what? Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones, relevant to the project we develop. Abstraction helps managing complexity Abstraction is something we do every day: Looking at an object, we see those things that have meaning to us and ignore all others. Represent a complex reality with a simplified model. In a bank application, customers have: name, phone and address, but they don’t have : hair color and favorite drink, which we are disregarded from. "Relevant" to what? © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Abstraction Example Abstraction lets you focus on what the object does instead of how it does it. Abstraction means ignoring irrelevant features, properties, or functions and emphasizing the relevant ones, relevant to the project we develop. Abstraction helps managing complexity Abstraction is something we do every day: Looking at an object, we see those things that have meaning to us and ignore all others. Represent a complex reality with a simplified model. In a bank application, customers have: name, phone and address, but they don’t have : hair color and favorite drink, which we are disregarded from. "Relevant" to what? © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

How do we achieve abstraction? There are two ways to achieve abstraction in Java Interfaces (100% abstraction) Abstract class (0% - 100% abstraction) public interface Animal {} public abstract class Mammal {} public class Person extends Mammal implements Animal {} There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Abstraction vs Encapsulation Achieve with interfaces and abstract classes Abstraction is a process of hiding the implementation details and showing only functionality to the user. Encapsulation Achieve with access modifiers (private, public…) Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Abstraction vs Encapsulation (2) There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Interface © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Interface Internal addition by compiler Keyword public or default modifier public interface Printable { int MIN = 5; void print(); } Name compiler adds public static final before fields There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces interface Printable { public static final int MIN = 5; public abstract void print(); } public abstract before methods © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

implements vs extends Relationship between classes and interfaces Multiple inheritance Class Inteface Inteface extends implements extends Class Class Inteface There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Inteface Inteface Inteface Inteface implements extends Class Inteface © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Interface Example Implementation of print()is provided in class A6 public interface Printable { int MIN = 5; 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(); } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Polymorphism © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Problem: Shapes Drawing Build project that contain interface for drawable objects Implements two type of shapes: Circle and Rectangle Both classes have to print on console their shape with "*". <<interface>> Drawable +draw() <<Drawable>> Rectangle -width: Integer -height: Integer There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces <<Drawable>> Circle -radius: Integer © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Shapes Drawing public interface Drawable { void draw(); } public class Rectangle implements Drawable { //TODO Add fields and constructor @Override public void draw() { slide 17 } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces public class Circle implements Drawable { //TODO Add fields and constructor @Override public void draw() { slide 18 } } © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Shapes Drawing - Rectangle Draw Public class Rectangle implements Drawable { public void draw() { for (int i = 0; i < height; i++) { System.out.print("*"); for (int k = 1; k < width - 1; k++) { System.out.print(" "); if (i == 0 || i == (height - 1)) { } else { } } System.out.print(" "); System.out.print("*"); System.out.print("\n"); } } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Shapes Drawing - Circle Draw public class Circle implements Drawable { public void draw() { double r_in = this.radius - 0.4; double r_out = this.radius + 0.4; for(double y = this.radius; y >= -this.radius; --y) { for(double x = -this.radius; x < r_out; x += 0.5) { double value = x * x + y * y; if(value >= r_in * r_in && value <= r_out * r_out) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

<<interface>> Problem: Car Shop Serializable Seat -countryProduced: String +toString(): String <<interface>> <<Car>> +TIRES: Integer +getModel() +getColor() +getHorsePower() There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Car Shop public interface Car { int TIRES = 4; String getModel(); String getColor(); int getHorsePower(); } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Car Shop public class Seat implements Car, Serializable { //TODO: Add fields //TODO: Add constructor //TODO: Add private methods @Override public String getModel() { return this.model; } public String getColor() { return this.color; } public int getHorsePower() { return this.horsePower; } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Live Exercises in Class (Lab) Interfaces Live Exercises in Class (Lab) © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Extend Interface Interface can extend another interface public interface Showable { int MIN = 5; void show(); } public interface Printable extends Showable { void print(); } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Extend Interface (2) Class which implements child interface MUST provide implementation for parent interface too class Circle implements Printable { public void print() { System.out.println("Hello"); } public void show() { System.out.println("Welcome"); There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

<<Rentable>> <<Sellable>> Problem: Car Shop Refactor your first problem code Add interface for sellable cars Add interface for rentable cars Add class Audi, which implements rentable <<Car>> +getModel() +getColor() +getHorsePower() There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces <<Rentable>> +getMinRentDay(): Integer +getPricePerDay(): Double <<Sellable>> +getPrice(): Double © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Car Shop public interface Sellable extends Car { Double getPrice(); } public interface Rentable extends Car{ Integer getMinRentDay(); Double getPricePerDay(); } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Car Shop public class Audi implements Rentable { @Override public String getModel() { return this.model; } public String getColor() { return this.color; } public int getHorsePower() { return this.horsePower; } public Integer getMinRentDay() { return this.minDaysForRent; } public Double getPricePerDay() { return this.pricePerDay; } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Default Method Since Java 8, we can have method body in interface If you need to Override default method think about you Design public interface Drawable { void draw(); default void msg() { System.out.println("default method:) } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Default Method (2) Implementation doesn’t need for default methods class Rectangle implements Drawable{ public void draw() { System.out.println("drawing rectangle"); } } class TestInterfaceDefault { public static void main(String args[]) { Drawable d=new Rectangle(); d.draw(); d.msg(); } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces //drawing rectangle //default method © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Static Method Since Java 8, we can have static method in interface 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)); } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces //27 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Problem: Say Hi! Design project, which have: Interface for Person Three implementation for different nationality Override where need <<interface>> <<Person>> +getName(): String sayHi() <<Person>> European -name: String <<Person>> Bulgarian -name: String +sayHi(): String <<Person>> Chinese -name: String +sayHi(): String There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Say Hello public interface Person { String getName(); default void sayHello() { System.out.println("Hello");} } public class European implements Person{ private String name; public European(String name) { this.name = name; } @Override public String getName() { return this.name; } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Say Hello public class Bulgarian implements Person { private String name; public Bulgarian(String name) { this.name = name; } @Override public String getName() { return this.name; } public void sayHello() {System.out.println("Здравей");} //TODO: Make same for Chinese There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Interface vs Abstract Class Abstract class doesn't support multiple inheritance. Abstract class can have abstract and non- abstract methods. Abstract class can have final, non-final, static and non- static variables. Interface Interface supports multiple inheritance. Interface can have only abstract  methods. Since Java 8, it can have default and static methods also. Interface has only static and final variables. There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Problem: Base Person Refactor code from last problem Add BasePerson abstract class Move in it all code duplication from European, Bulgarian, Chinese BasePerson -setName(): void -name: String 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 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Solution: Say Hello 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; } } There are two ways to achieve abstraction - Using Abstract Classes - Using Interfaces Check your solution here: https://judge.softuni.bg/Contests/Compete/Index/498#3 © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Interfaces and Abstract Class Live Exercises in Class (Lab) © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Summary Abstraction Interface Inheritance Polymorphism Override / Overload Abstraction : Models class behavior Interfaces : Define a set of methods (contracts) Abstract classes : Mixes between class and interface

Java OOP Principles https://softuni.bg/courses/databases-advanced-hibernate © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University Forums forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.