The document discusses abstraction in Java. There are two main ways to achieve abstraction - abstract classes and interfaces. Abstract classes can contain both abstract and non-abstract methods, while interfaces only contain abstract methods. Lambda expressions allow implementing functional interfaces using minimal syntax and are useful for collection operations like filtering and mapping.
The document discusses abstraction in Java. There are two main ways to achieve abstraction - abstract classes and interfaces. Abstract classes can contain both abstract and non-abstract methods, while interfaces only contain abstract methods. Lambda expressions allow implementing functional interfaces using minimal syntax and are useful for collection operations like filtering and mapping.
The document discusses abstraction in Java. There are two main ways to achieve abstraction - abstract classes and interfaces. Abstract classes can contain both abstract and non-abstract methods, while interfaces only contain abstract methods. Lambda expressions allow implementing functional interfaces using minimal syntax and are useful for collection operations like filtering and mapping.
The document discusses abstraction in Java. There are two main ways to achieve abstraction - abstract classes and interfaces. Abstract classes can contain both abstract and non-abstract methods, while interfaces only contain abstract methods. Lambda expressions allow implementing functional interfaces using minimal syntax and are useful for collection operations like filtering and mapping.
implementation details and showing only functionality to the user. • There are two ways to achieve abstraction in java Abstract class (0 to 100%) Interface (100%) Abstract class in Java • A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. • It needs to be extended and its method implemented. It cannot be instantiated. • Rule: If you are extending an abstract class that has an abstract method, you must either provide the implementation of the method or make this class abstract. • Class A • { • Abstract void run(); • } //CTE Interface in Java • An interface in java is a blueprint of a class. It has static constants and abstract methods. • interfaces can have abstract methods and variables. It cannot have a method body. • Java Interface also represents the IS-A relationship. • It cannot be instantiated just like the abstract class. • Since Java 8, we can have default and static methods in an interface. • Since Java 9, we can have private methods in an interface. Why use Java interface? • There are mainly three reasons to use interface. They are given below. • It is used to achieve abstraction. • By interface, we can support the functionality of multiple inheritance. • It can be used to achieve loose coupling. • Interface fields are public, static and final by default, and the methods are public and abstract. Multiple inheritance in Java by interface • If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. Java Inner Classes • Java inner class or nested class is a class which is declared inside the class or interface. • We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable. • Additionally, it can access all the members of outer class including private data members and methods. Syntax class Java_Outer_class{ //code class Java_Inner_class{ //code } } Advantage of java inner classes • 1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private. • 2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only. • 3) Code Optimization: It requires less code to write. Difference between nested class and inner class in Java • Inner class is a part of nested class. Non-static nested classes are known as inner classes. Types of Nested classes • There are two types of nested classes non- static and static nested classes. The non-static nested classes are also known as inner classes. • Non-static nested class (inner class) – Member inner class – Anonymous inner class – Local inner class • Static nested class Type Description
Member Inner Class A class created within class
and outside method.
Anonymous Inner Class A class created for
implementing interface or extending class. Its name is decided by the java compiler.
Local Inner Class A class created within method.
Static Nested Class A static class created within
class.
Nested Interface An interface created within
class or interface. Internal working of Java member inner class • The java compiler creates two class files in case of inner class. The class file name of inner class is "Outer$Inner". • If you want to instantiate inner class, you must have to create the instance of outer class. In such case, instance of inner class is created inside the instance of outer class. Java static nested class • A static class i.e. created inside a class is called static nested class in java. It cannot access non-static data members and methods. It can be accessed by outer class name. • It can access static data members of outer class including private. • Static nested class cannot access non-static (instance) data member or method. Static Inner Class Code Example • In this example, you need to create the instance of static nested class because it has instance method msg(). But you don't need to create the object of Outer class because nested class is static and static properties, methods or classes can be accessed without object. Java Nested Interface • An interface i.e. declared within another interface or class is known as nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. • The nested interface must be referred by the outer interface or class. It can't be accessed directly. Points to remember for nested interfaces • Nested interface must be public if it is declared inside the interface but it can have any access modifier if declared within the class. • Nested interfaces are declared static implicitly. Can we define a class inside the interface? • Yes, If we define a class inside the interface, java compiler creates a static nested class. interface M { class A{} } Java Lambda Expressions • Lambda expression is a new and important feature of Java which was included in Java SE 8. • It provides a clear and concise way to represent one method interface using an expression. • It is very useful in collection library. It helps to iterate, filter and extract data from collection. • The Lambda expression is used to provide the implementation of an interface which has functional interface. • Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. • Java provides an annotation @FunctionalInterface, which is used to declare an interface as functional interface. Functional Interface • An Interface that contains exactly one abstract method is known as functional interface. • It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class. • Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces. • It is a new feature in Java, which helps to achieve functional programming approach. Why use Lambda Expression • To provide the implementation of Functional interface. • Less coding. Syntax (argument-list) -> {body} Syntax Tear Down • Java lambda expression is consisted of three components. • 1) Argument-list: It can be empty or non- empty as well. • 2) Arrow-token: It is used to link arguments- list and body of expression. • 3) Body: It contains expressions and statements for lambda expression.