Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Interfaces and Inner Classes. Show all posts
Showing posts with label Interfaces and Inner Classes. Show all posts

A simple program to demonstrate working of Inner Classes in Java

What do we mean by Inner Classes in Java ?.

Inner Classes in Java provides a way to place definition of a class within another class. The class containing an inner class is termed as enclosing class.   




A simple program to demonstrate working of Inner Classes in Java

package com.hubberspot.examples.inner;

public class InnerClassTest {

   public Customer getCustomer() {
 return new Customer();
   }

   class Customer {

 private Integer id;
 private String firstName; 
 private String lastName;
 private String email;

 public Customer(){

        }

 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 } 
   }

   public static void main(String[] args) {

 InnerClassTest outer = new InnerClassTest();  
 Customer customer = outer.getCustomer();

 customer.setId(1);
 customer.setFirstName("jonty");
 customer.setLastName("magicman");
 customer.setEmail("jonty@magicman.com");

 System.out.println("FirstName : " + customer.getFirstName());
 System.out.println("LastName : " + customer.getLastName());
 System.out.println("Email : " + customer.getEmail());

   }

}




Output of the program :




Interfaces Vs Abstract Classes in Java

Introduction 
In this section of blog, we will look into comparison between Interfaces and Abstract Classes. Generally, programmers often get confuse at the time of design that, when to use interfaces and when to use abstract classes. It all depends on class design. An abstract class is a class which is incomplete, it can contain data members, constructors, method definitions, static fields and methods. So it has already functionality defined as compared to interface. A class which extends an abstract class inherits all this. When say a class tyre extends an abstract class car, objects of class tyre are also of type car.

Interfaces are used when one or more classes need the functionality of the interface. It is not necessary that this class must have any relationship with interface. Let say we have two classes class Employee and class Rectangle, both class can implement Comparable interface for comparing their objects. But, none of the class Employee and Rectangle has any relationship with the Comparable interface.

Comparison between Interfaces and Abstract Classes

1. An Abstract class is an Incomplete class, whereas an Interface is a pure abstract class. 

2. An Abstract class generally defines common behaviour and attributes that a class inherits, whereas an Interface specifies functionality which a class should have.

3. An Abstract class contains method declarations and definitions, whereas an Interface contains only method declarations.

4. An Abstract class can contain fields which can be static, final, as well as instance fields, where as Interface fields are implicitly final and static.

5. An Abstract class can have a constructor, whereas an Interface cannot have a constructor.

6. An Abstract class is used with extends keyword, whereas an Interface is used with implements keyword.

7. An Abstract class can be extended by more than one class, whereas an Interface can be implemented by more than one class.

8. A class can extend only one abstract class, whereas a class can implement more than one Interface.

9. An Abstract class can be extended by another abstract as well as concrete class, whereas an Interface can be extended by another Interface.

10. A class cannot extend more than one class, whereas an Interface can be used to implement multiple inheritance. 


 
© 2021 Learn Java by Examples Template by Hubberspot