SlideShare a Scribd company logo
3
Most read
10
Most read
12
Most read
Abstract Classes
and Interfaces
Ahmed Nobi
Notes before starting
• Red color font for external sources
- javatpoint.com
- docs.oracle.com
- beginnersbook.com
• Orange color font for” Introduction to Java programming comprehensive
version [Tenth Edition] written by Y. Daniel Liang” source
Also self effors
Abstract Class
- An abstract class cannot be used to create objects. An abstract class can contain abstract
methods, which are implemented in concrete subclasses.
- An abstract class is a class that is declared abstract—it may or may not include abstract
methods. Abstract classes cannot be instantiated, but they can be sub classed.
Let us discuss a point. All of us know the regular class. A class is basic building block of an object-oriented language. So
what does it mean by abstract class ?. we can answer it later. But first we need to show up some concepts may you faced it
before such as Inheritance.
Overview about Inheritance
• Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviors of a parent object.
The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields
of the parent class. Moreover, you can add new methods and fields in your current class
also.
Why use inheritance in java ?
• For Method Overriding (so runtime
polymorphism can be achieved).
• For Code Reusability.
Types of inheritance in java
On the basis of class, there can be three types of
inheritance in java: single, multilevel and hierarchical.
Terms used in Inheritance
• Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a
derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
• Reusability: As the name specifies, reusability is a mechanism which facilitates you to
reuse the fields and methods of the existing class when you create a new class. You can
use the same fields and methods already defined in the previous class.
“ In the inheritance hierarchy, classes become more specific and concrete with each new subclass. If
you move from a subclass back up to a superclass, the classes become more general and less
specific. Class design should ensure that a superclass contains common features of its subclasses.
Sometimes a superclass is so abstract that it cannot be used to create any specific instances. Such a
class is referred to as an abstract class.”
Which means every subclass you create its has own behaviors and fields but also it has a common behaviors which
inheritances it from parent classes. Also when you check the pervious diagram of inheritance types especially
hierarchal type, you will find the class C and B have common behaviors which are inherited from parent. That make us
wonder if we have more than C and B classes and all of them are extended from A class which means all of them have
common behaviors. For ex: our School it has students, Professors and employees, if we need a program that tells the
user who are the students of that school ?, who are the professors ? And who are employees ? Just give the user
their names and few data.
We notice that all of them students, professors and employees have common methods
Public string getName () {
return name ;
}
and so on for getAge() , getID() …
Conclusion
• If we implement it through only the inheritance concept, we will face
a problem that all of them have different fields and methods also
there are some important methods we need to ensure that some
methods will be overridden like getters. Now we are close enough to
get what the abstract class is. “a superclass is so abstract that it
cannot be used to create any specific instances. Such a class is
referred to as an abstract class.” and that the answer of our per
question “what does it mean by abstract class ?”
How to use In Java ?
• 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.
for example
abstract class A {}
Abstract Methods
• “An abstract method is defined without implementation. Its implementation is
provided by the subclasses. A class that contains abstract methods must be defined
as abstract.
The constructor in the abstract class is defined as protected, because it is used only
by subclasses. When you create an instance of a concrete subclass, its superclass’s
constructor is invoked to initialize data fields defined in the superclass”
• A method without body (no implementation) is known as abstract method.
For example
abstract class A {
abstract void print() ;
}
Simple example
• This example show how to create abstract class and
methods
public abstract class A {
public abstract void print();
}
public class B extends A {
@Override
Public void print(){
System.out.println(“Hello World!”)
}
}
Class B extends
Class A
Abstract methods must
be overridden in
subclasses
Abstract methods must be
defined in abstract classes
Let’s code…..
Code of simple example
public abstract class Faculty {
public abstract void setName (String x) ;
public abstract String getName () ;
public abstract void setAge(int x) ;
public abstract int getage () ;
public abstract void setID (int x) ;
public abstract int getID () ;
}
public class Professors extends
Faculty {
private String name ;
private int age ;
private int ID ;
private int salary ;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public void setName(String x) {
this.name = x ;
}
@Override
public String getName() {
return this.name ;
}
@Override
public void setAge(int x)
{
this.age = x ;
}
@Override
public int getage() {
return this.age ;
}
@Override
public void setID(int x) {
this.ID = x ;
}
@Override
public int getID() {
return this.ID ;
}
}
public class Student extends Faculty {
private String name ;
private int age ;
private int ID ;
private int score ;
private int level ;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
@Override
public void setName(String x) {
this.name = x ;
}
@Override
public String getName() {
return this.name ;
}
@Override
public void setAge(int x) {
this.age = x ;
}
@Override
public int getage() {
return this.age ;
}
@Override
public void setID(int x) {
this.ID = x ;
}
@Override
public int getID() {
return this.ID ;
}
}
Interesting Points about Abstract Classes
The following points about abstract classes are worth noting
■ An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract
superclass does not implement all the abstract methods, the subclass must be defined as abstract.
In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods
must be implemented. Also note that abstract methods are nonstatic.
■ An abstract class cannot be instantiated using the new operator, but you can still define its
constructors, which are invoked in the constructors of its subclasses. For instance.
■ A class that contains abstract methods must be abstract. However, it is possible to define an
abstract class that doesn’t contain any abstract methods. In this case, you cannot create instances
of the class using the new operator. This class is used as abase class for defining subclasses.
■ A subclass can override a method from its superclass to define it as abstract. This is
very unusual, but it is useful when the implementation of the method in the superclass
becomes invalid in the subclass. In this case, the subclass must be defined as abstract.
■ A subclass can be abstract even if its superclass is concrete. For example, the Object
class is concrete, but its subclasses, such as GeometricObject, may be abstract.
■ You cannot create an instance from an abstract class using the new operator, but an
abstract class can be used as a data type. Therefore, the following statement, which
creates an array whose elements are of the GeometricObject type, is correct.
GeometricObject[] objects = new GeometricObject[10];
An Interface
- An interface is a class-like construct that contains only constants and abstract methods. In
many ways an interface is similar to an abstract class, but its intent is to specify common
behavior for objects of related classes or unrelated classes.
- An interface in java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract
methods in the Java interface, not method body.
- An interface is treated like a special class in Java. Each interface is compiled into a
separate
bytecode file, just like a regular class. You can use an interface more or less the same way
you use an abstract class. For example, you can use an interface as a data type for a
reference
variable, as the result of casting, and so on. As with an abstract class, you cannot create an
instance from an interface using the new operator.
What does abstraction mean ?! We will answer that question Later
How to declare an interface?
• An interface is declared by using the interface keyword. It provides
total abstraction; means all the methods in an interface are declared
with the empty body, and all the fields are public, static and final by
default. A class that implements an interface must implement all the
methods declared in the interface.
Syntax
interface A {
//abstract methods
//constants
}
public class B implements A { //@Override Methods }
• “ The relationship between the class and the interface is known as interface inheritance. Since interface
inheritance and class inheritance are essentially the same, we will simply refer to both as inheritance. “
• “Interface fields are public, static and final by default, and the methods are public and abstract”
• “Since all data fields are public static final and all methods are public abstract
in an interface, Java allows these modifiers to be omitted.”
• For example
interface A {
void m1();
}
class B implements A {
@Override
void m1() {
System.out.println("m1");
}
}
Lets code
An interface is declared by using “ interface” keyword
Overridden methods, and notice that w don’t use
abstract Keyword to override it, but an interface
by default abstracted it
That’s how to inherits the interface
to class.
The Comparable interface
The Comparable interface defines the compareTo method for comparing objects
Suppose you want to design a generic method to find the larger of two objects of the same
type, such as two students, two dates, two circles, two rectangles, or two squares. In order to
accomplish this, the two objects must be comparable, so the common behavior for the objects
must be comparable. Java provides the Comparable interface for this purpose. The interface
is defined as follows:
// Interface for comparing objects, defined in java.lang
package java.lang;
public interface Comparable<E> {
public int compareTo(E o);
}
The compareTo method determines the order of this object with the specified object o and
returns a negative integer, zero, or a positive integer if this object is less than, equal to, or
greater than o.
The Comparable interface is a generic interface. The generic type E is replaced by a
concrete type when implementing this interface.
Examples
The Cloneable Interface
The Cloneable interface specifies that an object can be cloned
Often it is desirable to create a copy of an object. To do this, you need to use the clone
method and understand the Cloneable interface.
An interface contains constants and abstract methods, but the Cloneable interface is a
special case. The Cloneable interface in the java.lang package is defined as follows:
package java.lang;
public interface Cloneable {
}
This interface is empty. An interface with an empty body is referred to as a marker
interface. A marker interface does not contain constants or methods. It is used to denote
that a class
possesses certain desirable properties. A class that implements the Cloneable interface is
marked cloneable, and its objects can be cloned using the clone() method defined in the
Object class
Many classes in the Java library (e.g., Date, Calendar, and ArrayList) implement
Cloneable. Thus, the instances of these classes can be cloned. For example, the
following code
Calendar calendar = new GregorianCalendar(2013, 2, 1);
Calendar calendar1 = calendar;
Calendar calendar2 = (Calendar)calendar.clone();
System.out.println("calendar == calendar1 is " +(calendar == calendar1));
System.out.println("calendar == calendar2 is " + (calendar == calendar2));
System.out.println("calendar.equals(calendar2) is " +
calendar.equals(calendar2));
displays
calendar == calendar1 is true
calendar == calendar2 is false
calendar.equals(calendar2) is true
Interfaces VS Abstract Classes
“A class can implement multiple interfaces, but it can only extend one
superclass “ and that’s the main point, you can implements more than one
interface, but you cannot extend more than one abstract class. In other word you
can inherits just only one abstract class, but you can inherits more than one
interface
Abstraction in Java side Information
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.
How to achieve the abstraction ?
- Abstract classes
- Interfaces
“ In general, interfaces are preferred over abstract classes because an
interface can define a common super type for unrelated classes.
Interfaces are more flexible than classes. “
Thanks for attention

More Related Content

PPTX
Abstract Class & Abstract Method in Core Java
PPT
Java interfaces
PPT
Spring Core
PPSX
Data Types & Variables in JAVA
PPTX
Final keyword in java
PPTX
Lambda Expressions in Java 8
PPTX
Inheritance
Abstract Class & Abstract Method in Core Java
Java interfaces
Spring Core
Data Types & Variables in JAVA
Final keyword in java
Lambda Expressions in Java 8
Inheritance

What's hot (20)

PPTX
Classes and Objects in C#
PDF
Java 8 Lambda Built-in Functional Interfaces
PDF
Generics
PPT
sets and maps
PPT
standard template library(STL) in C++
PPT
Exception Handling in JAVA
PDF
Java 8 features
ODP
Introduction to Java 8
PDF
Java 8 Lambda Expressions
PPTX
Inner class
PPTX
Java Lambda Expressions.pptx
PDF
Collections Api - Java
PPTX
Super keyword in java
PDF
Java Collection framework
PPTX
Method Overloading in Java
PPS
Interface
PDF
PPTX
Multiple inheritance possible in Java
PPTX
Arrays in java
PPT
String Handling
Classes and Objects in C#
Java 8 Lambda Built-in Functional Interfaces
Generics
sets and maps
standard template library(STL) in C++
Exception Handling in JAVA
Java 8 features
Introduction to Java 8
Java 8 Lambda Expressions
Inner class
Java Lambda Expressions.pptx
Collections Api - Java
Super keyword in java
Java Collection framework
Method Overloading in Java
Interface
Multiple inheritance possible in Java
Arrays in java
String Handling
Ad

Similar to Abstraction in java [abstract classes and Interfaces (20)

PDF
java-06inheritance
PDF
Abstraction in Java: Abstract class and Interfaces
PPT
ABSTRACT CLASSES AND INTERFACES.ppt
PPTX
Abstract Class and Interface for Java Intoductory course.pptx
PDF
Java abstract Keyword.pdf
PPT
Java interfaces & abstract classes
PDF
Java 06
PPT
OOPS_AbstractClasses_explained__java.ppt
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
PPT
Java inheritance
PDF
Advanced Programming _Abstract Classes vs Interfaces (Java)
PPTX
Abstraction in java.pptx
PPTX
06_OOVP.pptx object oriented and visual programming
PPTX
Lecture 18
PDF
Abstract classes and Methods in java
PPT
Chap11
PPT
9781439035665 ppt ch10
DOC
Interface Vs Abstact
PPT
Chapter 9 Interface
java-06inheritance
Abstraction in Java: Abstract class and Interfaces
ABSTRACT CLASSES AND INTERFACES.ppt
Abstract Class and Interface for Java Intoductory course.pptx
Java abstract Keyword.pdf
Java interfaces & abstract classes
Java 06
OOPS_AbstractClasses_explained__java.ppt
Java OOP Programming language (Part 6) - Abstract Class & Interface
Java inheritance
Advanced Programming _Abstract Classes vs Interfaces (Java)
Abstraction in java.pptx
06_OOVP.pptx object oriented and visual programming
Lecture 18
Abstract classes and Methods in java
Chap11
9781439035665 ppt ch10
Interface Vs Abstact
Chapter 9 Interface
Ad

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Transform Your Business with a Software ERP System
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
System and Network Administraation Chapter 3
PDF
Nekopoi APK 2025 free lastest update
PDF
medical staffing services at VALiNTRY
PPTX
L1 - Introduction to python Backend.pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
CHAPTER 2 - PM Management and IT Context
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Digital Systems & Binary Numbers (comprehensive )
Computer Software and OS of computer science of grade 11.pptx
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Transform Your Business with a Software ERP System
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Which alternative to Crystal Reports is best for small or large businesses.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Wondershare Filmora 15 Crack With Activation Key [2025
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
System and Network Administraation Chapter 3
Nekopoi APK 2025 free lastest update
medical staffing services at VALiNTRY
L1 - Introduction to python Backend.pptx
Understanding Forklifts - TECH EHS Solution
Designing Intelligence for the Shop Floor.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
CHAPTER 2 - PM Management and IT Context

Abstraction in java [abstract classes and Interfaces

  • 2. Notes before starting • Red color font for external sources - javatpoint.com - docs.oracle.com - beginnersbook.com • Orange color font for” Introduction to Java programming comprehensive version [Tenth Edition] written by Y. Daniel Liang” source Also self effors
  • 3. Abstract Class - An abstract class cannot be used to create objects. An abstract class can contain abstract methods, which are implemented in concrete subclasses. - An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be sub classed. Let us discuss a point. All of us know the regular class. A class is basic building block of an object-oriented language. So what does it mean by abstract class ?. we can answer it later. But first we need to show up some concepts may you faced it before such as Inheritance.
  • 4. Overview about Inheritance • Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Why use inheritance in java ? • For Method Overriding (so runtime polymorphism can be achieved). • For Code Reusability. Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.
  • 5. Terms used in Inheritance • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
  • 6. “ In the inheritance hierarchy, classes become more specific and concrete with each new subclass. If you move from a subclass back up to a superclass, the classes become more general and less specific. Class design should ensure that a superclass contains common features of its subclasses. Sometimes a superclass is so abstract that it cannot be used to create any specific instances. Such a class is referred to as an abstract class.” Which means every subclass you create its has own behaviors and fields but also it has a common behaviors which inheritances it from parent classes. Also when you check the pervious diagram of inheritance types especially hierarchal type, you will find the class C and B have common behaviors which are inherited from parent. That make us wonder if we have more than C and B classes and all of them are extended from A class which means all of them have common behaviors. For ex: our School it has students, Professors and employees, if we need a program that tells the user who are the students of that school ?, who are the professors ? And who are employees ? Just give the user their names and few data. We notice that all of them students, professors and employees have common methods Public string getName () { return name ; } and so on for getAge() , getID() …
  • 7. Conclusion • If we implement it through only the inheritance concept, we will face a problem that all of them have different fields and methods also there are some important methods we need to ensure that some methods will be overridden like getters. Now we are close enough to get what the abstract class is. “a superclass is so abstract that it cannot be used to create any specific instances. Such a class is referred to as an abstract class.” and that the answer of our per question “what does it mean by abstract class ?”
  • 8. How to use In Java ? • 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. for example abstract class A {}
  • 9. Abstract Methods • “An abstract method is defined without implementation. Its implementation is provided by the subclasses. A class that contains abstract methods must be defined as abstract. The constructor in the abstract class is defined as protected, because it is used only by subclasses. When you create an instance of a concrete subclass, its superclass’s constructor is invoked to initialize data fields defined in the superclass” • A method without body (no implementation) is known as abstract method. For example abstract class A { abstract void print() ; }
  • 10. Simple example • This example show how to create abstract class and methods public abstract class A { public abstract void print(); } public class B extends A { @Override Public void print(){ System.out.println(“Hello World!”) } } Class B extends Class A Abstract methods must be overridden in subclasses Abstract methods must be defined in abstract classes Let’s code…..
  • 11. Code of simple example public abstract class Faculty { public abstract void setName (String x) ; public abstract String getName () ; public abstract void setAge(int x) ; public abstract int getage () ; public abstract void setID (int x) ; public abstract int getID () ; }
  • 12. public class Professors extends Faculty { private String name ; private int age ; private int ID ; private int salary ; public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } @Override public void setName(String x) { this.name = x ; } @Override public String getName() { return this.name ; } @Override public void setAge(int x) { this.age = x ; } @Override public int getage() { return this.age ; } @Override public void setID(int x) { this.ID = x ; } @Override public int getID() { return this.ID ; } } public class Student extends Faculty { private String name ; private int age ; private int ID ; private int score ; private int level ; public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } @Override public void setName(String x) { this.name = x ; } @Override public String getName() { return this.name ; } @Override public void setAge(int x) { this.age = x ; } @Override public int getage() { return this.age ; } @Override public void setID(int x) { this.ID = x ; } @Override public int getID() { return this.ID ; } }
  • 13. Interesting Points about Abstract Classes The following points about abstract classes are worth noting ■ An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as abstract. In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented. Also note that abstract methods are nonstatic. ■ An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. For instance. ■ A class that contains abstract methods must be abstract. However, it is possible to define an abstract class that doesn’t contain any abstract methods. In this case, you cannot create instances of the class using the new operator. This class is used as abase class for defining subclasses.
  • 14. ■ A subclass can override a method from its superclass to define it as abstract. This is very unusual, but it is useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be defined as abstract. ■ A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. ■ You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. Therefore, the following statement, which creates an array whose elements are of the GeometricObject type, is correct. GeometricObject[] objects = new GeometricObject[10];
  • 15. An Interface - An interface is a class-like construct that contains only constants and abstract methods. In many ways an interface is similar to an abstract class, but its intent is to specify common behavior for objects of related classes or unrelated classes. - An interface in java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. - An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. You can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a reference variable, as the result of casting, and so on. As with an abstract class, you cannot create an instance from an interface using the new operator. What does abstraction mean ?! We will answer that question Later
  • 16. How to declare an interface? • An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface. Syntax interface A { //abstract methods //constants } public class B implements A { //@Override Methods }
  • 17. • “ The relationship between the class and the interface is known as interface inheritance. Since interface inheritance and class inheritance are essentially the same, we will simply refer to both as inheritance. “ • “Interface fields are public, static and final by default, and the methods are public and abstract” • “Since all data fields are public static final and all methods are public abstract in an interface, Java allows these modifiers to be omitted.” • For example interface A { void m1(); } class B implements A { @Override void m1() { System.out.println("m1"); } } Lets code An interface is declared by using “ interface” keyword Overridden methods, and notice that w don’t use abstract Keyword to override it, but an interface by default abstracted it That’s how to inherits the interface to class.
  • 18. The Comparable interface The Comparable interface defines the compareTo method for comparing objects Suppose you want to design a generic method to find the larger of two objects of the same type, such as two students, two dates, two circles, two rectangles, or two squares. In order to accomplish this, the two objects must be comparable, so the common behavior for the objects must be comparable. Java provides the Comparable interface for this purpose. The interface is defined as follows: // Interface for comparing objects, defined in java.lang package java.lang; public interface Comparable<E> { public int compareTo(E o); } The compareTo method determines the order of this object with the specified object o and returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than o. The Comparable interface is a generic interface. The generic type E is replaced by a concrete type when implementing this interface.
  • 20. The Cloneable Interface The Cloneable interface specifies that an object can be cloned Often it is desirable to create a copy of an object. To do this, you need to use the clone method and understand the Cloneable interface. An interface contains constants and abstract methods, but the Cloneable interface is a special case. The Cloneable interface in the java.lang package is defined as follows: package java.lang; public interface Cloneable { } This interface is empty. An interface with an empty body is referred to as a marker interface. A marker interface does not contain constants or methods. It is used to denote that a class possesses certain desirable properties. A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method defined in the Object class
  • 21. Many classes in the Java library (e.g., Date, Calendar, and ArrayList) implement Cloneable. Thus, the instances of these classes can be cloned. For example, the following code Calendar calendar = new GregorianCalendar(2013, 2, 1); Calendar calendar1 = calendar; Calendar calendar2 = (Calendar)calendar.clone(); System.out.println("calendar == calendar1 is " +(calendar == calendar1)); System.out.println("calendar == calendar2 is " + (calendar == calendar2)); System.out.println("calendar.equals(calendar2) is " + calendar.equals(calendar2)); displays calendar == calendar1 is true calendar == calendar2 is false calendar.equals(calendar2) is true
  • 22. Interfaces VS Abstract Classes “A class can implement multiple interfaces, but it can only extend one superclass “ and that’s the main point, you can implements more than one interface, but you cannot extend more than one abstract class. In other word you can inherits just only one abstract class, but you can inherits more than one interface
  • 23. Abstraction in Java side Information 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. How to achieve the abstraction ? - Abstract classes - Interfaces “ In general, interfaces are preferred over abstract classes because an interface can define a common super type for unrelated classes. Interfaces are more flexible than classes. “