Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object Oriented Programming in Java Cheat Sheet, Cheat Sheet of Object Oriented Programming

Class, Object, Constructors, Inheritance, Polymorphism, Abstraction, Modifiers, Encapsulation terms in this cheat sheet

Typology: Cheat Sheet

2020/2021

Uploaded on 04/27/2021

dylanx
dylanx 🇺🇸

4.7

(21)

287 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA OOP CHEAT SHEET
Object Oriented Programming in Java Inheritance
Java is an Object Oriented Programming language
that produces software for multiple platforms. An
object-based application in Java is concerned with
declaring classes, creating objects from them and
interacting between these objects.
//Declaring and Initializing an object
Test t = new Test();
Java Object
Modifiers in Java
Class A {
//your parent class code
}
Class B extends A {
//your child class code
}
Single Inheritance
Class A {
//your parent class code
}
Class B extends A {
//your code
}
Class C extends B {
//your code
}
Multi Level Inheritance
Class A {
//your parent class code
}
Class B {
//your parent class code
}
Class C extends A,B {
//your child class code
}
Multiple Inheritance
class Test {
// class body
member variables
methods
}
Java Class
Constructors
class Test{
/* Added by the Java Compiler at the Run Time
public Test(){
}
*/
public static void main(String args[]) {
Test testObj = new Test();
}
}
Default Constructor
public class Test {
int appId;
String appName;
//parameterized constructor with two parameters
Test(int id, String name){
this.appId = id;
this.appName = name;
}
void info(){
System.out.println("Id: "+appId+" Name: "+appName);
}
public static void main(String args[]){
Test obj1 = new Test(11001,"Facebook");
Test obj2 = new Test(23003,"Instagram");
obj1.info();
obj2.info();
}
}
Parameterized Constructor
Scope Private Default Protected Public
Same class Yes Yes Yes Yes
Same package subclass No Yes Yes Yes
Same package non-subclass No Yes Yes Yes
Different package subclass No No Yes Yes
Different package non
-
subclass
No No No Yes
Access Modifiers
Type Scope
Static Makes the attribute dependent on a class
Final Once defined, doesn’t allow any changes
Abstract Makes the classes and methods abstract
Synchronized Used to synchronize the threads
Non - Access Modifiers
J ava C e r ti f i c a t io n
T r a i n i n g
Class A {
//your parent class code
}
Class B extends A {
//your child class code
}
Class C extends A {
//your child class code
}
Hierarchical Inheritance
A A
/ \ |
B C (OR) B
/ \ / \
D E C D
Hybrid Inheritance
Polymorphism
class Calculator {
static int add(int a, int b){
return a+b;
}
static double add( double a, double b){
return a+b;
}
public static void main(String args[]){
System.out.println(Calculator.add(123,17));
System.out.println(Calculator.add(18.3,1.9));
}
}
Compile Time Polymorphism
public class Mobile{
void sms(){System.out.println("Mobile class");}
}
//Extending the Mobile class
public class OnePlus extends Mobile{
//Overriding sms() of Mobile class
void sms(){
System.out.println(" OnePlus class");
}
public static void main(String[] args) {
OnePlus smsObj= new OnePlus();
smsObj.sms();
}
}
Run Time Polymorphism
Abstraction
public abstract class MyAbstractClass
{
public abstract void abstractMethod();
public void display(){
System.out.println("Concrete method");
}
}
Abstract Class
//Creating an Interface
public interface Bike { public void start(); }
//Creating classes to implement Bike interface
class Honda implements Bike{
public void start() {
System.out.println("Honda Bike");
} }
class Apache implements Bike{
public void start() {
System.out.println("Apache Bike");
} }
class Rider{
public static void main(String args[]){
Bike b1=new Honda();
b1.start();
Bike b2=new Apache();
b2.start();
} }
Interface
public class Artist {
private String name;
//getter method
public String getName() { return name; }
//setter method
public void setName(String name) { this.name = name; }
}
public class Show{
public static void main(String[] args){
//creating instance of the encapsulated class
Artist s=new Artist();
//setting value in the name member
s.setName(“BTS");
//getting value of the name member
System.out.println(s.getName());
}
}
Encapsulation

Partial preview of the text

Download Object Oriented Programming in Java Cheat Sheet and more Cheat Sheet Object Oriented Programming in PDF only on Docsity!

JAVA OOP CHEAT SHEET

Object Oriented Programming in Java Inheritance

Java is an Object Oriented Programming language that produces software for multiple platforms. An object-based application in Java is concerned with declaring classes, creating objects from them and interacting between these objects. //Declaring and Initializing an object Test t = new Test();

Java Object

Modifiers in Java

Class A { //your parent class code } Class B extends A { //your child class code }

Single Inheritance

Class A { //your parent class code } Class B extends A { //your code } Class C extends B { //your code }

Multi Level Inheritance

Class A { //your parent class code } Class B { //your parent class code } Class C extends A,B { //your child class code }

Multiple Inheritance

class Test { // class body member variables methods }

Java Class

Constructors

class Test{ /* Added by the Java Compiler at the Run Time public Test(){ } */ public static void main(String args[]) { Test testObj = new Test(); } }

Default Constructor

public class Test { int appId; String appName; //parameterized constructor with two parameters Test(int id, String name){ this.appId = id; this.appName = name; } void info(){ System. out.println("Id: "+appId+" Name: "+appName); } public static void main(String args[]){ Test obj1 = new Test(11001,"Facebook"); Test obj2 = new Test(23003,"Instagram"); obj1.info(); obj2.info(); } }

Parameterized Constructor

Scope Private Default Protected Public

Same class Yes Yes Yes Yes Same package subclass No Yes Yes Yes Same package non-subclass No Yes Yes Yes Different package subclass No No Yes Yes Different package non-subclass No No No Yes

Access Modifiers

Type Scope

Static Makes the attribute dependent on a class Final Once defined, doesn’t allow any changes Abstract Makes the classes and methods abstract Synchronized Used to synchronize the threads

Non - Access Modifiers

J ava C e r t i f i c a t i o n

T r a i n i n g

Class A { //your parent class code } Class B extends A { //your child class code } Class C extends A { //your child class code }

Hierarchical Inheritance

A A

/ \ |

B C (OR) B

/ \ / \

D E C D

Hybrid Inheritance

Polymorphism

class Calculator { static int add(int a, int b){ return a+b; } static double add( double a, double b){ return a+b; } public static void main(String args[]){ System. out.println(Calculator.add(123,17)); System. out.println(Calculator.add(18.3,1.9)); } }

Compile Time Polymorphism

public class Mobile{ void sms(){System.out.println(" Mobile class" ); } } //Extending the Mobile class public class OnePlus extends Mobile{ //Overriding sms() of Mobile class void sms(){ System. out.println(" OnePlus class"); } public static void main(String[] args) { OnePlus smsObj= new OnePlus(); smsObj.sms(); } }

Run Time Polymorphism

Abstraction

public abstract class MyAbstractClass { public abstract void abstractMethod(); public void display(){ System. out.println("Concrete method"); } }

Abstract Class

//Creating an Interface public interface Bike { public void start(); } //Creating classes to implement Bike interface class Honda implements Bike{ public void start() { System. out.println("Honda Bike"); } } class Apache implements Bike{ public void start() { System. out.println("Apache Bike"); } } class Rider{ public static void main(String args[]){ Bike b1=new Honda(); b1.start(); Bike b2=new Apache(); b2.start(); } }

Interface

public class Artist { private String name; //getter method public String getName() { return name; } //setter method public void setName(String name) { this.name = name; } } public class Show{ public static void main(String[] args){ //creating instance of the encapsulated class Artist s=new Artist(); //setting value in the name member s.setName(“BTS"); //getting value of the name member System. out.println(s.getName()); } }

Encapsulation