[Go to site: main page, start]

0% found this document useful (0 votes)
4 views37 pages

Module 2 - Core Java

The document outlines a course module on Core Java, focusing on object-oriented features such as classes, methods, constructors, inheritance, method overloading, and interfaces. It explains key concepts like access specifiers, garbage collection, and the use of keywords like final and static. Additionally, it covers method overriding, abstract classes, and the significance of interfaces in achieving abstraction and multiple inheritance in Java.
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)
4 views37 pages

Module 2 - Core Java

The document outlines a course module on Core Java, focusing on object-oriented features such as classes, methods, constructors, inheritance, method overloading, and interfaces. It explains key concepts like access specifiers, garbage collection, and the use of keywords like final and static. Additionally, it covers method overriding, abstract classes, and the significance of interfaces in achieving abstraction and multiple inheritance in Java.
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

CORE JAVA -

CSIT751

Mr Sumin Sybol
Visiting Faculty,
Amity Institute of Information Technology
Amity University, Noida
MODULE 2 –
JAVA WITH OBJECT ORIENTATED FEATURES

• Introducing Classes, A Closer look at Methods and Classes, constructors, types of


constructors, and method overloading;
• Inheritance, Single Inheritance, Multilevel hierarchy; Method overriding; Abstract classes;
Interface; final; static; super; Garbage Collection;
CLASS FILE

• A Java class file is a file containing Java bytecode and having .class extension that can be
executed by JVM.
• A Java class file is created by a Java compiler from .java files as a result of successful
compilation.
• As we know a single Java programming language source file (or we can say .java file) may
contain one class or more than one class.
• So if a .java file has more than one class then each class will compile into a separate class
files.
CLASS FILE
// Compiling this Java program would result in multiple class files.
class Sample
{
}

class Student
{
}

class Test
{
public static void main(String[] args)
{
[Link]("Class File Structure");
}
}
METHOD

• A method is a block of code or collection of statements, or a set of code grouped together


to perform a certain task or operation.
• It is used to achieve the reusability of code.
• We write a method once and use it many times.
• We do not require to write code again and again.
• It also provides the easy modification and readability of code, just by adding or removing a
chunk of code.
• The method is executed only when we call or invoke it.
METHOD DECLARATION
ACCESS SPECIFIER

• Access Specifier: Access specifier or modifier is the access type of the method. It specifies
the visibility of the method. Java provides four types of access specifier:
• Public: The method is accessible by all classes when we use public specifier in our
application.
• Private: When we use a private access specifier, the method is accessible only in the
classes in which it is defined.
• Protected: When we use protected access specifier, the method is accessible within the
same package or subclasses in a different package.
• Default: When we do not use any access specifier in the method declaration, Java uses
default access specifier by default. It is visible only from the same package only
CONSTRUCTORS

• In Java, a constructor is a block of codes similar to the method. It is called when an


instance of the class is created. At the time of calling the constructor, memory for the object
is allocated in the memory.

• It is a special type of method that is used to initialize the object.

• Every time an object is created using the new() keyword, at least one constructor is called.

• It calls a default constructor if there is no constructor available in the class. In such cases,
the Java compiler provides a default constructor by default.
CONSTRUCTORS

Rules for creating Java constructor


• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized

There are two types of constructors in Java:


1. Default constructor (no-arg constructor)
2. Parameterized constructor
CONSTRUCTORS

class Geek
{
.......

// A Constructor
Geek() {
}

.......
}
METHOD OVERLOADING

If a class has multiple methods having the same name but different parameters, it is known as
Method Overloading.

There are two ways to overload the method in Java


• By changing the number of arguments
• By changing the data type
METHOD OVERLOADING

• By changing the number of arguments

class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}

class TestOverloading1{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](11,11,11));
}}
METHOD OVERLOADING

• By changing the data type

class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](12.3,12.6));
}}
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
systems).

The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
the parent class. Moreover, you can add new methods and fields in your current class.

Inheritance represents the IS-A relationship, which is also known as a parent-child


relationship.
INHERITANCE IN JAVA

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: A Subclass is a class that 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 that 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.
INHERITANCE IN JAVA

SYNTAX:
class Subclass-name extends Superclass-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 that is inherited is called a parent or


superclass, and the new class is called a child or subclass.
TYPES OF INHERITANCE
TYPES OF INHERITANCE
TYPES OF INHERITANCE

Q) 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 the A and B classes. If
A and B classes have the same method and you call it from the 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 errors if you
inherit 2 classes. So, whether you have the same method or different, there will be a compile
time error.
JAVA MULTILEVEL HIERARCHY

• Java Multilevel Hierarchy allows you to inherit properties of a grandparent in a child class.
• In simple inheritance, a sub-class or derived class derives its properties from its parent or super
class. However in multilevel inheritance, a sub-class is derived from a derived class.
• Each class inherits only a single class. Therefore, in multilevel inheritance, a ladder is formed,
which at each step increases by one and the lowermost class will have the properties of all the
super classes.
METHOD OVERRIDING

• If the subclass (child class) has the same method as declared in the parent class, it is known as
method overriding in Java.
• In other words, If a subclass provides the specific implementation of the method that has been
declared by one of its parent class, it is known as method overriding.

• Usage of Java Method Overriding


• Method overriding is used to provide the specific implementation of a method that is
already provided by its superclass.
• 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).
METHOD OVERRIDING

class Vehicle{
void run(){
[Link]("Vehicle is running");}
}

class Bike2 extends Vehicle{


void run(){
[Link]("Bike is running safely");}

public static void main(String args[])


{
Bike2 obj = new Bike2();//creating object
[Link]();//calling method
}
}
ABSTRACT CLASS

A class that is declared as abstract is known as an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

A method that is declared as abstract and does not have implementation is known as an abstract
method.

Points to Remember
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods that will force the subclass not to change the body of the method.
ABSTRACT CLASS

abstract class A{} class Honda extends Bike


{
abstract void printStatus(); void run(){
[Link]("running safely");
Example: }
abstract class Bike public static void main(String args[]){
{ Bike obj = new Honda();
abstract void run(); [Link]();
} }
}
ABSTRACT CLASS

abstract class Bank{ class TestBank{


abstract int getRateOfInterest(); public static void main(String args[]){
} Bank b;
b=new SBI();
class SBI extends Bank{ [Link]("Rate of Interest is:
int getRateOfInterest(){return 7;} "+[Link]()+" %");
} b=new PNB();
class PNB extends Bank{ [Link]("Rate of Interest is:
int getRateOfInterest(){return 8;} "+[Link]()+" %");
} }
}
INTERFACE

• 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.

• Since Java 8, we can have default and static methods in an interface.


• Since Java 9, we can have private methods in an interface.
INTERFACE

Why use Java interface?


There are mainly three main reasons to use an interface. They are:
• It is used to achieve abstraction.
• By interface, we can support the functionality of multiple inheritance.
• It can be used to achieve loose coupling.
INTERFACE

SYNTAX:

interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
}
INTERFACE
INTERFACE – MULTIPLE INHERITANCE

interface Printable{
void print();
}

interface Showable{
void print();
}

class TestInterface3 implements Printable, Showable{


public void print(){[Link]("Hello");}
public static void main(String args[]){
TestInterface3 obj = new TestInterface3();
[Link]();
}
}
FINAL

The final keyword in Java is used to restrict the user. The Java final keyword can be used in many
contexts. The final can be:

• variable
• method
• Class

The final keyword can be applied to the variables; a final variable that has no value is called a blank final
variable or an uninitialized final variable. It can be initialized in the constructor only. The blank final
variable can also be static, which will be initialized in the static block only. We will have detailed learning
of these. Let’s first learn the basics of the final keyword.
STATIC

The static keyword in Java is used for memory management mainly. We can apply static keywords with
variables and methods. The static keyword belongs to the class rather than an instance of the class.

The static can be:


• Variable (also known as a class variable)
• Method (also known as a class method)
STATIC VARIABLE

class Counter2{
static int count=0
• The static variable can be used to refer
Counter2(){
to the common property of all objects
count++;
(which is not unique for each object), for
[Link](count);
example, the company name of
}
employees, college name of students,
etc.
public static void main(String args[]){
• The static variable gets memory only
Counter2 c1=new Counter2();
once in the class area at the time of class
Counter2 c2=new Counter2();
loading.
Counter2 c3=new Counter2();
}
}
STATIC METHOD

class Calculate{
static int cube(int x){
• A static method belongs to the class
return x*x*x;
rather than the object of a class.
}
• A static method can be invoked without
the need for creating an instance of a
public static void main(String args[]){
class.
int result=[Link](5);
• A static method can access static data
[Link](result);
members and can change their value it.
}
}
SUPER

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is
referred by super reference variable.

Usage of Java super Keyword


• super can be used to refer immediate parent class instance variable.
• super can be used to invoke immediate parent class method.
• super() can be used to invoke immediate parent class constructor.
SUPER

class Animal{
String color="white";
}
class TestSuper1{
public static void main(String args[]){
class Dog extends Animal{
Dog d=new Dog();
String color="black";
[Link]();
void printColor(){
}
[Link](color);
}
[Link]([Link]);
}
}
ASSIGNMENT

- Explain Static keyword and Garbage Collection.

Note:
• Assignment should be of a minimum of 5 pages.
• Assignments are to be done individually.
• The completed assignments will be uploaded in the Google Classroom as a PDF and submitted to the faculty as a hard copy.

You might also like