0% found this document useful (0 votes)
10 views10 pages

Public Class Int Public Static Void New: ( ( Args) ( Objex - .Println (.) ) )

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

OOPs (Object-Oriented Programming System)

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);
}
}

Collection of objects is called class. It is a logical entity.

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)

If one task is performed in different ways, it is known as polymorphism.ex: to convince the


customer differently, to draw some shape. we use method overloading and method overriding
to achieve polymorphism.it is the ability of a message to be displayed in more than one form.

Types of Polymorphism

Compile-time Polymorphism Runtime


Polymorphism

1
Compile-Time Polymorphism (overloading)

This type of polymorphism is achieved by function overloading or operator 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

public class Main {


public static void main (String args []){
Student student=new Student();
student.read( "Maths Book");
}
}

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);
}
}

//output---- reading Maths Book

Runtime Polymorphism (overriding)

This type of polymorphism is achieved by Function OverridingThe function call is resolved at


runtime in runtime polymorphism. Obj is bound with thier functionality at run time.
package MyPackage2;// main class

public class Main {


public static void main (String args []){
Person person=new Emp();// can change during runtime can be Emp or can be
showDetail
person.showDetail( );
}
}

package MyPackage2;// overriding during runtime

public class Person {


public void showDetail(){
System.out.println("Basic Details");
}
}
class Emp extends Person {
public void showDetail() {
System.out.println("details of employee");
}
}

//details of employee or Basic details

Encapsulation(DATA+METHODS)(Data Hiding/ info hiding)

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.

1. Declare the variables of class as private

2. Provide public setter and getter methods to modify & view the variable values.
package MyPackage2;

public class Employee {


private int emp_id;
public void setEmp_id(int eid){
emp_id=eid;
}
public int getEmp_id()
{
return emp_id;
}
}

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)

Constructor and private classes are not inherited.

Inheritance Types

⦁ Single Inheritance - subclasses inherit the features of one superclass


package MyPackage2;
public class Animal {
void eat(){
System.out.println("hey ");
}
}
class Dog extends Animal{
public static void main(String args[]){
Dog dog= new Dog();
dog.eat();
}

⦁ 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 (){

System.out.println("b class method");


}
}
class C extends B{
static void showC(){
System.out.println("C class method");
}
}

public static void main(String[] args) {


A a = new A();
a.showA();
System.out.println("----------------");
B b = new B();
b.showA();
b.showB();
System.out.println("----------------");
C c = new C();
c.showA();
c.showB();
C.showC();
System.out.println("----------------");
}

⦁ Hierarchical Inheritance - one class serves as a superclass (base class) for more than one sub
class.
package MyPackage2;

public class A { //multilevel inheritance


public void showA() {

System.out.println("a class method ");


}

public static void main(String[] args) {


A a = new A();
a.showA();
System.out.println("----------------");
B b = new B();
b.showA();
b.showB();
System.out.println("----------------");
C c = new C();
c.showA();
C.showC();
System.out.println("----------------");
}
}
class B extends A{
void showB (){

System.out.println("b class method");


}
}
class C extends A{
static void showC(){
System.out.println("C class method");
}
}

⦁ 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)

package MyPackage2; //multiple inheritance using interface

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.

Encapsulation vs Data Abstraction

Encapsulation is data hiding(information hiding) Abstraction is detailed hiding(implementation hiding).

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

(detail hiding/implementation hiding) 2 ways to achive abstraction = using abstract class


(between 0-100%) and interface (purely 100)%

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.

o It is used to achieve abstraction.

o By interface, we can support the functionality of multiple inheritance.

o It can be used to achieve loose coupling.

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.

Exception Handling used to handle the runtime errors such as ClassNotFoundException,


IOException, SQLException, RemoteException, etc.so that the regular flow of the application
can be preserved. Error: An Error indicates a serious problem that a reasonable application
should not try to catch.Exception: any unexpected/ unwanted event that occurs during
execution of program at runtiome that disturbs the normal flow of program.

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.

the possibilties that disturbs the execution of finally block

i.e using system.exit .// causing a fatal error that causes the process to abort

throw and throws -

Java Custom Exception

9
10

You might also like