00:00
/*D04, mt w/o calling */
public class D_004Animal {
public void eat() {
System.out.println("i am eating");
}
public static void main(String[] args) {
System.out.println("1");
}
}
// above code will only dispaly =1 ,
// method eat cannot execute iteself
/*D05, mt call */
public class D_005Animal {
public void eat() {
System.out.println("i am eating");
}
public static void main(String[] args) {
System.out.println("1");
D_005Animal buddy = new D_005Animal();
buddy.eat();
}
}
// 1
// i am eating Home
/*D06, 1 obj = 2 mt call */
class D_006Animal {
public void eat() {
System.out.println("i am eating");
}
public void run() {
System.out.println("runnng ");
}
public static void main(String[] args) {
System.out.println("1");
D_006Animal buddy = new D_006Animal();
buddy.eat();
buddy.run();
}
}
// 1
// i am eating
// running animalHome
/*D07, Direct other cls mt */
class D_007Animal {
public void eat() {
System.out.println("i am eating");
}
public void run() {
System.out.println("runnng ");
}
public static void main(String[] args) {
System.out.println("1");
D_007Animal buddy = new D_007Animal();
buddy.eat();
buddy.run();
buddy.fly(); // error
}
}
class bird {
void fly() {
System.out.println("i am flying");
}
}
// one class object cannot directly call another class objects.
/*D08, Direct mt call with other cls obj. */
class D_008Animals {
public void eat() {
System.out.println("i am eating");
}
public void run() {
System.out.println("runnng ");
}
public static void main(String[] args) {
System.out.println("1");
D_008Animals buddy = new D_008Animals();
buddy.eat();
buddy.run();
Birds sp = new Birds();
sp.fly();
}
}
class Birds {
void fly() {
System.out.println("i am flying");
}
}
// 1
// i am eating
// running
// i am flyingHome
/*D09, obj. inti. with ref. var. */
class D_009Animal {
String color;
int age;
public static void main(String[] args) {
D_009Animal buddy = new D_009Animal();
buddy.color = "black";
buddy.age = 10;
System.out.println(buddy.color + " " + buddy.age);
}
}
// black 10
// 1. When You Create An Instance Of The D_009Animal Class Using New
// D_009Animal(),You're Actually Invoking The Default Constructor, Even Though You Didn't
// Explicitly Define It.It Initializes The Instance Variables Color And Age
// To Their Default Values (Null = String And 0 = Int). Then, You're Setting
// The Values Of Color And Age Explicitly In Your Main Method.
// 4. So, Even Though You Don't See The Default Constructor Explicitly Defined
// In Your Code, It's Still There And Plays A Role In Initializing
// The Instance Variables Of The D_009Animal Objects.
/*D10, obj. init. with mt */
class D_010Animal {
String color;
int age;
void intObj(String c, int a) {
color = c;
age = a;
}
void display() {
System.out.println(color + " " + age);
}
public static void main(String[] args) {
D_010Animal buddy = new D_010Animal();
buddy.intObj("black", 10);
buddy.display();
}
}
// black 10
/*D11, Print msg. with - no arg. c/t. */
class D_011Test {
// public void D_011Test(){} // not c/t , simple method
public D_011Test() {
System.out.println("\nMETHOD EXECUTE - - w/o Using Object\n");
}
public static void main(String[] args) {
D_011Test t = new D_011Test(); // auto calling
// t.D_011Test(); // no need
}
}
// 1. c/t is a block similar to a method, having same name as class name
// 2. No Return type , not even void.
// 3. Modifier - public, protected, default, private
// In The Provided Code, The Constructor D_011Test() Is Indeed Initializing
// The Object Of The D_011Test Class But It's Also Printing A Message.
// This Is A Common Practice For Constructors To Perform Additional Actions
// Beyond Just Initializing Variables. However, Their Primary Purpose Remains
// Initialization. So, In Summary, While Constructors Are Typically Used To Initialize
// Instance Variables, They Can Also Perform Other Tasks, Such As Printing
// Messages Or Performing Additional Setup.
/*D12, obj. initi. by c/t with default ref. val. */
class D_012Employee {
String name;
int emp_id;
public static void main(String[] args) {
D_012Employee e1 = new D_012Employee();
D_012Employee e2 = new D_012Employee();
System.out.println("\n" + e1.name + " " + e1.emp_id);
System.out.println(e2.name + " " + e2.emp_id + " \n");
}
}
// jaise he e1, e2, object banege tab name = null aur emp_id = 0 compiler apne
// aap set kar deyga aur is case me sabhee ki id=0 aur naam null ho jayegea,
// jisse project me dikkat ho saktee hai is leeaye mai chahta hu ki jaise ki
// object create ho , value apne aap initilized ho jaye
/*D13, obj. initi. by c/t with user val. */
class D_013Employee {
String name = "Rajat";
int emp_id = 101;
public static void main(String[] args) {
D_013Employee e1 = new D_013Employee();
D_013Employee e2 = new D_013Employee();
System.out.println("\n" + e1.name + " " + e1.emp_id);
System.out.println(e2.name + " " + e2.emp_id + " \n");
}
}
// is case me bhee emp ki id same ho jayegee
/*D14, obj. initi. ( w/o ptc/t ) unique val. */
class D_014Employee {
String name;
int emp_id;
public static void main(String[] args) {
D_014Employee e1 = new D_014Employee();
e1.name = "Rajat";
e1.emp_id = 101;
D_014Employee e2 = new D_014Employee();
e2.name = "Deepak";
e2.emp_id = 102;
System.out.println("\n" + e1.name + " " + e1.emp_id);
System.out.println(e2.name + " " + e2.emp_id + " \n");
}
}
// 1000 employee ke leeaye 2000 line likhne hogi
/*D16, Default c/t. */
class D_016Test {
// Test(){} complier is line ko khud generate karegea ,
public static void main(String[] args) {
D_016Test t = new D_016Test();
}
}
// agar maine ek bhee constructor crate kar hai to
// complier khud se kabhe bhee constaructor create nahe karega
// in above case complier creates its own constructor
/*D17, Default c/t print instance var. */
class D_016Test1 {
int i;
public static void main(String[] args) {
D_016Test1 t = new D_016Test1();
System.out.println(t.i);
}
}
// o/p = 0, t naam ka object bana jisme i(=0) ko load karke print kar deeya ,
// yah kam constaructor ne kara khud se ,
// c/t banayega aur sabhe instance variable ko initilized ka dee
/*D18, No arg. c/t print instance value */
class D_017Test {
int i;
String s;
D_017Test() {
} // user create, now compiler will not create c/t
public static void main(String[] args) {
D_017Test t = new D_017Test();
System.out.println(t.i + " " + t.s);
}
}
/*D19, call ptc/t - no argument + arg.c/t */
class D_018Test {
D_018Test(String name) {
System.out.println("right way");
System.out.println(name);
}
public static void main(String[] args) {
D_018Test t = new D_018Test(); // error , The constructor D_018Test() is undefined
D_018Test t1 = new D_018Test("Book"); // right way
}
}
/*D20, call ptc/t with no arg.c/t */
class D_019Test {
D_019Test(int a) {
System.out.println("parametrized constructor");
}
public static void main(String[] args) {
D_019Test t = new D_019Test(); // yaha par complier apne aap c/t nahe banayege , user already created the
}
}
// c/t in class can not be applied to given type,
// above throug error
/*D21, call ptc/t with ptc/t */
class D_020Test {
D_020Test(int a) {
System.out.println("parametrized constructor");
}
public static void main(String[] args) {
D_020Test t = new D_020Test(10); // yaha par complier apne aap c/t nahe banayege , mai bana rakkha hai pahle
}
}
// c/t in class can not be applied to given type,
// above give error
/*D22, need obj of class, solve by creating no argument c/t */
class DClass {
DClass() {
System.out.println("no argument constructor"); // issue solved
}
DClass(String name) {
System.out.println("right way");
System.out.println(name);
}
public static void main(String[] args) {
DClass t = new DClass();
DClass t1 = new DClass("Book");
}
}
// no argument constructor
// right way
// Book
// GPT
// 1. In Your Code, You've Defined A Constructor D_018Test(String Name),
// 2. But You Haven't Defined A Default Constructor (One That Takes No
// Arguments) Explicitly.
// 3. Hence, If You Want To Create An Object Of D_018Test Without Arguments,
// 4. You Must Explicitly Define A Constructor That Takes No Arguments.
/*D23, Dot operator */
// Define a class named MyClass
class MyClass {
int x = 10; // instance variable
static int staticField = 20; // static field
void myMethod() { // Define mt
System.out.println("Inside myMethod");
}
static void staticMethod() { // static method
System.out.println("Inside staticMethod");
}
static class NestedClass { // Define a nested class
void nestedMethod() {
System.out.println("Inside nestedMethod");
}
}
}
// Define another class to use MyClass */
public class DClass {
public static void main(String[] args) {
MyClass obj = new MyClass();
int value = obj.x; // use dot operator
System.out.println("Value of x: " + value);
obj.myMethod(); // Invoking a method using dot operator
int y = MyClass.staticField; // Accessing static field using dot operator
System.out.println("Value of staticField: " + y);
MyClass.staticMethod(); // Invoking a static method using dot operator
MyClass.NestedClass nestedObj = new MyClass.NestedClass(); // Accessing nested cls, invoking its mt
nestedObj.nestedMethod();
}
}
/*D24, can we define multiple method in main */
public class DClass {
public static void main(String[] args) {
DClass obj = new DClass();
public void method1() {
System.out.println("Method 1");
}
public void method2() {
System.out.println("Method 2");
}
}
}
// C:\Users\rajat\Desktop\x>javac DClass.java
// DClass.java:16: error: unnamed classes are a preview feature and are disabled
// by default public void method1(){
// (use --enable-preview to enable unnamed classes)
// DClass.java:25: error: class, interface, enum, or record expected
// GPT
// Certainly! The Code You Provided Seems To Be Attempting To Define Methods
// Method1() And Method2()
// Inside The Main Method, Which Is Not Allowed In Java.
/*D25 initi.obj, with ptc/t & this */
class D_022Employee {
String name;
int emp_id;
D_022Employee(String name, int emp_id) {
this.name = name;
this.emp_id = emp_id;
}
public static void main(String[] args) {
D_022Employee e1 = new D_022Employee("Rajat", 101);
// jais ehe obj baneyga , paramterized constructor call hoga aur e1 me null aur
// 0 ki jagah par rajat aur 101 aa jayega
D_022Employee e2 = new D_022Employee("Abc", 102);
// jaise he obj create ho raha ha instance variable initilized ho ja rahe hai
System.out.println("\n" + e1.name + " " + e1.emp_id);
System.out.println(e2.name + " " + e2.emp_id + " \n");
}
}
// use - 1. use to initilization of object
// 2. wrong = constructor to create an objects