Posts

Differences between abstract class and interface in Java

Image
One of the most frequently asked questions on Java job interview: " What the difference between abstract class and interface in Java? " And in this post I will try to answer that question. The differences are following: The number of extendings . An abstract class can extend only one typical Java class or one abstract class at a time. But an interface can extend any number of interfaces at a time. What can extend . An abstract class can extend concrete (regular) class or abstract class. But interface can extend another interface only. Keyword "abstract" for methods . Keyword "abstract" is mandatory for declaring a method as an abstract in abstract class. But in interface that keyword is optional, because methods abstract here by default. Access modifiers for methods . An abstract class can have protected and public abstract methods. But interface can have public abstract methods only. Variables . An abstract class can have static, final or static f...

Ways of creating threads in Java

Image
In this post I will tell you about how to create threads in Java. So, let's get started! Threads in Java are objects. They are instances of java.lang.Thread class or its subclasses. There are two ways of creating threads in Java: First . We can just create an instance of Thread class . Thread thread = new Thread(); // And start it thread.start(); But this is useless, because our thread has no code to execute. Instead we can extend Thread class and create our subclass of Thread with custom code to execute. public class CustomThread extends Thread { public void run(){ System.out.println("Our custom thread is running"); } } CustomThread customThread = new CustomThread(); customThread.start(); In method run we can put all necessary code to execute in our thread. Also we can create anonymous subclass of Thread class. Thread thread = new Thread(){ public void run(){ System.out.println("This is anonymous subclass of Thread. And it...

Get links to all articles from site with python

Image
Hi, everybody! In this post will tell you about one small python script. Recently I have met task to get all links to articles from my blog. I have made some Google search and find some points to this task. I used python and its package BeautifulSoup for html parsing. Also used re module and urllib3 for http request. Here is it, my script. I will be happy if you find it usefull. from bs4 import BeautifulSoup import urllib3 import re links = [] site = "https://alextech18.blogspot.com" pattern = "://alextech18.blogspot.com" def getLinks(url, pattern, html_only=True): http = urllib3.PoolManager() html_page = http.request('GET', url) soup = BeautifulSoup(html_page.data, features="html.parser") # Use separate pattern, because, blogspot for example, can use links leading with http and https for link in soup.findAll('a', attrs={'href': re.compile(pattern)}): if link.get('href') not in links...

Simple network console chat on Java

In this post I will show you simple example of network chat on Java. Let's start with our task. Task You need to implement console network chat. There must be a server instance and a client instance. Server starts, wait for single connection. Client starts, connects to server. Than client can write a message in its console. Message will be send to server and appears in server console. Server can do the same. Client and server can close the chat. Let's start. First, create basic class for handling typical operations by client and server in our task: get input from Std.in, set output to socket, get input from socket. package chat; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class Client { private Scanner in; private Scanner input; private PrintWriter out; private Thread threadIn; private Thread threadOut; public Client(Socket sock, String name) { try { in =...