Java OOP
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with less code and shorter development time
Class
A Class is like a "blueprint" for creating objects.
public class MyClass {
int x = 5;
}
Object
An object is created from a class.
public class MyClass {
int x = 5;
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}Static vs Non-Static Java Class Methods
A static
method, which means that it can be accessed without creating an object of the class.
A public which can only be accessed by
objects.
public class MyClass {
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args) {
myStaticMethod(); // Call the static method
MyClass myObj = new MyClass(); // Create an object of MyClass
myObj.myPublicMethod(); // Call the public method on the object
}
}Constructor
A constructor in Java is a special method that is used to initialize objects.
It can be used to set initial values for object attributes
public class Car { int modelYear; String modelName; public Car(int year, String name) { modelYear = year; modelName = name; } public static void main(String[] args) { Car myCar = new Car(1969, "Mustang"); System.out.println(myCar.modelYear+" "+myCar.modelName);// Output: 1969 Mustang} }
Access Modifiers
| public The code is accessible for all classes private The code is only accessible within the declared class default The code is only accessible in the same package. This is used when you don't specify a modifier. protected The code is accessible in the same package and subclasses. final Attributes and methods cannot be overridden/modified static Attributes and methods belongs to the class, rather than an object abstract Can only be used in an abstract class, and can only be used on methods. The method does not have a body. The body is provided by the subclass (inherited from). Abstraction | ||
Encapsulation
| ||
Static keyword
No object needs to be created to use static variable or call static methods, just put the class name before the static variable or method to use them.
A Static method can not call a non-static method.
It makes your program memory efficient (i.e., it saves memory).
When and why we use static variable:
Suppose we want to a store record of all employees of any company, in this case, employee id is unique for every employee but company name is common for all. When we create a static variable as a company name then only once memory is allocated otherwise it allocates a memory space each time for every employee.
Difference between static and final keyword: Video Link
static keyword always fixed the memory that means that it will be located only once in the program whereas final keyword always fixed the value that means it makes variable values constant.
Why Main Method Declared Static ?
Because the object is not required to call static method. If main() is non-static method, then JVM creates an object first then calls main() method due to that face the problem of extra memory allocation.
Inheritance
- subclass (child) - the class that inherits from another class
- superclass (parent) - the class being inherited from
To inherit from a class, use the extends
keyword.
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
Car myCar = new Car();
myCar.honk();
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
Method overloading
Multiple methods can have the same name with different parameters:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)Method overriding
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
Usage of Java Method Overriding
- Method overriding is used to provide the specific implementation of a method which is already provided by its super class.
- Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
- The method must have the same name as in the parent class
- The method must have the same parameter as in the parent class.
- There must be an IS-A relationship (inheritance).
Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myPig = new Pig();
Animal myDog = new Dog();
myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
}
}
Why And When To Use "Inheritance" and "Polymorphism"?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Java Abstraction
Data abstraction is the process of hiding certain details and showing only essential information to the user.
// Abstract class
abstract class Animal {
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep() {
System.out.println("Zzz");
}
}
class Pig extends Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Why And When To Use Abstract Classes and Methods?
To achieve security - hide certain details and only show the important details of an object.
Note: Abstraction can also be achieved with Interfaces.Interfaces
Another way to achieve abstraction in Java, is with interfaces.
An interface is a completely "abstract class"
that is used to group related methods with empty bodies.
To access the interface methods, the interface must be "implemented"
(kinda like inherited) by another class with the implements
keyword (instead of extends). The body of the
interface method is provided by the "implement" class:
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
class Pig implements Animal {
public void animalSound() {
System.out.println("The pig says: wee wee");
}
public void sleep() {
System.out.println("Zzz");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSound();
myPig.sleep();
}
}
Using Build in Classes
Comments
Post a Comment