0% found this document useful (0 votes)
3K views

Java OOP-Cheat Sheet

This document provides a cheat sheet on object-oriented programming concepts in Java, including classes, objects, constructors, inheritance, polymorphism, and abstraction. It defines key OOP terms and shows code examples to illustrate concepts like single, multi-level, and multiple inheritance as well as compile-time and runtime polymorphism using method overloading and overriding.

Uploaded by

zanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Java OOP-Cheat Sheet

This document provides a cheat sheet on object-oriented programming concepts in Java, including classes, objects, constructors, inheritance, polymorphism, and abstraction. It defines key OOP terms and shows code examples to illustrate concepts like single, multi-level, and multiple inheritance as well as compile-time and runtime polymorphism using method overloading and overriding.

Uploaded by

zanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

JAVA OOP CHEAT SHEET Learn JAVA from experts at https://www.edureka.

co

Object Oriented Programming in Java Inheritance


Java is an Object Oriented Programming language Single Inheritance Hierarchical Inheritance
that produces software for multiple platforms. An Class A {
object-based application in Java is concerned with //your parent class code Class A {
declaring classes, creating objects from them and } //your parent class code
interacting between these objects. }
Class B extends A {
//your child class code
Java Class } Class B extends A {
//your child class code
class Test { }
// class body Multi Level Inheritance
member variables Class A { Class C extends A {
methods //your parent class code //your child class code
} } }
Class B extends A {
//your code
Java Object } Multiple Inheritance
//Declaring and Initializing an object Class C extends B {
Test t = new Test(); //your code Class A {
} //your parent class code
}
Class B {
Constructors Hybrid Inheritance
//your parent class code
A A
}
/ \ |
Default Constructor Class C extends A,B {
B C (OR) B
class Test{ //your child class code
/ \ / \
/* Added by the Java Compiler at the Run Time }
D E C D
public Test(){
}
*/ Polymorphism Abstraction
public static void main(String args[]) {
Test testObj = new Test();
} Compile Time Polymorphism Abstract Class
} class Calculator { public abstract class MyAbstractClass
static int add(int a, int b){ {
return a+b; public abstract void abstractMethod();
Parameterized Constructor } public void display(){
static double add( double a, double b){ System.out.println("Concrete method");
public class Test {
return a+b; }
int appId;
} }
String appName;
public static void main(String args[]){
//parameterized constructor with two parameters
System.out.println(Calculator.add(123,17));
Test(int id, String name){ Interface
System.out.println(Calculator.add(18.3,1.9));
this.appId = id;
} //Creating an Interface
this.appName = name;
} public interface Bike { public void start(); }
}
//Creating classes to implement Bike interface
void info(){
Run Time Polymorphism class Honda implements Bike{
System.out.println("Id: "+appId+" Name: "+appName);
public void start() {
} public class Mobile{ System.out.println("Honda Bike");
void sms(){System.out.println("Mobile class");} } }
public static void main(String args[]){ } class Apache implements Bike{
Test obj1 = new Test(11001,"Facebook");
public void start() {
Test obj2 = new Test(23003,"Instagram"); //Extending the Mobile class System.out.println("Apache Bike");
obj1.info(); public class OnePlus extends Mobile{ } }
obj2.info(); //Overriding sms() of Mobile class class Rider{
} void sms(){ public static void main(String args[]){
} System.out.println(" OnePlus class"); Bike b1=new Honda();
} b1.start();
Bike b2=new Apache();
public static void main(String[] args) { b2.start();
J ava C e r t i f i c a t i o n OnePlus smsObj= new OnePlus();
smsObj.sms();
} }

Training }
}
Encapsulation
public class Artist {
Modifiers in Java private String name;
//getter method
public String getName() { return name; }
Access Modifiers Non - Access Modifiers //setter method
public void setName(String name) { this.name = name; }
Scope Private Default Protected Public Type Scope }
public class Show{
Same class Yes Yes Yes Yes
Static Makes the attribute dependent on a class public static void main(String[] args){
//creating instance of the encapsulated class
Same package subclass No Yes Yes Yes Artist s=new Artist();
Final Once defined, doesn’t allow any changes //setting value in the name member
Same package non-subclass No Yes Yes Yes s.setName(“BTS");
//getting value of the name member
Abstract Makes the classes and methods abstract
Different package subclass No No Yes Yes System.out.println(s.getName());
}
Different package non-subclass No No No Yes Synchronized Used to synchronize the threads }

You might also like