Public Class Int Public Static Void New: ( ( Args) ( Objex - .Println (.) ) )
Public Class Int Public Static Void New: ( ( Args) ( Objex - .Println (.) ) )
Public Class Int Public Static Void New: ( ( Args) ( Objex - .Println (.) ) )
Object means a real-world entity such as a pen, chair, An Object can be defined as an instance
of a class
public class ObjEx {
int x = 5;
public static void main(String[] args) {
ObjEx myObj = new ObjEx();
System.out.println(myObj.x);
}
}
A class can also be defined as a blueprint from which you can create an individual object. Class
doesn't consume any space.
public class ClassEx {
int x = 5;
}
Method/functions
it is a block of code which only runs when it is called.& used to perform certain actions.
public class MethodEx {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
Polymorphism(Code Reusability)
Types of Polymorphism
1
Compile-Time Polymorphism (overloading)
When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded Obj is bound with thier functionality at compile time
package MyPackage2;// main class
package MyPackage2;//overloading
public class Student {
public void read(){
System.out.println("reading xyz book");
}
public void read(String bookName ){
System.out.println("reading "+bookName);
}
}
Binding (or wrapping) code and data together into a single unit.A java class is the example of
encapsulation. Java bean is the fully encapsulated class because all the data members are
2
private here.in this a class's variables are hidden from other classes and can only be accessed by
the methods of the class in which they are found.
2. Provide public setter and getter methods to modify & view the variable values.
package MyPackage2;
class Company{
public static void main(String args[]){
Employee employee=new Employee();
employee.setEmp_id(102);
System.out.println(employee.getEmp_id());
}
}
//output 102
Inheritance in Java (is- a relationship)(code reusability)(can achieve polymorphism method overriding)
It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of
another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits
from another class can reuse the methods and fields of that class. (using extends keyword)
Inheritance Types
⦁ Multilevel Inheritance- a derived class will be inheriting a base class, and as well as the derived
class also acts as the base class for other classes
package MyPackage2;
class A {
void showA() {
System.out.println("a class method ");
}
}
3
class B extends A{
void showB (){
⦁ Hierarchical Inheritance - one class serves as a superclass (base class) for more than one sub
class.
package MyPackage2;
⦁ Multiple Inheritance one class can have more than one superclass and inherit features from all
parent classes. Java does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces.(ambiguity)
4
public interface I1 {
abstract void show();
}
interface I2{
void display();
}
class Test implements I1,I2 {
@Override
public void show() {
System.out.println("hi");
}
@Override
public void display() {
System.out.println("hello");
}
public static void main(String args[]) {
Test test = new Test();
test.show();
test.display();
}
}
⦁ Hybrid Inheritance mix of two or more of the above types of inheritance. Since Java doesn’t
support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In
Java, we can achieve hybrid inheritance only through Interfaces.
While encapsulation groups together data and methods that act upon the data, data abstraction deal with
exposing the interface to the user and hiding the details of implementation.
Encapsulated classes are Java classes that follow data hiding and abstraction We can implement
abstraction by using abstract classes and interfaces.
Encapsulation is a procedure that takes place at the implementation level, while abstraction is a design-
level process
Abstraction
is a process which displays only the information needed and hides the unnecessary
information.(Data hiding)
An abstract class is a type of class that declares one or more abstract methods. An abstract
method is a method that has a method definition but not implementation.
5
Abstract methods are used when two or more subclasses do the same task in different ways
and through different implementations. An abstract class can have both methods, i.e., abstract
methods and regular methods.
If a regular class extends an abtract class then the class must have to implement all the abstract
methods of abstract parent class or it has to be declared abstract as well.
package MyPackage2;//abstraction
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle{
@Override
void start() {
System.out.println("car starts with key");
}
}
class Scooter extends Vehicle{
@Override
void start() {
System.out.println("Scooter starts with kick");
}
public static void main(String [] args){
Car car= new Car();
car.start();
Scooter scooter= new Scooter();
scooter.start();
}
Interface in Java (class ko krna kya h)(used to achieve abstraction)(loose coupling)(supports multiple inheritance)
An interface in java is a blueprint of a class. It has static constants and abstract methods.The
interface in java is a mechanism to achieve abstraction. There can be only abstract methods in
the java interface not method body. It is used to achieve abstraction and multiple inheritance
in.Java Interface also represents IS-A relationship. It cannot be instantiated just like abstract
class. There are mainly three reasons to use interface.
package MyPackage2;
public interface I1 {
abstract void show();
}
6
class Test implements I1{
@Override
public void show() {
System.out.println("hi");
}
public static void main(String args[]){
Test test =new Test();
test.show();
}
}
Coupling in Java
Coupling refers to the relationship between two classes. It indicates the knowledge one object
or class has of another. That means that if one class changes its properties or behaviour, it will
affect the dependent changes in the other class
Tight coupling: If one class is strongly interrelated to another class, it is said to have a tight
coupling with that class.
Loose coupling: If one class is weakly interrelated to another class, it is said to have loose
coupling with that class. Loose coupling is preferred over tight coupling.
obj class is the parent class of all class in java and throwable is the parent class of exception
Types of Exceptions
7
package MyPackage2.ExceptionHandling;
public class a {
public static void main(String args []) {
System.out.println("start");
int result = 0;
try {
int n1 = 100;
int n2 = 0;
System.out.println("2no.");
result = n1 / n2;
} catch (Exception e) {
System.out.println("n2=!0");
System.out.println(e.getMessage());
}
finally {
System.out.println("hey finally");
}
System.out.println(result);
System.out.println("terminate");
}
}
OR
package MyPackage2.ExceptionHandling;
public class Test {
public static void main (String args []){
try {
System.out.println(100 / 0);
} catch (Exception e) {
System.out.println("exception");
System.out.println(e.getMessage());
}
}
}
8
exception obj= exception(class name) description(message)stacktrace(loaction of exception)
we can handle the exception using 5 keywords i.e. try , catch, finally,throw,throws.
e.printStackTrace,e,e.toString,e.getMessage
finally block- it always executed wheather exception is handled or not jo resources try me open kiye vo
yha close honge/clean up
we can use multiple catch blocks with one try but we can use single finally block with one try.
i.e using system.exit .// causing a fatal error that causes the process to abort
9
10