vasanth0989 / prep-myself

0 stars 0 forks source link

Oops Concept - Class Creation, Object Instantiation, Constructor, Access modifiers #1

Open vasanth0989 opened 10 months ago

vasanth0989 commented 10 months ago

Class Creation

package com.test;

import com.test.pojo.Employee;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) {
        // Topic 1: Classes, Object, Object initiation, Constructors, NoArg, All Arg
        // primitive data types
        // bbcdfils - b-byte, b-boolean, c-char, d-double, f-float, i-int, l-long,s-short
        int i = 10;
        // Reference type we always need to create an instance with 'new'
        // by default you will be getting no argument constructor
        Account account = new Account();
        // All args constructor
        // Instantiating an Object
        Account account1 = new Account(1,"Checking");
        System.out.println(account1.id);
        System.out.println(account1.type);

        // Topic 2: Access modifiers
        // default, public, private and protected
        // pojo - Plain Old Java Object
        // packages and import statements
        Employee employee = new Employee();
        // Access modifiers will be applied to class, fields, methods
        // public fields are visible everywhere
        //employee.name = "Vasanth";
        // How to call fields | variables vs methods
        // You can access fields by using . operator
        //employee.name = "";
        // Calling instance method will always have the () bracket notation
        // private fields are visible only inside the class
        employee.setName("Vasanth");
        System.out.println(employee.getName());
    }
}

// default
class Account {
    public int id;
    public String type;

    //No Argument constructor
    public  Account(){}

    // All args constructor
    public Account(int id, String type){
        this.id = id;
        this.type = type;
    }
}
package com.test.pojo;

public class Employee {

    private String name;

    // getters and setters
    // accessModifier returnType methodName
    public void setName(String name) {
        this.name = name;
    }
    // getters
    public String getName(){
        return this.name;
    }
}
vasanth0989 commented 10 months ago

https://github.com/vasanth0989/prep-myself/blob/main/notes/01_11_14_2023.md

vasanth0989 commented 10 months ago
package com.test.quiz;

class Employee {
    private String name;
    private int age;
    private double salary;

    private boolean fullTime;
    // NoArgument Constructor
    public Employee() {
    }
    // All Argument Constructor
    public Employee (String name, int age, double salary) {
     this.name = name;
     this.age = age;
     this.salary = salary;
    }

    //getters
    public String getName() {
        return name;
    }

    //setters
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public boolean isFullTime() {
        return fullTime;
    }

    public void setFullTime(boolean fullTime) {
        this.fullTime = fullTime;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

class TestMain {

    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.name = "";
        System.out.println("Age:"+employee.getAge());
        System.out.println("Salary:"+employee.getSalary());
        System.out.println("FullTime:"+employee.isFullTime());
        System.out.println("Name:"+employee.getName());

        // Instantiating or creating an Object
        // You use constructor to create an Object
        Employee employee1 = new Employee("Vasanth", 33, 252.2d);
        System.out.println("Before"+employee1.getName());
        employee1.setName("John");
        System.out.println("After:"+employee1.getAge());
    }
}
vasanth0989 commented 10 months ago
package com.test;

public class Calculator {

    private String name;

    //no-arg-constructor
    public Calculator() {}

    // Argument constructor
    public Calculator(String name){
        this.name = name;
    }
    //methods
    public int add(int num1, int num2){
        return num1 + num2;
    }
    //multiply
    public int multiply(int num1, int num2){
        return num1 * num2;
    }
    public int subtract(int num1, int num2){
     return  num1 - num2;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.test;

public class TestMain {

    public static void main(String[] args) {
        Calculator calculator = new Calculator("Super Awesome Calculator");
        calculator.setName("Simple Calculator");
        int sum = calculator.add(5,5);
        System.out.println("Sum:"+sum);
        int sub = calculator.subtract(10, 5);
        System.out.println("Sub:"+sub);
        int mul = calculator.multiply(4,5);
        System.out.println("multiply:"+mul);
        // Methods within String
        String calName = calculator.getName();
        String calUpperCase = calName.toUpperCase();
        System.out.println("CalUpperCase:"+calUpperCase);
        String calLowerCase = calName.toLowerCase();
        System.out.println("CalLowerCase:"+calLowerCase);
        System.out.println("These are the results form "+calculator.getName());

    }
}