Java Basics & OOPs Concepts Explained with Examples

// 1.1. Basic Main Method
public class SampleProgram {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}
// 1.2. Main Method with Different Argument Name (Not necessary to keep it args)
public class SampleProgram {
    public static void main(String[] myArgs) {
        System.out.println("Arguments length: " + myArgs.length);
    }
}

Run this with:
java CommandLineDemo Hello Java

// 1.3. Main Method with Command Line Arguments
public class SampleProgram {
    public static void main(String[] args) {
        for (String arg : args) {
            System.out.println("Argument: " + arg);
        }

        System.out.println("Argument: " + arg[0]);
    }
}
// 1.Overloading main Method?
public class SampleProgram {
    public static void main(String[] args) {
        System.out.println("Main method called");
        main(5);
    }

    public static void main(int number) {
        System.out.println("Overloaded main: " + number);
    }
}
Data TypeSizeDefault Value
byte1 byte (8-bit)0
short2 bytes (16-bit)0
int4 bytes (32-bit)0
long8 bytes (64-bit)0L
float4 bytes (32-bit)0.0f
double8 bytes (64-bit)0.0d
char2 bytes (16-bit)'\u0000' (null char)
boolean~1 bit (JVM dependent)false
public class VariablesAndDatatypes {
	
	static int a;

	public static void main(String[] args) {
		
		int a = 10;
		int A = 10;
		String employeeName = "Amit";
		
		System.out.println("a: " + a);
		
		int age = 25;
		String name = "Rahul";
		double price = 99.99;
		boolean isActive = true;
		
		System.out.println(age);
		System.out.println(name);
		System.out.println(price);
		System.out.println(isActive);
		
		String city = "Delhi";
		int[] marks = {90, 85, 88};


	}

}
public class ControlAndConditionalStatements {

	public static void main(String[] args) {
		
		int age = 17;
		if (age >= 18) {
		    System.out.println("Eligible to vote1");
		} else if (age == 17){
			System.out.println("Eligible to vote2");			
		}else {
			System.out.println("Not Eligible");			
		}
		
		int a = 2;
		switch(a) {
		    case 1: 
		    	System.out.println("It is 1"); 
		    	break;
		    case 2: 
		    	System.out.println("It is 2");
		    	break;
		    case 3: 
		    	System.out.println("It is 3");
		    	break;
		    default: 
		    	System.out.println("Invalid data");
		}
		
	}

}
public class ControlAndConditionalStatements {

	public static void main(String[] args) {
		
		for (int i = 1; i <= 5; i++) {
	         System.out.println("Hello " + i);
	  	}
		
		int i = 1;
		while (i <= 5) {
			System.out.println("Count: " + i);
		    i++;
		}
		
		int j = 1;
		do {
		    System.out.println("Run at least once");
		    j++;
		} while (j <= 1);
		
	}

}
public class ControlAndConditionalStatements {

	public static void main(String[] args) {
		
		for (int i = 1; i <= 5; i++) {
			
			if(i == 4) {
				continue;//break;
			}
			System.out.println("Hello " + i);
	  	}
	}

}
public class SampleProgram {

	public static void main(String[] args) {

		ABC2 obj = new ABC2();
		obj.display1(); 
		
		ABC2.display2();
		main();

	}
	
	public static void main() {

		ABC2 obj = new ABC2();
		obj.display2(); 
		
		ABC2.display();

	}
	
	public void main2() {

		ABC2 obj = new ABC2();
		obj.display2(); 
		
		ABC2.display1();

	}

}

class ABC2 {

	int a = 10;
	static int b = 10;
	

	static void display1() {
		
		int c = 10;

		b++;
		
		//System.out.println(a);
		System.out.println(b);

	}
	
	void display2() {
		
		System.out.println(a);
		//As a was a local variable so we can not access it outside the method
		//System.out.println(b);

	}
}
Name: Akash  
Age: 22
How to Achieve in Java?:
  1. Abstract classes
  2. Interfaces
abstract class C1{
	abstract void display1();
	
	void display2() {
		System.out.println("Inside display2()");
	}
} 

interface I1{
	void display3();//public & abstract 
}

class C2 extends C1 implements I1{
	void display1() {
		System.out.println("Inside display1()");
	}
	
	public void display3() {
		System.out.println("Inside display3()");		
	}
}

public class AbstractionInJava {

	public static void main(String[] args) {

		C1 obj = new C2();
		obj.display1();
		obj.display2();
		
		System.out.println("\n");
		
		I1 obj2 = new C2();
		obj2.display3();
		
	}

}
  1. Private variables – restrict direct access.
  2. Public getters/setters – provide controlled access.
  3. Helps achieve data hiding.
class MyData{
	private String name = "Amit";
	
	public int salary = 12000;
	
	public String getName() {
		return "Mr. " + name;
	}
	
	public void setName(String s1) {
		name = s1;
	}
	
}

public class EncapsulationInJava {

	public static void main(String[] args) {
		
		MyData obj = new MyData();
		
		System.out.println(obj.salary);
//		System.out.println(obj.getName());
//		System.out.println(obj.name);
		
//		obj.name = "Sumit";
		obj.setName("Sumit");
		System.out.println(obj.getName());
		
	}
	
}
  1. Single inheritance: Yes
  2. Multilevel Inheritance: Yes
  3. Multiple Inheritance: No, Java does not support multiple inheritance with classes. It handles this feature by implementing more than one interface instead of extending more than one class
  4. Hierarchical Inheritance (Tree Based): Yes
public class InheritanceInJava {

	public static void main(String[] args) {

		Three g = new Three();
		g.methodOne();
		g.methodTwo();
		g.methodThree();

		Two two = new Two(1,2,2);
		
		D obj = new D(2);
		obj.methodFour();

	}

}

class One {
	int a;
	
	One(){
		System.out.println("One Constructor with 1P!");
	}

	One(int y, int z) {
		System.out.println("One Constructor with 2P!");
	}
	
	public void methodOne() {
		System.out.println("FirstMethod from class One!");
	}
}


class Two extends One {
	int b;
	
	Two(int x, int y, int z) {
		super();
		//super(1,2);
		b = x;
		System.out.println("Two");
	}
	
	public void methodTwo() {
		System.out.println("SecondMethod!");
	}
}

class Three extends Two {
	int c;

	Three() {
		super(1,2,3);
		System.out.println("Three Constructor!");
	}
	
	Three(int x, int y, int z) {
		//super();
		super(x,y,z);
		c = x;
		System.out.println("Three");
	}
	
	public void methodThree() {
		System.out.println("SecondMethod!");
	}
}
class Bird{
	void speak() {
		System.out.println("Tweet Tweet");
	}
	
	void speak(String hungry) {
		System.out.println("Tweet Tweet Tweet Tweet");
	}
}

class Koel extends Bird{
	void speak() {
		System.out.println("Koo-Kooo");
	}
}

public class Polymorphism {

	public static void main(String[] args) {
		Bird obj = new Koel();
		obj.speak();
	}

}
public class OverloadingAndOverridingInJava {

	public static void main(String args[]) {

		OverloadingAndOverridingInJava obj = new OverloadingAndOverridingInJava();

		System.out.println("add() with 2 parameters");
		System.out.println(obj.add(4, 6));

		System.out.println("add() with 3 parameters");
		System.out.println(obj.add(4, 6, 7));

		InheritFirstOne obj2 = new InheritFirstOne();
		System.out.println(obj2.add(1, 2));
		//System.out.println(obj2.add(1, 2, 3));

	}

}

class OverloadingAndOverridingInJava {

	// Within the same class: Method overloading
	int add(int a, int b) {
		System.out.print("Inside, OverloadingAndOverridingInJava!: ");
		return a + b;
	}

	int add(int a, int b, int c) {
		System.out.print("Inside, OverloadingAndOverridingInJava!: ");
		return a + b + c;
	}
}

//Overriding in Java: Requires Inheritance!
class InheritFirstOne extends OverloadingAndOverridingInJava {

	int add(int a, int b) {
		System.out.print("Inside, InheritFirstOne!: ");
		return a + b + 1;
	}
	
	int add(int a, int b, int c) {
		System.out.print("Inside, OverloadingAndOverridingInJava!: ");
		return a + b + c;
	}
}
public InterfaceInJava{

	public static void main(String[] args) {

		C1 obj = new C1();
		obj.funcA();
		obj.funcB();
		
		System.out.println(a);

	}

}

interface A1 {

	void funcA();

}

interface B1 extends A1 {

	void funcB();

}

class C1 implements B1 {

	public void funcA() {
		System.out.println("This is funcA from A1");
	}

	public void funcB() {
		System.out.println("This is funcB from A2");
	}
}
  • The main purpose of using a class declared as final is to prevent the class from being subclassed.
  • If a class is marked as final, then no class can inherit any feature from the final class.
  • You cannot extend a final class. If you try it gives you a compile-time error.
abstract class Bike {

	int a;
	
	String abc;

	Bike() {
		System.out.println("Inside Bike Constructor: " + a + "\n");
	}

	abstract void run(); // abstract method! (0-100%)

	void display() {
		System.out.println("This is display method!");
	}
}

//The type Honda must implement the inherited abstract method Bike.run()
class Honda extends Bike {

	void run() {

		System.out.println("run method from Bike class!");

	}
}

public class AbstractAndFinalClassInJava {
	public static void main(String[] args) {
		 Honda obj = new Honda();
		 obj.display();
		 obj.run();

//		 can not be instantiated
		 Bike obj2 = new Bike();//Error
	}

}
final class ABC {

	public int data = 30;
	
	String a = "Hello!";

	void display() {
		System.out.println(data);
	}
}

class CBA extends ABC{//Error
	public int data = 30;

	void display() {
		System.out.println(data);
	}
}
  • It is called when an instance/object of the class is created
  • At the time of calling the constructor, memory for the object is allocated in the memory
  • It is a special type of method that is used to initialize the object
  • Every time an object is created using the new() keyword, at least one constructor is called
  • Two types of Constructors in Java:
    • Parameterized Constructor
    • Default Constructor
public ConstructorsInJava {

	public static void main(String[] args) {
		
//		ABC4 obj = new ABC4();
		
		ABC4 obj1 = new ABC4(10000);
		
		ABC4 obj2 = new ABC4(1,2,3);
		
//		System.out.println(obj.a);
		System.out.println(obj1.a);
		System.out.println(obj2.a);
		
	}

}


class ABC4 {

	int a;
	int b;
	int c;
	
	
	/*
	 ClassName(Constructor Parameters){
	 	All the class and Instance variables can be initialized here!
	 }
	 */
	
	
	ABC4(int a){
		this.a = a;
	}
	
	ABC4(int a, int b, int c){
		this.a = a;
		this.b = b;
		this.c = c;
	}
	
	
	void display() {
		
		int b = 10;

		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		

	}
	
	int display2() {
		
		System.out.println(a);
		//As a was a local variable so we can not access it outside the method
		//System.out.println(b);
		
		return a;

	}
}

2 Comments.

  1. Sir….in the main method with command line argument snippet code….in line number 8, it should be args[0] instead of arg[0]…just a little correction but important

Leave a Reply

Your email address will not be published. Required fields are marked *