0%
Loading ...

Java Tutorials

This category contains Java basic tutorials

Interface in Java

In this tutorial, we will learn about Interface in Java. Interface is like an blue print of class. An Interface contain all its variable as static, final and all its methods as abstract. You can say an Interface is a complete Abstract class. We can not instantiate interface same as abstract class we can only use an interface by inheriting it our class. Some important things to remember about interface. Interface contain all the final and static variables Interface contain all abstract methods we use interface keyword to create new interface To inherit interface in class we use implements keyword. Example Program: // Interface interface Bike { public void speed(); // interface method without body public void price(); // interface method without body } //Pulser public class Pulser implements Bike { public void speed() { System.out.println(“Max Speed 134 km/h”); } public void price() { System.out.println(“Price Rs. 1.18 lakh onwards”); } public static void main(String[] args) { Pulser obj = new Pulser(); obj.speed(); obj.price(); } } Output: Max Speed 134 km/h Price Rs. 1.18 lakh onwards Multiple Inheritance: It is important to note we can perform multiple inheritances in case of the interface. It means we can inherit multiple interfaces in one class. Check out the following example. // Interface interface Bike { public void speed(); // interface method without body public void price(); // interface method without body } interface ExtraFeatures{ public void showExtraFeatures(); // interface method without body } //Pulser public class Pulser implements Bike, ExtraFeatures { public void speed() { System.out.println(“Max Speed 134 km/h”); } public void price() { System.out.println(“Price Rs. 1.18 lakh onwards”); } public void showExtraFeatures(){ System.out.println(“Tubeless Tyre, single-channel ABS”); } public static void main(String[] args) { Pulser obj = new Pulser(); obj.speed(); obj.price(); obj.showExtraFeatures(); } } Output: Max Speed 134 km/h Price Rs. 1.18 lakh onwards Tubeless Tyre, single-channel ABS   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Interface in Java Read More »

Abstract class in Java

In this tutorial, we will learn about the Abstract class in Java. The main motive of creating an abstract class is to restrict the user to create an object of the class. Abstract classes can only be used with the help of inheritance means we can inherit the abstract class in class. Java provides us “abstract” which we can use to make a class abstract. When we define an abstract method in abstract class then we don’t provide the body of the method. we define body in child class while inheriting abstract class. The main reason why we make some classed abstract because at the time of making some classes we don’t know exact working of some methods so we create some abstract methods in the class. And we don’t want to allow any user to create an object of this incomplete class Important things to know about Abstract class: The abstract keyword is used to create an object of the class. An abstract class may have or may not have abstract methods We can not create an object of abstract class we can only inherit it. If there is an abstract method in abstract class then after inheritance we have to override abstract(to provide its working) method. Otherwise, the compiler will show an error. Here we will see an example in which we will create an abstract class called Person. In Person class we will create a abstract method called work and then we will create two other classes called Student and Teacher. Example Program: abstract class Person{ String name; int age; public void add( String name,int age){ this.name=name; this.age=age; } //abstract methoth work abstract public void work(); public void show(){ System.out.println(“Hello “+name+” your age is “+age); work(); } } public class Student extends Person{ //overriding abstract method work public void work(){ System.out.println(“Your work is to study”); } public static void main(String args[]){ Student obj=new Student(); obj.add(“Abc”,20); obj.show(); } } Output: Hello Abc your age is 20 Your work is to study If we will not override abstract work method in Student class. Then compiler will show us an error: Student is not abstract and does not override abstract method work() in Person public class Student extends Person{ ^ 1 error Now we will implement the same Person class in Teacher class and here we will again override the work method but with the different code. Example Program: abstract class Person{ String name; int age; public void add( String name,int age){ this.name=name; this.age=age; } //abstract method work abstract public void work(); public void show(){ System.out.println(“Hello “+name+” your age is “+age); work(); } } public class Teacher extends Person{ //overriding abstract method work public void work(){ System.out.println(“Your work is to teach students”); } public static void main(String args[]){ Teacher obj=new Teacher(); obj.add(“XYZ”,30); obj.show(); } } Output: Hello XYZ your age is 30 Your work is to teach students   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Abstract class in Java Read More »

Final Keyword in Java

In this tutorial, we will learn about the final keyword in Java. The main motive of the final keyword is to restrict the user and final keyword can be used to create three kinds of restriction. These are: final with variable final with method final with class Final keyword in Java with variable: The final keyword is to make the variable final(constant). Once you declared a variable with the final keyword you can not change the value of this final variable throughout the program. Example Program: class Math{ final static double pi=3.14; public static void main(String args[]){ System.out.println(“Value of Pi is “+pi); //pi=2.5; if you will try to assign value compiler will show an error } } Final keyword in Java with Method: When we make a method final in class it means we cannot override this method in child class. Example Program: class Math{ final static double pi=3.14; final public void squareRoot(int num){ System.out.println(“SQ ans is “+(num*num)); } } public class Calculator extends Math{ //if we try to override this function it will show an error /*public void squareRoot(int num){ System.out.println(“SQ ans is “+(num*num)); }*/ public static void main(String args[]){ Calculator cal=new Calculator(); cal.squareRoot(5); } } Final keyword with Class: When we use final keyword with a class, it means we can not inherit this class in any other class. The following code will show an error. final class Math{ final static double pi=3.14; public void squareRoot(int num){ System.out.println(“SQ ans is “+(num*num)); } } public class Calculator extends Math{ public static void main(String args[]){ Calculator cal=new Calculator(); cal.squareRoot(5); } } Have you ever tried to inherit String class in your class. You can not inherit String class because it is a final class. Click here to check. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Final Keyword in Java Read More »

Method Overriding in Java

In this tutorial, we will learn about Method Overriding in java. If we create a method in the child class with the same name as declared in parent class it is known as Method Overloading. Rules to perform Method Overloading in Java: Method name should be same as declared in Parent class. Parameter of the method should be the same as a method declared in Parent class. Example Program: class Master{ public void show(){ System.out.println(“This is show method in Master class”); } public void show2(){ System.out.println(“This is show2 method in Master class”); } } public class Student extends Master{ public void show(){ System.out.println(“This is show method in Student class”); } public static void main(String args[]){ Student obj=new Student(); obj.show(); obj.show2(); } } As in the above example, you can see the method show is already present in Master class but we are overriding this method in Student class. It means when we call this method with the help of object of Student class it will invoke the method which is defined inside the student class. Method overriding is very helpful when we want to change the working of the method which is already defined in Parent class. In this case, with the help Method Overriding, we can Override that particular method in Child class. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Method Overriding in Java Read More »

Super keyword in Java

In this tutorial we will learn about super keyword in Java. super keyword in java refers to object of Parent class. Super keyword is mainly use to call super class methods or constructor of super class. Usage of super keyword: The super keyword can be used to use the variable of parent class in child class. The super keyword can be used to call methods of the parent class. The super keyword can be used to invoke the constructor of the parent class. Example Program: In this example, how we can use the super keyword to use the variable from the parent class. class A{ int num=10; } class B extends A{ int num=20; public void show(){ System.out.println(num); //using num from B class System.out.println(super.num); //using num from A class } } public class MyClass{ public static void main(String args[]){ B obj=new B(); obj.show(); } } Example Program: In this example, we will see how we can use the super keyword to the class method of the parent class. class A{ int num=10; public void show(){ System.out.println(num); } } class B extends A{ int num=20; public void show(){ System.out.println(num); super.show(); //calling to show method of A class } } public class MyClass{ public static void main(String args[]){ B obj=new B(); obj.show(); } } Example Program: In this example, we will see how we can use the super keyword to invoke the constructor of the parent class. class A{ public A(int num){ System.out.println(“This is num “+num); } } class B extends A{ int num2; public B(int num,int num2){ super(num); this.num2=num2; } public void show(){ System.out.println(“This is Num2 “+num2); } } public class MyClass{ public static void main(String args[]){ B obj=new B(10,20); obj.show(); } }   Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Super keyword in Java Read More »

This Keyword in Java

In this tutorial we will learn about this Keyword in Java. In very simple words this keyword in Java references to object of the current class. At present, it might be sound confusing. But you will find it easy after checking the following example: Example code: Class ThisExample{ String name; public ThisExample(String name){ this.name=name; } public void show(){ System.out.println(“Hello “+name); } public static void main(String args[]){ ThisExample obj=new ThisExample(“Owlbuddy”); obj.show(); } } In the above example, you can see we have used this keyword inside the constructor of the class. If you will see very clearly in code you will find that variable name is defined to time. Once as a global variable in class and then as a local variable in Constructor. It means when we assign local name value to global name value that will be confusing for the compiler. But we used this keyword to overcome this problem. As we discussed earlier this keyword refers to the object of the current class. So it means when we wrote this.name it means we were referring to a global variable and when we wrote simple name it means were referring to a local variable of the constructor. This keyword can be used in several ways. such as in constructor overloading (we will cover this topic in upcoming tutorials), to differentiating global and local variable names in Methods(Same as Constructor) etc. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

This Keyword in Java Read More »

Inheritance in Java

In this tutorial, we will learn about Inheritance in Java. Inheritance is a mechanism in which we can inherit properties(methods and variable) of one class into another. This helps us to create a new class that will have its own properties and properties of its parent class(from which the new class is inheriting features). While performing inheritance you will see two kinds of classes as follow: Parent Class: The which being inherit. Child Class: The class which is inheriting features of another class: It is important to note, we use extends keyword to perform inheritance in Java. Types of Inheritance in Java: There is a total of three types of inheritance available in Java. These types are on the basis of the order in which classes being inherited. Single Level Inheritance Multi-Level Inheritance Hierarchical Inheritance I know students who have background in c++ might be missing Multiple Inheritance here. But Multiple Inheritance in classes is not available in Java. Single Level Inheritance: When a class inherit another class this is known as a single level inheritance. As in the given illustration you can see class B is inheriting to class A. Example Program: class A{ public void showA(){ System.out.println(“I am show method in A Class”); } } public class B extends A{ public void showB(){ System.out.println(“I am show method in B Class”); } public static void main(String args[]){ B obj=new B(); obj.showA(); obj.showB(); } } Multi-Level Inheritance: When inheritance performs in multiple classes and at several levels. This is known as Multi-Level inheritance. As in the given illustration you can see class B is inserting into class A and class C is inheriting to class B. In this case class C will have features of class A and class B because class C is inheriting to class B which already have features of class A. Example Program: class A{ public void showA(){ System.out.println(“I am show method in A Class”); } } class B extends A{ public void showB(){ System.out.println(“I am show method in B Class”); } } public class C extends B{ public void showC(){ System.out.println(“I am show method in C Class”); } public static void main(String args[]){ C obj=new C(); obj.showA(); obj.showB(); obj.showC(); } } Hierarchical Inheritance When multiple classes inherit to single class it is known ad Hierarchical Inheritance. As in the given illustration you can see class B and class C both are inheriting to class A. Example Program: class Greeting{ public void welcome(String str){ System.out.println(“Hello, We welcome you “+str); } } class Teacher extends Greeting{ public static void main(String args[]){ new Teacher().welcome(“Sir”); } } class Student extends Greeting{ public static void main(String args[]){ new Student().welcome(“dear Student”); } } In the above example, I have used an anonymous object. If don’t have any idea about the anonymous object. Click here to learn. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Inheritance in Java Read More »

Method Overloading in Java

In this tutorial, we will learn about Method Overloading in Java. When we create more than methods in a class with the same name it is known as Method Overloading in Java. The main motive behind this concept is to keep code more readable and easy. Some rules to perform method overloading in Java. Name of the method must be the same. Arguments must be changed (In count or type) Can change return type. Can change access modifier. Next, we will try to understand this concept with the help of an example. In Example, we will create a class called Calculator and we will create three add methods in this class(Mean we will overload add method). Example Program: class Calculator{ public int add(int x,int y){ return x+y; } public int add(int x,int x,int z){ return x+y+z; } public double add(double x,double y){ return x+y; } public static void main(String args[]){ Calculator obj=new Calculator(); System.out.println(obj.add(10,12)); System.out.println(obj.add(10,12,15)); System.out.println(obj.add(10.5,20.6)); } } In the above example, you can see we have created three add methods in Calculator class and each with different arguments(number or data type-wise) and with the same name. After that when we called them there is no confusion we are just calling add method and it automatically invokes to an appropriate method according to too actual arguments. Wait have you ever noticed. It doesn’t method what type(primary data types) of value in print or println method they always show output. How this is possible. Simple because they are overloaded in PrintStream class. Click here to check. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Method Overloading in Java Read More »

Static block in Java

In this tutorial, we will learn about Static block in Java. A very important thing about the static block is the static block belongs to class not to instance. That’s why static block only runs once it doesn’t matter how many numbers of instance we create. There are few things to note about the static block in Java. We do not use any identifier with static block but we use static keyword. Static block only runs once. Static block can even run even without creating an object of the class. Static block runs before the constructor and instance bock of the class. Example Program class Example{ //Static Block static{ System.out.println(“This is static block”); } //Constructor public Example(){ System.out.println(“This is constructor block”); } //main method public static void main(String args[]){ } } Output: This is static block As you can see we haven’t even created a single object of the class still our static block is running. Here is another example to show what if we will create constructor and instance blocks in class. Example Program public class Example{ //Static Block static{ System.out.println(“This is static block”); } //Instance Block { System.out.println(“This is instance block”); } //Constructor public Example(){ System.out.println(“This is constructor block”); } //main method public static void main(String args[]){ new Example(); new Example(); new Example(); } } Output: This is static block This is instance block This is constructor block This is instance block This is constructor block This is instance block This is constructor block As you can see in above example static block is only running once. On the other hand instance block and constructor running three time because we have created three objects of class. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Static block in Java Read More »

Instance block in Java

In this tutorial, we will learn about Instance block in Java. Instance blocks are used to initialize instance variables of the class. These blocks run every time when we create an object of the class. There are few things to note about instance blocks. we do not right any identifier with instance block instance block run every time we create an object of class it is not mandatory to create instance block in each class instance blocks run even before the constructor when we create an object of the class. Example Program: class Example{ //Instance Block { System.out.println(“This is instance block”); } //Constructor public Example(){ System.out.println(“This is constructor block”); } //main method public static void main(String args[]){ Example obj=new Example(); Example obj2=new Example(); } } Output: This is instance block This is constructor block This is instance block This is constructor block In above example you can see we have created two object of class and every time instance block is running before the constructor. Parvesh SandilaParvesh Sandila is a passionate web and Mobile app developer from Jalandhar, Punjab, who has over six years of experience. Holding a Master’s degree in Computer Applications (2017), he has also mentored over 100 students in coding. In 2019, Parvesh founded Owlbuddy.com, a platform that provides free, high-quality programming tutorials in languages like Java, Python, Kotlin, PHP, and Android. His mission is to make tech education accessible to all aspiring developers.​ new.owlbuddy.com

Instance block in Java Read More »

Scroll to Top
×