Você está na página 1de 21

JAVA

Basics
OOPs: Inheritance &
Polymorphism, Packages
Session 4

LEARN. DO. EARN


Agenda
Sl. No. Agenda Topics Sl. No. Agenda Topics
1 Inheritance 8 Final Keyword in Java
2 Inheritance and Inheritance 9 Final Variable with Example
Hierarchy
10 Final Methods and Classes
3 Types of Inheritance
4 Super Keyword in Inheritance
11 Command-Line Arguments

5 Polymorphism 12 Packages

6 Method Overloading & Overriding 13 Access Specifiers


7 Overriding Vs. Overloading 14 Short Assignments

3
Inheritance
Inheritance is the process where one object acquires the properties of another and
whereby each subclass inherits attributes and methods of its super-class
The subclass can have additional specific attributes and methods
Classification helps in handling complexity
The extends keyword is used to achieve inheritance.

class Bicycle{
}
class MountainBike extends Bicycle{
}
class RoadBike extends Bicycle{
}
class TandemBike extends Bicycle{
}

4
Inheritance and Inheritance Hierarchy
IS-A Relationship: Apple is a Fruit, Car is a
Vehicle etc. Inheritance is uni-directional. For
example House is a Building. But Building is
not a House. When extends or implements
keyword in a class declaration is given, then
this class is said to have IS-A relationship.

HAS-A Relationship: Composition (HAS-A)


simply means use of instance variables that
are references to other objects. For example:
Maruti has Engine or House has Bathroom

5
Types of Inheritance

6
Super Keyword in Inheritance
super is used to refer immediate parent class instance variable.

Syntax: super.variableName;

super() is used to invoke immediate parent class constructor.

Syntax: super(Arguments List);

super is used to invoke immediate parent class method.

Syntax: super.methodName(Arguments List);

7
Polymorphism
The Greek word polymorphism means a
state of having many shapes or the capacity to
take on different forms.

Polymorphism is the capability of an action or


method to do different things based on the
object that it is acting upon.

Polymorphism in Java has two types: Compile


time polymorphism (static binding-
Overloading ) and Run time polymorphism
(dynamic binding-overriding)

8
Method Overloading & Overriding
Overloading

Reusing the same name for a method


Arguments (& possibly return type) should be different
The method calls are resolved at compile time using the method signature
Compile time error occurs if compiler cant match the args or if more than one match is
possible

Overriding

method must have same name as in the parent class


method must have same parameter as in the parent class.
must be IS-A relationship (inheritance).

9
Overriding Vs. Overloading
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
A a = new A(); A a = new A();
a.p(10); a.p(10);
a.p(10.0); a.p(10.0);
} }
} }

class B { class B {
public void p(double i) { public void p(double i) {
System.out.println(i * 2); System.out.println(i * 2);
} }
} }

class A extends B { class A extends B {


// This method overrides the method in B // This method overloads the method in B
public void p(double i) { public void p(int i) {
System.out.println(i); System.out.println(i);
} }
} }

10
Final Keyword in Java
Final keyword in Java is used to restrict the user.
Final keyword can be used in 3 contexts :
Variable
Method
Class

11
Final Variable with Example
Final variable is a constant in Java. Once you create and initialize then you cannot
change its value.
The final method can't override in a subclass.
Syntax:

12
Final Methods and Classes
A final method cannot be overridden in a sub class
private methods are implicitly final.
A class declared as final cannot be sub classed
Every method of a final class is by default final

Note: A visible advantage of declaring a java variable as static final is thatm the
compiled java class results in faster performance.

13
Command-Line Arguments
Command line arguments are parameters that are supplied to the application program at
the time of invoking it for execution.
Whatever arguments are passed as command line arguments while executing a Java
program are placed in the String [] array parameter of the main method.
The length field of an array always has a non-negative value.
When there are no arguments, the String array is an empty array
If you pass a single argument, it is placed at the array index 0
So, args[0] corresponds to this argument.
Thus, the number of arguments becomes the length of the array and the arguments are
accessed in the program in the usual way of accessing elements of an array.
The syntax to pass arguments from command line is as follows:
java <classname> <argument1> <argument2> <argument3> ........

14
Packages
Package
A way to organize files in java, it is used when a project consists of multiple modules.
It helps to resolve naming conflicts.
Package's access level allows you to protect data from being used by the non-authorized classes.

Some of the existing packages in Java are:


java.lang - bundles the fundamental classes
java.io - classes for input , output functions are bundled in this package

The package keyword is used to create a package in Java.


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

15
Packages (Contd.)
Note:
1. The package statement should be the first line in the source file.
2. There can be only one package statement in each source file, and it applies to all types
in the file.
3. There are three ways to access the package from outside

import package.*; // import mypack.*;


import package.classname; // import mypack.Hello;
fully qualified name // mypack.Hello obj = new mypack.Hello();

16
Access Specifiers
Access specifier is also known as Visibility Specifiers which regulate access to classes,
fields and methods.
Access specifiers determine whether a field or method in a class, can be used or
invoked by another method in another class or sub-class.
Access specifiers can be used to restrict access.

17
Short Assignments
1. You have the following code in a file called Test.java
class Base{
public static void main(String[] args){
System.out.println("Hello");
} }
public class Test extends Base{}

What will happen if you try to compile and run this?


a. It will fail to compile.
b. Runtime error
c. Compiles and runs with no output.
d. Compiles and runs printing "Hello

2. Name the access modifier which when used with a method, makes it available to all
the classes in the same package and to all the subclasses of the class.

18
Short Assignments (Contd.)
3. What is the result of attempting to compile and run this?
class Base{
String s = "Base";
String show() {
return s;
} } class Derived extends Base{
String s = "Derived";
} public class Test {
void print(Base b){
System.out.println(b.show());
} void print(Derived d){
System.out.println(d.show());
} public static void main(String[] args){
Test t = new Test();
Base b = new Derived();
t.print(b); }}
a. Code will not compile
b. Run time error
c. Will compile and run printing "Derived
d. Will compile and run printing "Base"

19
Short Assignments (Contd.)
4. Which of the following are correct. Select the one correct answer.
a. An import statement, if defined, must always be the first non-comment statement of
the file.
b. private members are accessible to all classes in the same package.
c. An abstract class can be declared as final.
d. Local variables cannot be declared as static

5. If a base class has a method defined as void method() { }

Which of the following are legal prototypes in a derived class of this class. Select the two
correct answers.
a. void method() { }
b. int method() { return 0;}
c. void method (int i) { }
d. private void method() { }

20
THANK YOU
Email us at - support@acadgild.com

LEARN. DO. EARN

Você também pode gostar