Uploaded by arun kumar

CoreJava27Jun2023 for beginers

advertisement
class Demo{
static String name;
static int id;
public Demo(String name,int id){//parameterized constructor
this.name=name;
this.id=id;
System.out.println("Constructor is executing");
Demo();
}
public static final void Demo(){//instance method
System.out.println("Name = "+name+" Id = "+id);
}
}
class Sample extends Demo{
Sample(){
super("Nilphar",202324);
}
}
class MainClass{
public static void main(String... args){
Demo d1 = new Demo("Abinaya",202324);
System.out.println(d1);
}
}
//method can be final
//final method cannot be overridden
//and also final method can be made inline by compiler
//final class cannot be inherited
//final variables are constant cannot be modified
class Demo1{
static int a = 20;
public void display(){
this.a=50;
System.out.println("a = "+a);
}
}
class MainClass1{
public static void main(String... args){
Demo1 d1 = new Demo1();
d1.display();
}
}
//Types of constructor
//1) Default constructor or no-arg constructor
//2) Parameterized constructro
//3) Copy constructor
class Demo1{
int a;
Demo1(int a){
this.a = a;
}
Demo1(Demo1 d1){
this.a = d1.a;
}
}
class MainClass1{
public static void main(String... args){
Demo1 d1 = new Demo1(20);
Demo1 d2 = new Demo1(d1);
d2.a=50;
System.out.println("d1.a = "+d1.a+" d2.a = "+d2.a);
}
}
//Types of constructor
//1) Default constructor or no-arg constructor
//2) Parameterized constructro
//3) Copy constructor
//In Java there is no concept of copy constructor but if u need u can create one
//There is no concept called as destructor in java but we have finalize() method
class Demo1{
int a;
Demo1(int a){
this.a = a;
}
Demo1(Demo1 d1){
this.a = d1.a;
}
void display(String... str){
System.out.println("Name = "+str[0]+" Address = "+str[1]+" city = "+str[2]);
}
}
class MainClass1{
public static void main(String... args){
Demo1 d1 = new Demo1(20);
Demo1 d2 = new Demo1(d1);
d2.a=50;
d2.display("Abishek","TN","Chennai");
System.out.println("d1.a = "+d1.a+" d2.a = "+d2.a);
}
}
//Types of constructor
//1) Default constructor or no-arg constructor
//2) Parameterized constructro
//3) Copy constructor
//In Java there is no concept of copy constructor but if u need u can create one
//There is no concept called as destructor in java but we have finalize() method
import java.util.*;
class Demo2{
static long fact=1;
public static long WORecursion(int num){
while(num>=1){
fact*=num;
num--;
}
return fact;
}
public static long WRecursion(int num){
if(num>=1){return num*WRecursion(num-1);}
else{return 1;}
}
}
class MainClass2{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input a number: ");
int num = sc.nextInt();
System.out.println("Factorial is "+Demo2.WORecursion(num));
System.out.println("Factorial is "+Demo2.WRecursion(num));
}
}
//Recursion -> A method calling itself again and again is called as Recursion
//u need a condition when to stop recursion
//Same recursion i need addition of first 5 numbers
//5+4+3+2+1=15
//There is a method called Factory methods in java
//it is a static method which returns the instance of the class
//What is the advantage of factory method
//if we want to create different objects then we need different constructors
//for this we go for factory methods which can return the instance of the class
//and create objects differently
import java.text.*;
import java.util.*;
class MainClass6{
public static void main(String... args){
Scanner sc = new Scanner(System.in);
System.out.println("Input the radius : ");
int rad = sc.nextInt();
double aoc = Math.PI*rad*rad;
double d = 20567.45768;
NumberFormat nf = NumberFormat.getInstance();
System.out.println(nf.format(d));
System.out.println(aoc);
nf.setMinimumFractionDigits(3);
nf.setMaximumIntegerDigits(3);
String str = nf.format(aoc);
double r = Double.parseDouble(str);
System.out.println(r);
}
}
import static java.lang.System.*;
import java.util.*;
class Demo6{
String str;
Demo6(){
str="I am creating object using class.forName";
}
}
public class MainClass8 {
public static void main(String... args) {
try {
Class c =
Class.forName("com.arun.batch10am.methods.Demo6");
Demo6 d6 = (Demo6)c.newInstance();
out.println(d6.str);
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
//What are different ways of creating objects in java
//1) new keyword
//Example e1 = new Example();
//2) Using Factory methods we can create objects
//NumberFormat nf = NumberFormat.getInstance()
//3) Class.forName() and c.newInstance()
//clonning
//Example e2 = (Example)e1.clone();
class Sample3{
String name;
Sample3(){
name="Kumaran";
}
}
class MainClass7{
public static void main(String... args){
try{
Class c = Class.forName("Sample3");
Sample3 s3 = (Sample3)c.newInstance();
System.out.println(s3.name);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//What are different ways of creating objects in java
//1) new keyword
//Example e1 = new Example();
//2) Using Factory methods we can create objects
//NumberFormat nf = NumberFormat.getInstance()
//3) Class.forName() and c.newInstance()
//4) clonning
//Example e2 = (Example)e1.clone();
class Sample3{
String name;
Sample3(){
name="Kumaran";
}
}
class MainClass7{
public static void main(String... args){
try{
Class c = Class.forName("Sample3");
Sample3 s3 = (Sample3)c.newInstance();
System.out.println(s3.name);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
//Object class is the super class of all classes in java
//it contains 9 methods
//1) getClass()
//2) clone()
//3) equals()
//4) hashCode()
//5) wait()
//6) notify()
//7) notifyAll()
//8) finalize()
//9) toString()
class Employee implements Cloneable{
String name;
Employee(String name){
this.name=name;
}
public Object myClone()throws CloneNotSupportedException{
return super.clone();
}
}
class MainClass9{
public static void main(String... args)throws CloneNotSupportedException{
Employee e1 = new Employee("Lavanya");
System.out.println(e1.name);
Employee e2 = (Employee)e1.myClone();
System.out.println(e2.name);
e2.name=e2.name.toUpperCase();
System.out.println(e1.name+" : "+e2.name);
}
}
28-06-2023
class Example{
String name;
int id;
{
name="Arun Kumar";
System.out.println("Instance block is getting executed");
}
Example(String name,int id){
System.out.println("Constructor block is getting executed");
this.name=name;
this.id=id;
}
public void display(){
System.out.println("Name : "+name+" Id : "+id);
}
@Override
public String toString(){
return "Name : "+name+" Id : "+id;
}
}
class MyClass{
int a;
MyClass(int a){
this.a=a;
}
public static void getByName(Object obj){
Class c = obj.getClass();
String str = c.getName();
System.out.println(str);
}
}
class MainClass{
public static void main(String... args){
Example e1 = new Example("Kumaran",202327);
System.out.println(e1);
Integer in1 = new Integer(15);
Integer in2 = new Integer(15);
MyClass mc1 = new MyClass(20);
MyClass mc2 = new MyClass(20);
if(in1.equals(in2)){System.out.println("Both are same");}
else{System.out.println("Both are not same");}
if(mc1.equals(mc2)){System.out.println("Both are same");}
else{System.out.println("Both are not same");}
MyClass.getByName(e1);
MyClass.getByName(mc1);
}
}
//Object class methods
//1) clone()
//2) hashCode()
//3) equals()
//4) notify()
//5) notifyAll()
//6) finalize()
//7) toString()
//8) getClass()
//9) wait()
//OOPS Concept
//1) Class & Object
//2) Encapsulation -> Data hiding biding of data and code inside a class
//3) Inheritance -> Code reusability we can use extends keyword
1) Single inheritance
2) Multilevel inheritance
3) Hierarchial inheritance
4) Multiple inheritance
5) Hybrid inheritance
//4) Polymorphism -> Poly->many morphism -> forms taking many forms
1) static or compile time polymorphism
2) dynamic or run time polymorphism
3) Method overloading
4) Method overriding
5) Type casting -> we can cast objects -> Upcasting and Downcasting
//5) Abstraction -> hiding unncessary details and showing only the essential details 50% abstraction
//6) Interface -> 100% abstraction
//1) Class and Object -> Flower -> Abstract thing but lilly,rose,jasmine,lotus -> exist physically
Flower is a class and lilly,rose,jasmine,lotus are objects of class Flower
class is a blueprint or logical entity with which we create objects which exist physically
so object is a physical entity
30Jun2023
Download