Сообщения

Сообщения за февраль, 2021

Inheritance and Polymorphism. Upcasting and Downcasting

 *Inheritance.  Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class). extends  is the keyword used to inherit the properties of a class. Example: package Main ; public class Car { private Integer speed ; private String color ; private Integer seatCount ; public Integer getSpeed () { return speed ; } public void setSpeed (Integer speed) { this . speed = speed ; } public String getColor () { return color ; } public void setColor (String color) { this . color = color ; } public Integer getSeatCount () { return seatCount ; } public void setSeatCount (Integer

Static block, Non-static(instance) block, state, behaviour

 There are two types of blocks static and non-static blocks. Static block runs first. But when we create an object of class first runs static block and then non-static block and after constructor of particular class. package Main ; public class Calculator { public static int a ; public int b ; static { System. out .println( "static block loaded" ) ; sum ( 1 , 2 ) ; a ++ ; } { System. out .println( "non static block loaded" ) ; b ++ ; a ++ ; foo() ; sum ( 1 , 2 ) ; } public Calculator () { System. out .println( "Constructor of a class" ) ; } public static double sum ( double a , double b){ return a + b ; } public void foo (){ System. out .println( "salam" ) ; } } So we can load a class by typing: Class.forName("Main.Calculator"); inside we have to mention package name and class name. package Main ; publi

JDBC - Java Database Connectivity

Изображение
 I started to work JDBC project on Maven. First of all we have to add properties and dependency for it: <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies> so version in the future may be different so please pay attention to it. Then via Java we have to connect to our Database(DB). String url = "jdbc:mysql://localhost:3306/resume?serverTimezone = UTC"; String username = "root"; String password = "112358"; Connection c = DriverManager. getConnection (url, username, password); Statement stmt = c.createStatement(); stmt.execute("select * from user"); ResultSet rs = stmt.getResultSet(); Co