2. INHERITANCE & INTERFACES
Syllabus:
• overview of inheritance, working with
subclasses and superclasses,
• overriding methods in the super class,
introducing polymorphism,
• creating and extending abstract classes,
• polymorphism in the JDK foundation classes,
• using Interfaces,
• using the List Interface,
• introducing Lambda expressions
3. overview of inheritance
One class is hiring properties from another class is
called inheritance.
Advantages: Reusability
Class ClassA
{
}
Class ClassB extends ClassA
{
}
4. subclasses and superclasses
A class whose features are inherited is known as
super class(or a base class or a parent class).
here ClassA is called super class or base class or
parent class
A class that inherits the other class is known as
sub class (or derived class or a child class)
here ClassB is called sub class or derived class or
child class.
5. Inheritance Types:
• Single (Example: SingleInheritanceEx.java)
• Multilevel(Example:multilevel.java)
• Hierarchical(Example:hierarchical.java)
Using interface it is possible:
• Hybrid
• Multiple (using Interface:
Example:interfaceEx.java)
6. Single inheritance:
• One base class and one derived class
• One-to-one relationship
Base Class
Derived Class
Bank
protected int num1
void disp1()
Customer
int num2
void disp2(){
num1+num2}
7. Multilevel inheritance:
• One class is going to become base class for other derived class
Science
private bonus=2000
void disp1(){}
Computer
private bonus=3000
void disp2(){}
Faculty
double salary,totalSalary=0;
Faculty(){ salary=25000}
Base Class for class Science
Derived Class from Faculty and
Base Class for Computer
Derived Class from Science
Example
8. Hierarchical inheritance:
• One base class and multiple derived class, one –to-many relationship
SBI
Private IFSCCode
Bank(){rateofInterest=4.5}
PNB
Private IFSCCode
Bank(){rateofInterest=5.5}
Bank
rateofInterest
Bank(){rateofInterest=0} Example
9. Important Notes:-
• Inheritance Example(SingleInheritanceEx.java)(Single
Inheritance) or Relationship between one to one class.
• Default parent class constructor automatically called by child class but
for calling parameterized constructor of base class we need to use super
keyword.
• Example(inheritDefaultConstructorEx.java)-Default parent
class constructor calling in child class
• Example(inheritParameterizedConstructorEx.java)- parent
class parameterized constructor calling in child class(used
super)
• Example(callingMemberMethodUsingSuperEx.java)-calling
datamember and method using Super
10. Super keyword
• super keyword can be used to access the
immediate parent class constructor.
• super keyword can be used to invoke
immediate parent class members and
methods.
• In some scenario when a derived class and
base class has same data members or
methods, in that case super keyword also
used.
• Example: superEx.java
11. Method overriding
• Overriding Example:OverridenEx.java
• Difference between method overloding and
method overriding
Next Slide
12. Method Overloading Method Overriding
This is the example of compile time
polymorphism
This is the example of run time
polymorphism
Method overloading is performed within same
class.
Method overriding is performed within
different classes.
In case of method overloading, method
signature must be different.
In case of method overriding, method
signature must be same.
You can perform overloading on static
methods.
You cannot perform overriding on static
methods.
Example: Example:
13. interface
• Collection of abstract methods.
• Use keyword interface
• Achieve multiple inheritance
• Use implements keyword for calling in derive class
Syntax:
interfcae Bank
{
int rateOfInterest();
}
class SBI implements Bank {
int rateOfInterest()
{
retutn 5;
}
14. Abstract Class (Example: abstractEx.java)
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• A class which is declared with the abstract keyword
is known as an abstract class in Java.
• It holds both abstract and non-abstract methods.
• It can have constructor also
• We can achieve abstraction by using either abstract
class or interface.
• It can’t be instantiated
• The abstract class can also be used to provide some
implementation of the interface.
15. Q: Difference between abstract class and
interface
• Ans:Abstract class vs Interface in Java
• 1) First and the major difference between abstract class and an interface is
that an abstract class is a class while the interface is an interface, means by
extending the abstract class you cannot extend another class because Java
does not support multiple inheritances but you can implement multiple
inheritance in Java.
2) The second difference between interface and abstract class in Java is
that you cannot create a non-abstract method in an interface, every
method in an interface is by default abstract, but you can create a non-
abstract method in abstract class. Even a class which doesn't contain
any abstract method can be made abstract by using the abstract keyword.
16. 3) The third difference between abstract class vs interface in Java is that
interface is better suited for Type declaration and abstract class is more
suited for code reuse and evolution perspective. The Effective Java has one
item dedicated to explaining about why you should be using interface for
type declaration. You should check that out as well.
4) The fourth difference between abstract class and interface in Java is that
abstract class are slightly faster than interface because interface involves a
search before calling any overridden method in Java. This is not a
significant difference in most of the cases but if you are writing a time critical
application then you may not want to leave any stone unturned.
5) Another notable difference between interface and abstract class is
that when you add a new method in existing interface it breaks all its
implementation and you need to provide an implementation in all clients
which is not good. By using an abstract class, you can provide a default
implementation for a new method in the superclass without breaking existing
clients.
17. List interface
• List Interface is implemented
by ArrayList, LinkedList, Vector and Stack class
es.
Creating List Objects:
List is an interface, and the instances of List
can be created in the following ways:
List a = new ArrayList(); List b = new
LinkedList(); List c = new Vector(); List d = new
Stack();
Working with List Interface Example
18. introducing Lambda expressions
• Lambda expression is a new feature of Java
which was included in Java SE 8.
• In programming, a Lambda expression (or
function) is just an anonymous function, i.e., a
function with no name
• The Lambda expression is used to provide the
implementation of an functional interface ,we
don't need to define the method again for
providing the implementation.
• Note: An interface which has only one abstract
method is called functional interface.
19. Java Lambda Expression Syntax
(argument-list) -> {body};
1) Argument-list: It can be empty or non-empty
2) Arrow-token: It is used to link arguments-list
and body of expression.
3) Body: It contains expressions and statements
for lambda expression.
Example: lambdaEx.java
lambdaEx2.java