Sunteți pe pagina 1din 30

CORE JAVA

•PRITI PATIL
•SOHA PATHAN
•GAYATRI SURAVASE
(BE CSE)
INTRODUCTION

 What is java?
 JVM
 How it works?
 Object
 Class
 Access control
INTRODUCTION-What is java?
• Programming Language-
• Another programming language using which we can develop
applets, standalone applications, web applications and
enterprise applications.
• Platform Independent-
• A java program written and compiled on one machine can be
executed on any other machine(irrespective of the operating
system).
• Object Oriented –
• Compiles to object oriented programming concepts.Your
program is not object oriented unless you code that way.
• Compiled and interpreted-
• The .java file is compiled to a .class file and the .class file is
interpreted to machine code.
JVM
 JVM stands for
Java Virtual Machine

 Unlike other languages, Java “executables” are


executed on a CPU that does not exist.
How it works?
 Java is independent only for one reason:
1. JVM(Java Virtual Machine) acts as a run-time engine
to run Java applications.
2. Only depends on the Java Virtual Machine(JVM)
3. Code is compiled to byte code, which is interpreted
by the resident JVM.
Objects
 identity – unique identification of an object
 attributes – data/state
 services – methods/operations
 supported by the object
 within objects responsibility to provide these services to
other clients
Class
 “type”
 object is an instance of class
 class groups similar objects
 same (structure of) attributes
 same services
 object holds values of its class’s attributes
Hello World
Hello.java

class Hello {
public static void main(String[] args) {
System.out.println(“Hello World !!!”);
}
}

C:\javac Hello.java ( compilation creates Hello.class )

C:\java Hello (Execution on the local JVM)


Access Control
 public member (function/data)
 Can be called/modified from outside.
 protected
 Can be called/modified from derived classes
 private
 Can be called/modified only from the current class
 default ( if no access modifier stated )
 Usually referred to as “Friendly”.
 Can be called/modified/instantiated from the same
package.
Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires


all the properties and behaviours of a parent object. It is an
important part of OOPs (Object Oriented programming system).

Why use inheritance in java

For Method Overriding (so runtime polymorphism can be achieved).


For Code Reusability.
Terms used in Inheritance

Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created.

Sub Class/Child Class: Subclass is a class which inherits the other class. It
is also called a derived class, extended class, or child class.

Super Class/Parent Class: Superclass is the class from where a subclass


inherits the features. It is also called a base class or a parent class.

Reusability: As the name specifies, reusability is a mechanism which


facilitates you to reuse the fields and methods of the existing class when you
create a new class. You can use the same fields and methods already defined
in the previous class.
The syntax of Java Inheritance

class Subclass-name extends Super class-name


{
//methods and fields
}

The extends keyword indicates that you are making a new class that derives
from an existing class. The meaning of "extends" is to increase the functionality.
In the terminology of Java, a class which is inherited is called a parent or super
class, and the new class is called child or subclass
Types of inheritance

•Single inheritance

•Multilevel inheritance

•Hierarchical inheritance
Single inheritance
 Single inheritance is damn easy to understand.
When a class extends another one class only then
we call it a single inheritance. The below flow diagram
shows that class B extends only one class which is A.
Here A is a parent class of B and B would be a child
class of A.
class Animal{
void eat()
{
System.out.println("eating...");}
}
class Dog extends Animal{
void bark()
{
System.out.println("barking...");}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}

Output:
barking... eating..
Multilevel inheritance
 When a class extends a class, which extends anther
class then this is called multilevel inheritance. For
example class C extends class B and class B extends
class A then this type of inheritance is known as
multilevel inheritance.
class Animal{
void eat(){System.out.println("eating...");}

}
class Dog extends Animal{
void bark(){System.out.println("barking...
");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping.
..");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping... barking... eating...
Hierarchical inheritance
 In Hierarchical Inheritance, one class serves as a super
class (base class) for more than one sub class . In below
image, the class A serves as a base class for the derived
class B,C and D.
class Animal{
void eat(){System.out.println("eating...");
}
}
class Dog extends Animal{
void bark(){System.out.println("barking..
.");}
}
class Cat extends Animal{
void meow(){System.out.println("meowi
ng...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing... eating...
Why multiple inheritance is not supported
in java?

 To reduce the complexity and simplify the language,


multiple inheritance is not supported in java.
 Consider a scenario where A, B, and C are three
classes. The C class inherits A and B classes. If A and B
classes have the same method and you call it from
child class object, there will be ambiguity to call the
method of A or B class.
 Since compile-time errors are better than runtime
errors, Java renders compile-time error if you inherit 2
classes. So whether you have same method or
different, there will be compile time error.
Exception handling
 The Exception Handling in Java is one of the
powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained.
 In this page, we will learn about Java exceptions, its type
and the difference between checked and unchecked
exceptions.
 What is Exception in Java
 Dictionary Meaning: Exception is an abnormal condition.
What is Exception Handling

Exception Handling is a mechanism to handle runtime errors such as


ClassNotFoundException, IO Exception, SQLException, RemoteException, etc.
Advantage of Exception Handling

The core advantage of exception handling is to maintain the


normal flow of the application. An exception normally
disrupts the normal flow of the application that is why we use
exception handling. Let's take a scenario:
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there are 10 statements in your program and there
occurs an exception at statement 5, the rest of the code will
not be executed i.e. statement 6 to 10 will not be executed. If
we perform exception handling, the rest of the statement will
be executed. That is why we use exception handling in Java.
Types of Java Exceptions
There are mainly two types of exceptions: checked and unchecked. Here,
an error is considered as the unchecked exception. According to Oracle,
there are three types of exceptions:

Checked Exception
Unchecked Exception
Error
1) Checked Exception

The classes which directly inherit Throwable class except


RuntimeException and Error are known as checked exceptions e.g.
IOException, SQLException etc. Checked exceptions are checked at
compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked


exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not
checked at compile-time, but they are checked at runtime.

3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError,
AssertionError etc
Exception handling keywords
 Try
 Catch
 Finally
 Throw
 Throws
class TestFinallyBlock
{
public static void main(String args[])
{
try
{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}

Output:5
finally block is always executed
rest of the code...
Thank you!

S-ar putea să vă placă și