Posts

Showing posts from January, 2019

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...