Skip to content

Instantly share code, notes, and snippets.

@MananAmin
Created September 8, 2021 18:37
Embed
What would you like to do?
basics of OOP in java
package com.oop;
import com.oop.encapsulation.EncapsClass;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// class properties and methods
Employee em = new Employee();
em.name = "Martin Garrix";
em.birth_year = 1995;
System.out.println(em.name + " is almost "+ em.getAge(true)+ " days old");
// constructor
Employer ey1 = new Employer("Google");
Employer ey2 = new Employer("Facebook");
System.out.println(ey1.name+" Total companies "+Employer.count);
// inheritance
Person p1 = new Person("bob",2000);
Person p2 = new Person("sana",2004);
Person p3 = new Person();
Manager m1 = new Manager("Raj",1996,"Cloud");
Person pm = new Manager("Dinesh",2002,"IT");
System.out.println(m1.name +" is "+m1.getAge()+" years old.");
System.out.println(m1.walk());
System.out.println(p1.walk());
System.out.println(p1.salary());
// Encapsulation and access modifiers
/* public everywhere
protected class and child class
default within package
private within class
*/
EncapsClass encap = new EncapsClass();
encap.doWork();
// Abstraction
Tesla t1 = new Tesla();
t1.startCar();
// Interfaces
BMW b1 = new BMW();
b1.start();
}
}
class Employee {
String name;
int birth_year;
// Compile time polymorphism
int getAge(){
return Math.abs(birth_year - Calendar.getInstance().get(Calendar.YEAR));
}
int getAge(boolean in_days){
return 365*Math.abs(birth_year - Calendar.getInstance().get(Calendar.YEAR));
}
}
class Employer{
String name;
// static use for Class property which shared by all the instances of class
static int count;
// Constructor
public Employer(String name){
this.name = name;
count++;
}
}
// Inheritance
class Person {
protected String name;
int birth_year;
static int population;
public Person(){
population++;
}
public Person(String name,int year){
this();
this.name = name;
this.birth_year =year;
}
int getAge() {
return Math.abs(birth_year - Calendar.getInstance().get(Calendar.YEAR));
}
String walk(){
return this.name +" is walking";
}
String salary(){
return this.name +" is making "+ this.getSalary()+" a year.";
}
// only access within class
private int getSalary(){
return 100000;
}
}
class Manager extends Person{
String department;
public Manager(String name,int year,String department){
super(name,year);
this.department = department;
}
String walk(){
return super.name +" is walking in "+this.department+" department.";
}
}
// abstraction
abstract class Car {
String brand;
protected int price;
abstract String getEngine();
void startCar(){
System.out.println("Car is starting ");
}
}
class Tesla extends Car{
void startCar(){
super.startCar();
System.out.println("Tesla is started");
}
public String getEngine(){
return "Electric Engine";
}
}
// Interfaces
interface Bike {
void start();
}
class BMW implements Bike{
@Override
public void start() {
System.out.println("BMW is started ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment