kchaitanya-training / BFS45

0 stars 0 forks source link

define two differnt packages and each package define two different class and try to access them (call them) with import and default and public access specifiers #17

Open kchaitanya-training opened 9 months ago

satshree commented 9 months ago
package com.satshree.package2;

public class Sample2 {
    public void method() {
        System.out.println("This is Sample2");
    }
}

package com.satshree.package2;

public class Example2 {
    public void method() {
        System.out.println("This is Example2");
    }
}

package com.satshree.package1;

public class Example {
    public void method() {
        System.out.println("This is example");
    }
}

package com.satshree.package1;
import com.satshree.package2.*;

public class Sample1 {
    void method() {
        System.out.println("This is Sample1");

        Sample2 s = new Sample2();
        s.method();

        Example e = new Example();
        e.method();

        Example2 e2 = new Example2();
        e2.method();
    }
    public static void main(String[] args) {
        Sample1 s = new Sample1();
        s.method();
    }
}

Image

gsajan21 commented 9 months ago

package com.package1;

import com.package2.ClassX; import com.package2.ClassY;

public class ClassA {

void print(){
    System.out.println("This is class A.");
}

public static void main(String[] args) {
    ClassB b = new ClassB();
    b.print();
    b.doSomething();

    ClassX x  = new ClassX();
    x.print();

// x.eat(); //can't access default method ClassY y = new ClassY(); y.print(); } }

package com.package1;

public class ClassB { void print(){ System.out.println("This is class B."); }

public void doSomething(){
    System.out.println("dance");

}

}

package com.package2;

public class ClassX { public void print(){ System.out.println("This is class X."); } void doSomething(){ System.out.println("eat"); } }

package com.package2;

public class ClassY { public void print(){ System.out.println("This is class Y."); } }

Image

sonamLama22 commented 9 months ago

package sonam;

public class Name {

public void printName(String name) {
    System.out.println("Name: "+ name);
}

} package sonam;

public class School {

public void printSchool(String school) {
    System.out.println("School name: "+ school);
}

} package sonam2;

public class Address {

void printAddress(String address) {
    System.out.println("Address: "+ address);
}

} package sonam2; import sonam.Name; import sonam.School;

public class Info {

void printInfo() {
    Name n = new Name();
    n.printName("Sonam");
    School s = new School();
    s.printSchool("Queens College");
    Address a = new Address();
    a.printAddress("New york");
}

public static void main(String[] args) {

    Info info = new Info();
    info.printInfo();

}

}

dhawasangbolama2 commented 9 months ago

package com.test.sample1;

import com.test.sample2.*;

public class Dhawa { public void methodA(){ System.out.println("Dhawa is my first name."); HarryPotter b = new HarryPotter(); b.notMyName();

}
public static void main(String[] args) {
    Dhawa Name = new Dhawa();
    Name.methodA();

}

} class Lama{ void methodB() { System.out.println("Lama is my last name."); } }

========================================================

package com.test.sample2;

//import com.test.sample1.methodB;

public class HarryPotter { public void notMyName() { System.out.println("Harry Potter is not my name."); //Lama LastName = new Lama();----->this will not work because class Lama in same1 is default //LastName.methodB(); } public static void main(String[] args) { //HarryPotter NoMyName = new HarryPotter(); //NoMyName.notMyname();

}

}

Output Dhawa is my first name. Harry Potter is not my name.

nimadsherpa commented 9 months ago
package com.test.sample1;
import com.test.sample2.*;

public class Circle {

    final float pi = 3.14f;
    int radius = 4;
    float area, perimeter;

    void calculateAll() {

        // Class and its object from different package
        Area a = new Area();
        area = a.areaCircle(radius,pi);
        Perimeter p = new Perimeter();
        perimeter = p.perimeterCircle(radius, pi);
        System.out.println("Area of circle: " + area);
        System.out.println("Perimeter of circle: " + perimeter);

        // same package class and its object
        Rectangle r = new Rectangle();
        r.calculate();
    }

    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.calculateAll();
    }
}

package com.test.sample1;

public class Rectangle {

    int l = 5;
    int w = 4;
    int perimeter, area;

    void calculate() {
        perimeter = 2*(l+w);
        area = l * w;

        System.out.println("Area of Rectangle: "+ area);
        System.out.println("Perimeter of Rectangle: "+ perimeter);
    }
}

package com.test.sample2;

public class Area {

    float area;

    public float areaCircle(int radius, float pi) {
        area = pi * (radius*radius);
        return area;
    }
}

package com.test.sample2;

public class Perimeter {

    float perimeter;

    public float perimeterCircle(int radius, float pi) {
        perimeter = 2*pi*radius;
        return perimeter;
    }
}

/* 
Sample Output: 
Area of circle: 50.24
Perimeter of circle: 25.12
Area of Rectangle: 20
Perimeter of Rectangle: 18
*/
mwdorji commented 9 months ago

package Sample1;

public class X { public void methodX() { System.out.println("This is methodX"); }

} package Sample1;

public class Y { public void methodY() { System.out.println("This is methodY"); }

} package Sample2; import Sample1.*;

public class A { public void methodA() { X x = new X(); x.methodX(); System.out.println("This is class A");

}

public static void main(String[] args) {
    A a = new A();
    a.methodA();
}

} package Sample2;

import Sample1.*;

public class B { public void methodB() { Y y = new Y(); y.methodY(); System.out.println("This is class B ");

}

public static void main(String[] args) {
    B b = new B();
    b.methodB();
}

} output for class A: This is methodX This is class A

output for class B This is methodY This is class B

bik-on-git commented 9 months ago

package TestA;

public class ClassX {

void methodofX()
{

    System.out.println("its Classs X saying which day of week");
}

public static void main(String[] args) {
    ClassX cx = new ClassX();
    cx.methodofX();
}

}

package TestA;

import TestB.Monday;

public class ClassY { void methodofY() { //default: has access within the packages only //x and y can access each other ClassX cx = new ClassX(); cx.methodofX();

    //to access class outside of package we have to define that class method as public to access and import
    Monday m = new Monday();
    m.dayName();
}

public static void main(String[] args) {
    ClassY cy = new ClassY();
    cy.methodofY();

}

}

package TestB;

public class Monday {

public void dayName() { System.out.println("its Monday today."); }

public static void main(String[] args) {
    Monday m = new Monday();
    m.dayName();
}

}

aashirwad43 commented 9 months ago

Image


package com.aashirwad.package1;

import com.aashirwad.package2.C;

public class A {
    public void classA(){
        C c= new C();

        B b = new B();
        b.classB();
    }

    public static void main(String[] args) {
        A a = new A();
        a.classA();
    }
}

package com.aashirwad.package1;

public class B {
    public void classB(){
        System.out.println("This is class B");
    }
}

package com.aashirwad.package2;

import com.aashirwad.package1.*;

public class C {
     public void classC(){
         B b = new B();
         b.classB();

         A a = new A();

        System.out.println("This is c class");
    }

    public static void main(String[] args) {
        C c = new C();
        c.classC();
    }
}

package com.aashirwad.package2;

public class D {

    public void classD(){
        System.out.println("This is class D");
    }
}