0% found this document useful (0 votes)
9K views

Java Essentials PPT Lesson02

Java Essentials

Uploaded by

The Gadget Bytes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9K views

Java Essentials PPT Lesson02

Java Essentials

Uploaded by

The Gadget Bytes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Java for Hadoop – Lesson 2

Java Constructors

Copyright © 2012-2014, Simplilearn, All rights reserved.


Objectives

In this lesson, you will be able to:


• Identify the various features of Java and how the object-oriented
programming techniques implement these features
• Discuss how to write and use constructors in Java programs
• Discuss how to create packages and application of packages
• Discuss how to reuse Java classes and codes through inheritance
• Identify the purpose and application of method overriding and method
overloading
• Discuss how to use abstract classes and interfaces
• Understand how interactive programs are written using input and output
classes in Java

Copyright © 2012-2014, Simplilearn, All rights reserved. 2


Features of Java

Following are the features of Java:

Platform
independent

Distributed
Robust
design

Features
of Java

Object- Internet
oriented friendly

Multi
threaded

Copyright © 2012-2014, Simplilearn, All rights reserved. 3


Classes, Objects, and Constructors

Classes define basic structure for the creation of objects.


Following is the syntax for creating a class:
class class_name{
// define body of class
}
Classes comprise variables and methods, which represent the properties and behavior of the objects of the class.

Object is an instantiation of class. An object of a class is created using ‘new’ operator.


Objects of a class are created according to the following syntax:
class_name obj = new class_name();

Constructors are methods used to initialize values of the object when instantiated.

Copyright © 2012-2014, Simplilearn, All rights reserved. 4


Constructors
The code demonstrates the usage of constructors.
public class Const_Demo {

int a;
int b;
Const_Demo()
{
a=100;
System.out.println("Variable a
initialized to "+a);
b=200;
System.out.println("Variable b
initialized to "+b);
}
public static void main(String
args[]){
Const_Demo c1 = new Const_Demo();
}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 5


Constructor Overloading

Constructors of a class can be defined as follows:


class example_classname{
//constructor declaration
Example_classname(){
//initialize the variables
}
}
• A constructor can be parameterized.
• There can be more than one constructor in a class. These constructors differ in the parameter list. This feature is
called constructor overloading.
• Appropriate constructor is invoked based on the parameters passed while creating the object.

Copyright © 2012-2014, Simplilearn, All rights reserved. 6


Constructor Overloading (contd.)

The code demonstrates default and parameterized constructors.


public class Const_Demo {

int a;
int b;
Const_Demo()
{
a=100;
System.out.println("Variable a initialized to "+a);
b=200;
System.out.println("Variable b initialized to "+b);
}
Const_Demo(int x,int y){
a=x;
b=y;
System.out.println("Variable a initialized to "+a);
System.out.println("Variable b initialized to "+b);
}
public static void main(String args[]){
Const_Demo c1 = new Const_Demo();
Const_Demo c2 = new Const_Demo(10,20);
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 7


PACKAGES

Copyright © 2012-2014, Simplilearn, All rights reserved. 8


Definition of Packages

Packages are:
• Logical grouping of classes and interfaces.
• Created to avoid naming conflicts among different classes.
• Predefined in Java.
• Analogous to directories of files, where the directories comprise class files.

Following is the syntax for defining the package:


package package_name;
The syntax is prefixed to the class definition which is supposed to become a part of the package.

Copyright © 2012-2014, Simplilearn, All rights reserved. 9


Advantages of Packages

Following are the advantages of packages:

Enable reuse of code

Prevent name collisions

Provide protected access to the data members of the class

Copyright © 2012-2014, Simplilearn, All rights reserved. 10


Naming Conventions of Packages

The naming convention for packages is as follows:

All package names should be in lower case.

All the packages are located relative to the base_location of Java in the file system.

User defined packages are also located relative to the base_location.

Copyright © 2012-2014, Simplilearn, All rights reserved. 11


INHERITANCE

Copyright © 2012-2014, Simplilearn, All rights reserved. 12


Definition of Inheritance

Inheritance is a mechanism used for reuse of code and


functionality of classes to create new classes.
Classes are inherited with the help of ‘extends’ keyword.

Following is the syntax for using inheritance to create new


classes:
class new_implementation extends
class existing_implementation{…..}

The basic type of inheritance where there is only one base


class and child class is called single level inheritance.

The figure demonstrates single level inheritance.

Copyright © 2012-2014, Simplilearn, All rights reserved. 13


Multilevel Inheritance

Multilevel inheritance is a pattern of inheriting one class


from another at various levels. The child class of an
inheritance becomes the base class in the next level.

In the given example, Person class is the base class of


Student class and Student class is the base class of
Research_Assistant class.

The given figure demonstrates multilevel inheritance.

Copyright © 2012-2014, Simplilearn, All rights reserved. 14


Hierarchical Inheritance

In hierarchical inheritance one base class is extended by multiple child classes.


For example:

Copyright © 2012-2014, Simplilearn, All rights reserved. 15


Method Overriding

The process of redefining the method in the child class is known as method overriding.

To override a method, the method definition in the child class must have the same signature as that in the base class.

The super keyword is used to access the method of the base class, which is also referred to as super class at times.

Copyright © 2012-2014, Simplilearn, All rights reserved. 16


Method Overriding (contd.)
Given are the definitions of class Circle and Cylinder, where Cylinder class inherits the class Circle.
package Geometry; package Geometry;
public class Circle { public class Cylinder extends Circle{
double radius; double r;
public Circle(){ double h;
radius =5.0; public Cylinder(double rad, double
} ht){
r = rad;
public Circle(double rad)
h=ht;
{
}
rad = radius;
public double area(){
}
return((super.area())*h);
public double area(){
}
return(3.14*this.radius*this.radius);
}
}
public double circumference(){
return(6.28*(this.radius));
}}

Copyright © 2012-2014, Simplilearn, All rights reserved. 17


Method Overriding (contd.)

Once the child class Cylinder is created, the main method program is written as shown in the code here.
import Geometry.*;
public class MetOverDemo {
public static void main(String
args[]){
Cylinder c1 = new
Cylinder(3,5);
double res = c1.area();
Circle c2 = new Circle(3);
System.out.println(res);
double res2 = c2.area();
System.out.println(res2);
}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 18


ABSTRACT CLASSES

Copyright © 2012-2014, Simplilearn, All rights reserved. 19


Definition of Abstract Classes

Abstract class is one which has abstract methods. In abstract methods, the method declaration is present but not the
definitions.
Such methods are represented by prefixing ‘abstract’ keyword to the method.
Syntax:
abstract class class_name{…}
abstract return_type method_name(parameter_list);

• Abstract classes can only be inherited not instantiated.

• The definition of abstract methods is provided in the child class.

Copyright © 2012-2014, Simplilearn, All rights reserved. 20


Usage of Abstract Classes
The code shown here demonstrates the usage of abstract class.
abstract class First{
defined_behavior()
{
System.out.println("Method defined in
class First");
}
abstract void undefined_behavior();
}
class Second extends First{
undefined_behavior(){
System.out.println("Undefined in First bu
defined in Second");
}
}
public class Abstract_Demo {
public static void main(String args[]){
Second s = new Second();
s.defined_behavior();
s.undefined_behavior();

Copyright © 2012-2014, Simplilearn, All rights reserved. 21


INTERFACES

Copyright © 2012-2014, Simplilearn, All rights reserved. 22


Features of Interfaces

An interface is an abstract class, where all the methods are abstract methods. Following are the features of interfaces:

If an interface has a method definition, then the method is a static final method.

Any class which uses the interface implements the interface, here ‘implements’ is a keyword like
‘extends’.
The child class which is implementing an interface has to define all the abstract methods of the
interface otherwise the child class becomes an abstract class and cannot be instantiated.

Interfaces do not have constructors.

A class can implement more than one interface.

Copyright © 2012-2014, Simplilearn, All rights reserved. 23


Syntax for Creating Interfaces

Syntax for creating an interface: Example of an interface named Comparable:

public interface interface_name{ package Geometry;


public interface Comparable {

// declaration of constants int compareTo(Object other);


// method signatures
}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 24


Implementing an Interface

Following is the syntax for implementing an interface:


class class_name implements interface_name{
//implementation of all the abstract methods in the interface
}

The interface created earlier can be implemented by a class and the methods of the interface can be invoked
through the instances of the class.

Copyright © 2012-2014, Simplilearn, All rights reserved. 25


Implementing an Interface (contd.)
The code shows the implementation of the interface ‘Comparable’.

public class Circle implements Comparable {


double radius;
public Circle(){
radius =5.0;
}

public Circle(double rad)


{
radius=rad;
}
public int compareTo(Object obj) {
Circle C = (Circle)obj;
if(radius>C.radius) {
return 1;
} else {
return -1;
}

}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 26


INPUT AND OUTPUT

Copyright © 2012-2014, Simplilearn, All rights reserved. 27


Features of Input and Output

In Java:
– Input and output are implemented as streams.
– Streams are handled through classes such as BufferedStream, InputStream, and OutputStream.
– Formatted data is handled through Scanner classes.
– The input and output functions are implemented through the packages java.io.

Reading Input from Keyboard


– System.in.read() method can be used to read input data from the keyboard.
– It reads data, one character at a time, from the keyboard.

Copyright © 2012-2014, Simplilearn, All rights reserved. 28


System.in.read() Method

Following program demonstrates the usage of System.in.read() method.

public class KeyBoardInput {


public static void main(String[] args)
throws IOException {

int c;
System.out.print("Enter a
character:");
c = System.in.read();
System.out.println("You entered " +
c);
}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 29


Reading Input from the Console

Input and output in Java is handled through one of the following streams of data:

ByteStreams CharacterStreams BufferedStreams

An additional buffer is
These streams are
Read data one allocated for the
used to read data in
character at a time. stream in the main
bytes.
memory.

Copyright © 2012-2014, Simplilearn, All rights reserved. 30


Stream Objects
The program demonstrates the usage of BufferedStream objects.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Stream_demo {
public static void main(String[] args)
throws IOException {
String name;
BufferedReader reader;//create
BufferedReader object
read=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your
name:");
name=read.readLine();
System.out.println(name);
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 31


StringTokenizer Class

StringTokenizer class divides the string into tokens.


import java.util.StringTokenizer;
• These tokens are manipulated and used with methods public class Stringtokens {
of the class according to the requirement. public static void main(String[] args)
• The class has methods such as hasMoreTokens(), {
nextToken(), countTokens(), and so on to handle the
tokens in the String. String s = "1,2,4,8,6,7";
Given is a code which uses StringTokenizer class, the int pro = 1;
program calculates the product of all the integers in a StringTokenizer st = new
given String object. StringTokenizer(s, ",");

while(st.hasMoreTokens())
pro =
pro*(Integer.parseInt(st.nextToken()));
System.out.println("The product
is: " + pro);
}
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 32


Scanner Class

Scanner class reads and writes formatted data. import java.util.Scanner;


public class ScannerDemo {
It can be used as a wrapper class on any other Stream public static void main(String[] args) {
class to format the Stream. int pro = 1;
Scanner s;
s = new Scanner(System.in);
Given program demonstrates the usage of Scanner class, Integer i= new Integer(1);
where the Scanner class is wrapped over the while(i!=0){
System.read.in stream. pro = pro*i;
i = s.nextInt();
}
System.out.println(pro);
}

Copyright © 2012-2014, Simplilearn, All rights reserved. 33


Writing Output to the Console

The most common way of writing output to the console is through System.out.print() and System.out.println() methods.

System.out.print() and System.out.println() are overloaded methods with different parameters.

Copyright © 2012-2014, Simplilearn, All rights reserved. 34


Summary

Let us summarize the topics covered in this lesson:


• Constructors are used to initialize class objects. Default constructors and
parameterised constructors can be defined. Constructor overloading is also
possible.
• Classes are logically grouped together to form packages. They enable reuse
of code. APIs of Java are implemented through packages.
• Inheritance also enables reuse of code. Java supports multilevel inheritance
and hierarchical inheritance.
• Abstract classes allow partial definition of classes.
• Interfaces implement abstract classes by defining all the abstract methods
in it. They enable distributed development of the components and their
integration in an application.
• Java handles input and output as streams. It supports input and output
through classes such as Scanner, InputStreamReader, BufferedReader, and
so on.

Copyright © 2012-2014, Simplilearn, All rights reserved. 35


Thank You

Copyright © 2012-2014, Simplilearn, All rights reserved. 36

You might also like