`
standalone
  • 浏览: 613139 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

can we create an object of an interface?

    博客分类:
  • java
阅读更多
Definitely, the answer is no!
But look at the code below:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class EmployeeSort {

	static Comparator<Employee> NAMESORT = new Comparator<Employee>(){

		public int compare(Employee e1,Employee e2){
			return e1.name.compareTo(e2.name);
		}


	};

	public static void print(List<Employee> empList){
		for (Employee o :empList){
			System.out.println(o.name);
		}
	}

	public static void main(String[] args) {
		List<Employee> empList = new ArrayList<Employee>();
		Employee e = new Employee("anil");
		Employee e1 = new Employee("babe");
		Employee e2 = new Employee("akhil");

		empList=Arrays.asList(e,e1,e2);

	    print(empList);

		Collections.sort(empList,NAMESORT);
		 print(empList);



	}

}


How do you explain the code snippet below?
 static Comparator<Employee> NAMESORT = new Comparator<Employee>(){

		public int compare(Employee e1,Employee e2){
			return e1.name.compareTo(e2.name);
		}


	};


Actually this expression instantiates a new object from an unnamed and previously undefined class, which automatically implements the interface named Comparator, and automatically extends the class named Object. The class can explicitly implement one, and only one interface, and cannot extend any class other than Object. Once again, the body of the new class is given by classBody.

I got answer from stackoverflow: http://stackoverflow.com/questions/2978849/can-anybody-explain-the-working-of-following-code?rq=1

More detailed information you can refer to the original article: http://www.developer.com/java/other/article.php/3300881/The-Essence-of-OOP-using-Java-Anonymous-Classes.htm
分享到:
评论

相关推荐

    JSP Simple Examples

    To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file. Setting Colors ...

    java英文笔试

    What is the difference between an abstract class and an interface? **Answer**: In Java, an abstract class can have both abstract and non-abstract (concrete) methods, whereas an interface can only ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    We can declare data members of type Foo* or Foo&. We can declare (but not define) functions with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a...

    XLSReadWriteII.v.5.20.67.D7.D2010-XE10

    With the formatting interface, you can format cells through a cell object. Fast and efficient support when you have any questions. No runtime fees for the component. Full source code included. We ...

    IOS5 Programming Cookbook

    Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....

    XLSReadWriteII.v.5.20.67.D7.D2010-XE10.rar

    With the formatting interface, you can format cells through a cell object. Fast and efficient support when you have any questi ons. No runtime fees for the component. Full source code included. We ...

    Firebird Maestro 17.1 带破解

    For example, you can perform operations with a group of objects as well as with a single object in Object Manager, sort, group and filter the database objects within Object Browser, copy an object ...

    Cours-VB-NET-2010-Developpez-com

    For example, a car can be considered an object with properties (color, make, model) and behaviors (drive, stop). ##### 2. In Visual Basic .NET In VB.NET, objects are used extensively. They are the ...

    Java邮件开发Fundamentals of the JavaMail API

    If you're viewing this document online, you can click any of the topics below to link directly to that section. 1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related ...

    Universal-USB-Installer

    When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and ...

    外文翻译 stus MVC

    Putting a great looking site together takes a lot of work, and most Java developers are more interested in creating a great looking object interface than a user interface. JavaServer Pages (JSP) ...

    Dundas.Chart.for.Winform.Enterprise.v7.1.0.1812.for.VS2008

    Custom label enhancements - New to Version 5.0, developers can associate a custom label to an image, a tooltip, an HREF or even an object Improved Data Labeling - Developers now have the opportunity ...

    beginning VisualBasic2010(英文版)入门必读

    buttons, forms, display dialog boxes, and create menus.Chapter 10 provides an introduction to application debugging and error handling.Chapters 11 through 13 provide an introduction to object–...

    VB编程资源大全(英文源码 控件)

    &lt;END&gt;&lt;br&gt;38,ShutDnCtl.zip Shutdown Control v1.5 This has been tested on Windows 9x, me, NT and 2000 operating systems.&lt;END&gt;&lt;br&gt;39,Margin.zip With the help of this Program we can Leave space...

    Odoo 11 Development Essentials 3 Edition(epub)

    Chapter 9, Backend Views – Design the User Interface, covers the web client's View layer, explaining the several types of views in detail and all the elements that can be used to create dynamic and ...

    VB编程资源大全(英文源码 其它)

    sprite1.zip This is an Excellent example on how to use sprites in your program.&lt;END&gt;&lt;br&gt;18 , charcreate.zip This is an example of how to assign "characters" to differant pictureboxes. This ...

    基于ssm+mysql的喀什古城旅游网源码数据库论文.doc

    ion software can direct tens of thousands of enterprise employees to carry out production tasks in an orderly manner. Just imagine, the power of information software is enough to shake the entire ...

    C# Game Programming Cookbook for Unity 3D - 2014

    4.9 Automatic Object Spinner............................................79 4.9.1 Script Breakdown..........................................80 ix Contents 4.10 Scene Manager...............................

    ios application programming guide

    3. **Swift Programming Language**: An overview of Swift, the recommended language for iOS development, including syntax, data types, control structures, and object-oriented programming concepts. ...

Global site tag (gtag.js) - Google Analytics