Java PRG - Intr, Classes, Inheriance, Interface
Java PRG - Intr, Classes, Inheriance, Interface
JAVA PROGRAMMING
Dr.N.BASKAR
What is Java?
02
Why use
03 Java
History of 04
Setting Path Java
05
Classes 06
07 Inheritance
Interface 08
Introduction
• Java is −
• Object Oriented − In Java, everything is an Object. Java can be easily extended
since it is based on the Object model.
• Platform Independent − when Java is compiled, it is not compiled into platform
specific machine, rather into platform independent byte code. This byte code is
distributed over the web and interpreted by the Virtual Machine (JVM) on
whichever platform it is being run on.
• Simple − Java is designed to be easy to learn. If you understand the basic concept
of OOP Java, it would be easy to master.
• Secure − With Java's secure feature it enables to develop virus-free, tamper-free
systems. Authentication techniques are based on public-key encryption.
• Architecture-neutral − Java compiler generates an architecture-neutral object file
format, which makes the compiled code executable on many processors, with the
presence of Java runtime system.
What is Java?
• Portable − Being architecture-neutral and having no implementation dependent
aspects of the specification makes Java portable.
• Robust − Java makes an effort to eliminate error prone situations by emphasizing
mainly on compile time error checking and runtime checking.
• Multithreaded − With Java's multithreaded feature it is possible to write programs
that can perform many tasks simultaneously.
• Interpreted − Java byte code is translated on the fly to native machine instructions
and is not stored anywhere.
• High Performance − With the use of Just-In-Time compilers, Java enables high
performance.
• Distributed − Java is designed for the distributed environment of the internet.
• Dynamic − Java is considered to be more dynamic than C or C++ since it is designed
to adapt to an evolving environment.
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to
Java or vice versa
Why Use Java?
It is owned by Oracle, and more than 3 billion devices run Java.
J2SE for Java Standard Edition, J2EE for Enterprise Application Edition, J2ME
for Mobile Application Edition
It is used for:
Mobile applications (specially Android apps)
Desktop applications
Web applications
Web servers and application servers
Games
Database connection
History of Java
• James Gosling initiated Java language project in June 1991 for use in one of
his many set-top box projects. The language, initially called ‘Oak’ after an
oak tree that stood outside Gosling's office, also went by the name ‘Green’
and ended up later being renamed as Java, from a list of random words.
• Sun released the first public implementation as Java 1.0 in 1995. It
promised Write Once, Run Anywhere (WORA), providing no-cost run-times
on popular platforms.
• On 13 November, 2006, Sun released much of Java as free and open source
software under the terms of the GNU General Public License (GPL).
• On 8 May, 2007, Sun finished the process, making all of Java's core code free
and open-source, aside from a small portion of code to which Sun did not
hold the copyright.
Popular Java Editors
• Notepad − On Windows machine, you can use any simple text editor like
Notepad, TextPad,etc.,
• Netbeans − A Java IDE that is open-source and free which can be
downloaded from https://www.netbeans.org/index.html.
• Eclipse − A Java IDE developed by the eclipse open-source community and
can be downloaded from https://www.eclipse.org/
Definition : Object, Class, Methods, Instance
• Object − Objects have states and behaviors. Example: A dog has states - color,
name, breed as well as behavior such as wagging their tail, barking, eating. An
object is an instance of a class.
• Class − A class can be defined as a template/blueprint that describes the
behavior/state that the object of its type supports.
• Methods − A method is basically a behavior. A class can contain many methods. It
is in methods where the logics are written, data is manipulated and all the actions
are executed.
• Instance Variables − Each object has its unique set of instance variables. An
object's state is created by the values assigned to these instance variables.
Setting permanent path
Setting permanent path
Java Compiler
Java Translation
• The Java compiler translates Java source code into a
special representation called bytecode
• Java provides a number of access modifiers to set access levels for classes,
variables, methods and constructors. The four access levels are −
• Visible to the package, the default. No modifiers are needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
Non-Access Modifiers
class rectangle
class rectareas
{int length,width; {public static void main(String ab[])
void getdata(int x,int y) {int a1,a2;
{length=x; rectangle rect1=new rectangle();
width=y; rectangle rect2=new rectangle();
} rect1.length=5;
int rectarea() rect1.width=10;
{int area=length*width; a1=rect1.length*rect1.width;
return(area); rect2.getdata(10,10);
} a2=rect2.rectarea();
} System.out.println("Area 1= "+a1);
System.out.println("Area 2= "+a2);
Inheritance
• Inheritance is the mechanism of deriving new class from old one, old
class is knows as superclass and new class is known as subclass.
• The subclass inherits all of its instances variables and methods defined by
the superclass and it also adds its own unique elements.
• Thus we can say that subclass are specialized version of superclass.
• Benefits of Java’s Inheritance
• Reusability of code
• Code Sharing
• Consistency in using an interface
Inheritance
Superclass(Base Class) Subclass(Child Class)
It is a class from which other It is a class that inherits some or
classes can be derived. all members from superclass.
The super keyword is similar to this keyword following are the scenarios where the super
keyword is used.
It is used to differentiate the members of superclass from the members of subclass, if they
have same names.
super.variable
super.method();
Output:
call Immediate Parent Class Constructor
using super Keyword
• The super() keyword can be used to invoke the Parent class constructor
Method Overriding
• If a class inherits a method from its super class, then there is a
chance to override the method provided that it is not marked
final.
• The benefit of overriding is: ability to define a behavior that's
specific to the subclass type which means a subclass can
implement a parent class method based on its requirement.
• In object-oriented terms, overriding means to override the
functionality of an existing method.
Method Overriding : Program
class sub extends Super class overrides
class Super
{ { {
int x; int y; public static void
Super(int x) sub (int x,int y) main(String ab[])
{ { {
this.x=x;
super(x); sub s1=new sub(10,20);
}
this.y=y; s1.display();
void display()
{ }
}
System.out.println( " Super void display( )
}
class x=" +x); {
} System.out.println(" super x = "
} +x);
System.out.println(" super y =
" +y );
}}
Final Keyword
Output
Abstract Classes
• When the keyword abstract appears in a class definition, it means that zero or
more of its methods are abstract.
• An abstract method has no body.
• Some of the subclass has to override it and provide the implementation.
• Objects cannot be created out of abstract class.
• Abstract classes basically provide a guideline for the properties and methods
of an object.
• In order to use abstract classes, they have to be subclassed.
• There are situations in which you want to define a superclass that declares the
structure of a given abstraction without providing a complete implementation
of every method.
Abstract Classes : Program
Interface
interface Printable{
void print(); }
interface Showable{
void show(); }
class A7 implements Printable,Showable {
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
public static void main(String args[]){
A7 obj = new A7();
obj.print(); obj.show(); } }
Interface inheritance
A class implements an interface, but one
interface extends another interface. class TestInterface4 implements Showable{
Example: public void print()
{System.out.println("Hello");}
interface Printable{ public void show()
void print(); {System.out.println("Welcome");}
} public static void main(String args[]){
interface Showable extends Printable{ TestInterface4 obj = new TestInterface4();
void show(); obj.print();
} obj.show();
}
}
Default Method in Interface
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
Static Method in Interface
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Interface : Lab program
interface area
class circle implements area
{
{public float compute(float x,float y)
final static float pi=3.14f;
{ return(pi*x*x); } }
float compute(float x,float y);
class testinterface
}
{ public static void main(String ab[])
class rectangle implements area
{ rectangle r1=new rectangle();
{
circle c1=new circle();
public float compute(float x,float y)
area a1; a1=r1;
{
System.out.println(“Rectangle Area="+a1.compute(10,10))
return(x*y);
a1=c1;
}
System.out.println("Area of circle= "+a1.compute(10,5));
}
Nested Interface
• An interface can have another interface which is known as a nested interface
Syntax
interface printable
{ void print();
interface MessagePrintable{
void msg();
}
}
Java Input Statement
Java provides different ways to get input from the user.
To get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to
import java.util.Scanner package.
Syntax:
// create an object of Scanner
Scanner input = new Scanner(System.in);
// take input from the user
int number = input.nextInt();
Java Input Statement
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);
// closing the scanner object
input.close();
}
Java Input Statement
// Getting double input
import java.util.Scanner; System.out.print("Enter double: ");
class Input { double myDouble = input.nextDouble();
public static void main(String[] args) { System.out.println("Double entered = " +
myDouble);
Scanner input = new Scanner(System.in);
// Getting String input
// Getting float input System.out.print("Enter text: ");
System.out.print("Enter float: "); String myString = input.next();
float myFloat = input.nextFloat(); System.out.println("Text entered = " + myString);
System.out.println("Float entered = " + }
myFloat); }
•
Getting input from keyboard : Output