Java Basics - 3 W3D1-1
~30 mins
Week 3 - Day 1
package week3.day1;
public class AccessCreditCard {
public static void main(String[] args) {
CreditCard access = new CreditCard();
System.out.println(access.getCreditCardNumber());
access.setCreditCardNumber("1111 1111 1111 1111");
System.out.println(access.getCreditCardNumber());
}
}
package week3.day1;
public interface Android {
public void getKernerlInfo();
public void getKeyboardType();
}
package week3.day1;
public class Auto extends Vehicle{
public void startUsingHandStart() {
System.out.println("The auto is started using hand start");
}
}
package week3.day1;
public class BajajAuto extends Auto {
public void fillGas() {
System.out.println("The auto is loaded with the LPG");
}
}
package week3.day1;
public class BMW extends Car{
public void openSunRoof() {
System.out.println("Sunroof is open");
}
public void applyBrake() {
System.out.println("The ABS brake is applied");
}
}
package week3.day1;
public class BuildHouse {
public static void main(String[] args) {
// Wanted to create an object
// ClassName obj = new ClassName();
// House build = new House();
/*MyHouse build = new MyHouse();
build.getAddress();
build.getNumberOfBedRooms();
System.out.println(build.baseColor); */
//PartialHouse build = new PartialHouse();
BuildNewHouse build = new BuildNewHouse();
build.buildLivingRoom();
build.buildKitchen();
build.buildRoof();
}
}
package week3.day1;
// Extends -> build parent - child relationship !!
public class BuildNewHouse extends PartialHouse{
public void buildRoof() {
System.out.println("Roof is ready");
}
}
package week3.day1;
public class BuyVecicle {
public static void main(String[] args) {
BMW myCar = new BMW();
myCar.openSunRoof();// BMW
myCar.turnAC(); // Car
myCar.applyBrake(); // BMW
//myCar.applyABS(); // ABS
BajajAuto myAuto = new BajajAuto();
myAuto.startUsingHandStart(); // Auto
myAuto.fillGas(); // Bajaj
myAuto.applyBrake(); // Vehicle
}
}
package week3.day1;
public class Car extends Vehicle{
public void turnAC() {
System.out.println("Turned ON the air condition");
}
}
package week3.day1;
public class ConcreteClass {
public void run() {
}
}
package week3.day1;
public class CreditCard {
private String creditCardNumber = "2019 9292 2992 2939";
public String getCreditCardNumber() {
return creditCardNumber;
}
public void setCreditCardNumber(String creditCardNumber) {
this.creditCardNumber = creditCardNumber;
}
}
package week3.day1;
public interface House {
// Variable
public static String baseColor = "White";
// Method -> Interface methods cannot have body (the implementation)
// 100% abstract
public int getNumberOfBedRooms();
// Address
public String getAddress();
// Interface cannot have constructor and hence
// You cannot create an object for the interface !!
// Exception: From Java 8, default methods are allowed with implementation !!
// We will learn this week 8.
}
package week3.day1;
public class Lunch {
/*
* Within the same class, you have more than 1 method
* - with same signature
* - with different input arguments
* - can be by number of arguments or data type of arguments
*
* What is real purpose?
* Simplify the verbose
*
* This will be known when you code itself !! Compile time polymorphism
*
*/
public String bringLunch() {
return "Variety Rice";
}
public String bringLunch(String type) {
if(type.equalsIgnoreCase("potluck"))
return "Chicken Briyani";
else
return "Curd Rice";
}
}
package week3.day1;
// Concrete class -> Class with complete implementation !!
public class MyHouse implements House{
public int getNumberOfBedRooms() {
return 3;
}
public String getAddress() {
return "25, Poes Garden, Chennai";
}
}
package week3.day1;
public class MyPhone {
public static void main(String[] args) {
Samsung myPhone = new Samsung();
myPhone.getKernerlInfo();
OnePlus myNewPhone = new OnePlus();
myNewPhone.getKernelInfo();
myNewPhone.setMemOptimization();
}
}
package week3.day1;
public class OnePlus extends Oxygen{
@Override
public void getKernelInfo() {
System.out.println("The kernel is designed for playing games");
}
}
package week3.day1;
public abstract class Oxygen {
public void setMemOptimization() {
System.out.println("Implemented");
}
public abstract void getKernelInfo();
}
package week3.day1;
// Partially implemented class + Design to implement by concrete class
// May have unimplemented methods and hence
// Abstract class does not allow you to create object
public abstract class PartialHouse {
public void buildKitchen() {
System.out.println("Modular Kitchen ready");
}
public void buildLivingRoom() {
System.out.println("Modern Living Room ready");
}
public abstract void buildRoof();
}
package week3.day1;
public class RowHouse implements House{
public int getNumberOfBedRooms() {
// TODO Auto-generated method stub
return 0;
}
public String getAddress() {
// TODO Auto-generated method stub
return null;
}
}
package week3.day1;
public class Samsung implements Android{
public void getKernerlInfo() {
System.out.println("The kernel is built on C++ with Kysis");
}
public void getKeyboardType() {
System.out.println("The keyboard with android icons");
}
}
package week3.day1;
public class Vehicle {
public void applyBrake() {
System.out.println("The brake is applied");
}
public void soundHorn() {
System.out.println("Horn to let people know that my vehicle is coming");
}
}