[go: up one dir, main page]

0% found this document useful (0 votes)
647 views5 pages

OOP Reviewer

This document provides an overview of object-oriented programming concepts: 1. It describes key OOP concepts like classes, objects, methods, encapsulation, inheritance, and polymorphism. 2. It explains access modifiers like public, private, and protected and how they control access to class members. 3. It discusses other concepts like constructors, the this keyword, static members, abstract classes, interfaces, and information hiding.
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)
647 views5 pages

OOP Reviewer

This document provides an overview of object-oriented programming concepts: 1. It describes key OOP concepts like classes, objects, methods, encapsulation, inheritance, and polymorphism. 2. It explains access modifiers like public, private, and protected and how they control access to class members. 3. It discusses other concepts like constructors, the this keyword, static members, abstract classes, interfaces, and information hiding.
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/ 5

OOP  Components are easier to understand/use

Object-Oriented Programming – programming  Simplifies modification and repair


methodology that defines n object rather than functions  Facilitated re-use
and logic. Public Instance Variable – no restriction where you can
Object – an instance of a class use its name.
 Has characteristics or attributes Private instance Variable – name cannot be used to
Example: access it outside a class
class Bicycle { Public method – can invoke anywhere without
// field of class restriction
int gear = 5; Private method – method cannot be invoked within the
// method of class definitions of method.
void braking() { Accessor – returns data from private i.v
} Mutator – changes the data stored in 1 or more private
} // create object i.v
Bicycle sportsBicycle = new Bicycle(); Encapsulation – combining data and actions into a
State – values of an object’s attributes single item
 First gear  Groups instance variable and methods into a
Behaviors – actions that objects can take class
 Breaking, accelerating  Hides implementation details
Method – behavior defined by piece of Java class UML Class Diagram – describe the structure of class by
 Collection of statements that are grouped displaying the:
together to perform an operation. 1. Class name
Class – blueprint for defining an object 2. Variables
 Object of same kind, same data type = same 3. Methods
class Reference – address of the object’s memory location
 Data type of obj. = name of its class  Class types are reference type
Inheritance – one way of organizing classes  Instead of ==, use equals()
 Allows objects of a class to take on properties Constructor – create and initialize an obj.
of objects from another class.  Uses new keyword
 Extends  MyClass mc = new myClass();
Interface – program component that contains the  Does not have a return type
heading for a number of public methods.  Can call methods within its class
 Implements Static Variables – shared by all objects of its class
 Use to specify methods that a class must  Can be public or private
implement.  Not constants
Package – collection of related classes and interface Static Method – method that can be invoked without
 Name of the folder = name of the package using any objects.
 import java.util.Scanner;  Written in a static keyword
 import java.util.*;  Write the class name instead of the object
Void – method is not use to return a value name.
Parameter – list of variables in a method declaration.  Cannot reference an instance variable of the
 Dog1 (String n, String b){ class.
Argument – Actual value that is passed Predefined Math Class – provides a number of standard
Instance variables – variables declared outside the mathematical methods.
method.  Power = Math.pow();
Local variables – variables declared outside the  Absolute value = Math.abs();
method.  Maximum value = Math.max();
this – object’s name receiving the method call  Minimum value = Math.min();
Information hiding – mechanism for restricting access
 Random # = Math.random(); CONSTRUCTORS
 Rounding = Math.round();
 Ceiling = Math.ceil() class Main {
 Floor = Math.floor(); private String name;
 Square root = Math.sqrt();
Overloading – when multiple methods have the same // constructor
name within the same class. Main() {
 Done b having different method definitions System.out.println("Constructor Called:");
Signature – Methods name and the number & types of name = "Programiz";
its parameters. }
Inheritance - the use of existing class to define a new
class public static void main(String[] args) {
Derived class – adding instance variables and methods
to an existing class. // constructor is invoked while
 Subclass // creating an object of the Main class
 Does not inherit any constructors from the base Main obj = new Main();
class. System.out.println("The name is " + obj.name);
Base class – Existing class that the derived class is built }
upon. }
 Superclass
Override – If a derived class defines a method with the THIS KEYWORD
same name, the same number and types of parameters
and the same return type as a method in the base class. class Main {
Super – first action in a constructor definition int instVar;
Polymorphism – ability of an object to take on many Main(int instVar){
forms. this.instVar = instVar;
System.out.println("this reference = " + this);
 Allows to make changes in the method
}
definition.
public static void main(String[] args) {
 Base class reference is used to refer to derived
Main obj = new Main(8);
class object.
System.out.println("object reference = " + obj);
Dynamic Binding – allows many meanings to be
}
associated to one method name.
}
 The definition of a method is not bound to an
invocation of the method until run time.
PRIVATE ACCESS MODIFIER
 Mechanism used by polymorphism
class Data {
Abstract class – base for subclasses
// private variable
Abstract method – declared as abstract and does not
private String name;
have an implementation.
}
 Written without braces followed by a
public class Main {
semicolon.
public static void main(String[] main){
 public abstract void methodName();
// create an object of Data
Data d = new Data();
// access private variable and field from another
class
d.name = "Programiz";
}
}
PUBLIC ACCESS MODIFIER rectangle.getArea();
}
// Animal.java file }
// public class
public class Animal { INFORMATION HIDING
// public variable
public int legCount; class Person {
// public method // private field
public void display() { private int age;
System.out.println("I am an animal."); // getter method
System.out.println("I have " + legCount + " legs."); public int getAge() {
} return age;
} }
// Main.java // setter method
public class Main { public void setAge(int age) {
public static void main( String[] args ) { this.age = age;
// accessing the public class }
Animal animal = new Animal(); }
// accessing the public variable class Main {
animal.legCount = 4; public static void main(String[] args) {
// accessing the public method // create an object of Person
animal.display(); Person p1 = new Person();
} // change age using setter
} p1.setAge(24);
// access age using getter
ENCAPSULATION System.out.println("My age is " + p1.getAge());
}
class Area { }

// fields to calculate area INHERITANCE


int length;
int breadth; class Animal {
// constructor to initialize values // field and method of the parent class
Area(int length, int breadth) { String name;
this.length = length; public void eat() {
this.breadth = breadth; System.out.println("I can eat");
} }
// method to calculate area }
public void getArea() { // inherit from Animal
int area = length * breadth; class Dog extends Animal {
System.out.println("Area: " + area); // new method in subclass
} public void display() {
} System.out.println("My name is " + name);
}
class Main { }
public static void main(String[] args) { class Main {
// create object of Area public static void main(String[] args) {
// pass value of length and breadth // create an object of the subclass
Area rectangle = new Area(5, 6); Dog labrador = new Dog();
// access field of superclass ACCESS SPECIFIER IN OVERRIDING
labrador.name = "Rohu";
labrador.display(); class Animal {
// call method of superclass protected void displayInfo() {
// using object of subclass System.out.println("I am an animal.");
labrador.eat(); }
} }
} class Dog extends Animal {
METHOD OVERRIDING public void displayInfo() {
System.out.println("I am a dog.");
class Animal { }
public void displayInfo() { }
System.out.println("I am an animal."); class Main {
} public static void main(String[] args) {
} Dog d1 = new Dog();
class Dog extends Animal { d1.displayInfo();
@Override }
public void displayInfo() { }
System.out.println("I am a dog.");
} ABSTRACT CLASS AND METHOD
}
class Main { abstract class Animal {
public static void main(String[] args) { abstract void makeSound();
Dog d1 = new Dog();
d1.displayInfo(); public void eat() {
} System.out.println("I can eat.");
} }
}
USE OF SUPER KEYWORD class Dog extends Animal {
// provide implementation of abstract method
class Animal { public void makeSound() {
public void displayInfo() { System.out.println("Bark bark");
System.out.println("I am an animal."); }
} }
} class Main {
class Dog extends Animal { public static void main(String[] args) {
public void displayInfo() { // create an object of Dog class
super.displayInfo(); Dog d1 = new Dog();
System.out.println("I am a dog."); d1.makeSound();
} d1.eat();
} }
class Main { }
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
INTERFACE }
}
// create an interface
interface Language { INHERITANCE
void getName(String name);
} // field and method of the parent class
// class implements interface String name;
class ProgrammingLanguage implements Language { public void eat() {
System.out.println("I can eat");
// implementation of abstract method }
public void getName(String name) { }
System.out.println("Programming Language: " +
name); // inherit from Animal
} class Dog extends Animal {
}
// new method in subclass
class Main { public void display() {
public static void main(String[] args) { System.out.println("My name is " + name);
ProgrammingLanguage language = new }
ProgrammingLanguage(); }
language.getName("Java");
} class Main {
} public static void main(String[] args) {

POLYMORPHISM // create an object of the subclass


Dog labrador = new Dog();
class Language {
public void displayInfo() { // access field of superclass
System.out.println("Common English Language"); labrador.name = "Rohu";
} labrador.display();
}
// call method of superclass
class Java extends Language { // using object of subclass
@Override labrador.eat();
public void displayInfo() {
System.out.println("Java Programming Language"); }
} }
}

class Main {
public static void main(String[] args) {

// create an object of Java class


Java j1 = new Java();
j1.displayInfo();

// create an object of Language class


Language l1 = new Language();
l1.displayInfo();

You might also like