0% found this document useful (0 votes)
7 views18 pages

Lecture 3

This document provides an overview of key Java concepts including command-line arguments, static members, final keywords, strings, arrays, inheritance, and method overriding. It explains how to use static methods and fields, the differences between final, finally, and finalize, and the structure of arrays and arrays of objects. Additionally, it discusses the importance of inheritance for code reusability and polymorphism in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Lecture 3

This document provides an overview of key Java concepts including command-line arguments, static members, final keywords, strings, arrays, inheritance, and method overriding. It explains how to use static methods and fields, the differences between final, finally, and finalize, and the structure of arrays and arrays of objects. Additionally, it discusses the importance of inheritance for code reusability and polymorphism in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 3

- By Prof. Shivani Supe


Command Line Arguments in Java
Java command-line argument is an argument i.e. passed at the time of running the Java
program. In Java, the command line arguments passed from the console can be received in
the Java program and they can be used as input. The users can pass the arguments during
the execution bypassing the command-line arguments inside the main() method.
// Java Program to Illustrate First Argument
class GFG {
public static void main(String[] args) {
// Printing the first argument
System.out.println(args[0]);
}
}
Static Data Members
In Java, static members are those which belongs to the class and you can access these members
without instantiating the class.
The static keyword can be used with methods, fields, classes (inner/nested), blocks.
Static Methods − You can create a static method by using the keyword static. Static methods
can access only static fields, methods. To access static methods there is no need to instantiate
the class, you can do it just using the class name as
public class MyClass {
public static void sample(){
System.out.println("Hello"); }
public static void main(String args[]){
MyClass.sample();
}}
Static Fields − You can create a static field by using the keyword static. The static fields have the
same value in all the instances of the class. These are created and initialized when the class is loaded
for the first time. Just like static methods you can access static fields using the class name (without
instantiation).
public class MyClass {
public static int data = 20;
public static void main(String args[]){
System.out.println(MyClass.data);
}}
Static Blocks − These are a block of codes with a static keyword. In general, these are used to
initialize the static members. JVM executes static blocks before the main method at the time of class
loading.
public class MyClass {
static{
System.out.println("Hello this is a static block"); }
public static void main(String args[]){
System.out.println("This is main method"); }}
Static vs. Public
You will often see Java programs that have either static or public attributes and methods.

In the example below, we created a static method, which means that it can be accessed without creating an object
of the class, unlike public, which can only be accessed by objects:

public class Main { // Static method


static void myStaticMethod() { System.out.println("Static methods can be called without creating objects"); }
// Public method
public void myPublicMethod() { System.out.println("Public methods must be called by creating objects"); }
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method on the object }}
Java final, finally and finalize
In Java, the final, finally, and finalize keywords play an important role in exception
handling. The main difference between final, finally, and finalize is,

● final: The “final” is the keyword that can be used for immutability and restrictions
in variables, methods, and classes.
● finally: The “finally block” is used in exception handling to ensure that a certain
piece of code is always executed whether an exception occurs or not.
● finalize: finalize is a method of the object class, used for cleanup before garbage
collection.
Strings
In Java, String is the type of objects that can store the sequence of characters enclosed by
double quotes and every character is stored in 16 bits.

Java provides a robust and flexible API for handling strings, allowing for various
operations such as concatenation, comparison, and manipulation.

A String Array in Java is an array that stores string values. The string is nothing but an
object representing a sequence of char values.

Declaration of String Arrays

String[] str0; // declaration without size

String[] str1 = new String[4]; //declaration with size

Initialization of String Arrays


String[] arr0 = new String[]{“Apple”, “Banana”, “Orange”};
Arrays
Arrays are fundamental structures in Java that allow us to store multiple values of the same type in
a single variable. They are useful for storing and managing collections of data.
public class Main {
public static void main(String[] args)
{ // initializing array
int[] arr = { 1, 2, 3, 4, 5 };
// size of array
int n = arr.length;
// traversing array
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}}
Array of Objects
Arrays in Java provide a way to store multiple elements of the same data type under a single variable
name. When it comes to arrays of objects, Java allows us to create arrays where each element is an
object of a particular class. This concept enables the storage and manipulation of multiple instances of
a class within a structured collection. Arrays of objects in Java help organize lots of similar things
together.

What is an Array of Objects?


Arrays of objects in Java are collections that can store multiple instances of a class. They follow a
similar structure to arrays of primitive data types but instead hold references to objects. Defining an
array of objects involves declaring the array variable and then allocating memory for the objects.

Declaring Arrays of Objects


To declare an array of objects, the syntax involves specifying the class type followed by square
brackets []:

MyClass[] objectArray = new MyClass[5];


Initialization and Memory Allocation of Array of Objects
Initializing an array of objects involves creating individual objects and assigning them to
each element of the array:
objectArray[0] = new MyClass();

objectArray[1] = new MyClass();

// ... (initializing other elements)


Accessing Elements in Arrays of Objects
Accessing elements in arrays of objects is similar to accessing elements in regular arrays.
Each element can be accessed by its index.
MyClass obj = objectArray[0];

// Use obj to perform operations or access properties/methods of MyClass


class Student { // array of Student objects
int id; Student[] s = {
String n; new Student(1, "Ram"),
// Constructor to initialize student object new Student(2, "Shyam")
Student(int id, String n) { };
this.id = id;

this.n = n; } // Displaying student details


// Method to display student details for (Student s1 : s) {
void display() { s1.display();
System.out.println("ID: " + id + ", Name: " + n); }
}} }
public class Main { }
public static void main(String[] args) {

// Creating and initializing an


Inheritance
In Java programming, the inheritance is an important of concept of Java OOPs.
Inheritance is a process where one class acquires the properties (methods and attributes) of
another. With the use of inheritance, the information is made manageable in a hierarchical
order.
The new class that is created is known as subclass (child or derived class) and the existing
class from where the child class is derived is known as superclass (parent or base class).
The extends keyword is used to perform inheritance in Java.
class Animal {
// methods and fields
}// use of extends keyword to perform inheritance
class Dog extends Animal {
// methods and fields of Animal, methods and fields of Dog }
class Animal { class Main {
// field and method of the parent class public static void main(String[] args) {
String name; // create an object of the subclass
public void eat() { Dog labrador = new Dog();
System.out.println("I can eat"); // access field of superclass
} labrador.name = "Rohu";
} labrador.display();
// inherit from Animal

class Dog extends Animal { // call method of superclass


// new method in subclass // using object of subclass
public void display() { labrador.eat();
System.out.println("My name is " + name); }
} }
}
Types of Inheritance
There are five types of inheritance.
Method Overriding
we see the object of the subclass can accessin Javaof theInheritance
the method superclass.

However, if the same method is present in both the superclass and subclass, what will happen?

In this case, the method in the subclass overrides the method in the superclass. This concept is known as method
overriding in Java.

class Animal { public void eat() { class Main {

// method in the superclass System.out.println("I eat dog public static void main(String[]
food"); args) {// create an object of the
public void eat() { subclass
}
System.out.println("I can eat"); Dog labrador = new Dog();
// new method in subclass
}} // call the eat() method
public void bark() {
// Dog inherits Animal labrador.eat();
System.out.println("I can bark");
class Dog extends Animal { labrador.bark();
}}
// overriding the eat() method }

}
super Keyword in Java Inheritance
we saw that the same method in the subclass overrides the method in superclass.

In such a situation, the super keyword is used to call the method of the parent class from the method of the child
class.

We can also use the super keyword to call the constructor of the superclass from the constructor of the subclass.

// Base class vehicle System.out.println("Maximum


Speed: "+ super.maxSpeed);
class Vehicle {
}}
int maxSpeed = 120;
// Driver Program
} // sub class Car extending vehicle
class Test {
class Car extends Vehicle {
public static void
int maxSpeed = 180; main(String[] args)
void display() { Car small = new
Car();
{ // print maxSpeed of base class (vehicle)

small.display();
protected Members in Inheritance
In Java, if a class includes protected fields and methods, then these fields and methods are accessible from the
subclass of the class.
class Main {
class Animal {
public static void main(String[] args) {
protected String name;
// create an object of the subclass
protected void display() {
Dog labrador = new Dog();
System.out.println("I am an animal.");
// access protected field and method
}
// using the object of subclass
}
labrador.name = "Rocky";
class Dog extends Animal {
labrador.display();
public void getInfo() {
labrador.getInfo();
System.out.println("My name is " + name);
}
}}
}
● The most important use of inheritance in Java is code reusability. The code that is
present in the parent class can be directly used by the child class.

● Method overriding is also known as runtime polymorphism. Hence, we can achieve


Polymorphism in Java with the help of inheritance.

You might also like