Chapter 3
Language Fundamentals ll
Objective
In this chapter learner able to understand:
• Lifetime of Variables
• Instance methods and class methods
• Understanding Polymorphism
• Understanding Inheritance
• Using “extends” keywords
• Abstract method & Abstract class
Static Members
Class members prefixed with non-access modifier "static", are
called static.
Static members are not particular to any object.
Most often, these represent public resources (data) like DB
connection or services (methods) common to all objects of the
class.
They are accessed with just the class name.
For example:-
class Math{
public static final float PI = 3.14F;
}
To access PI, we don’t need to instantiate Math. We’ll just use the
following:-
[Link]("Value of PI = "+ [Link]);
Static Members contd.
A class can have following type of static members:-
Static data fields (either variable or constants)
Static methods
Static nested classes (will be discussed later)
Static nested interfaces (will be discussed later)
Static Methods
class DBService{
public static ResultSet getTable(String tableName){
//your code goes here
}
}
How to access/invoke:-
[Link]("Users");
Static Members contd.
Points to remember:-
• A static method can access other static members (data or methods) in the same
class directly.
• A static method can’t access non-static members of the class directly. An object is
required.
• In a static method, we can’t use this or super.
Static Initializer Block
• Static block of code is used to initialize the static fields with some dynamic values.
• That’s why also called “static initializer block”.
Example:-
class DBService{
public static Connection dbConnection;
static{
// code to initialize dbConnection
}
}
• Static blocks automatically execute right after allocation of static data members
when the class is loaded.
Non-static Members
• Non-static members are particular to an instance of the class. So, also called as
instance members.
• A class can have following instance members:-
– Instance data fields (either variable or constant)
– Instance methods
– Inner classes (will be discussed later)
– Non-static nested interfaces (will be discussed later)
Instance fields & methods: An Example
class Monitor{
private String modelNo;
public void setModelNo(String modelNo){
// your code goes here
}
public String getModelNo(){
// your code goes here
}
}
In this example:-
Field modelNo is an instance data field(non-static data field).
Methods setModelNo(String) & getModelNo() are instance methods.
Non-Static Members contd.
• A non-static method can access other static & non-static methods in the same
class directly.
• A non-static method is implicitly provided 2 references:
– Reference to the current object (this)
– Reference to the super object (super)
Non-Static Block of Code
• Non-static block of code provided in the class execute every time an object of the
class is constructed.
• It automatically executes right after the allocation of non-static data fields and
just before the invocation of constructor.
• It can be used to initialize the non-static data fields with some dynamically
calculated values.
Constructors
Constructor is the section of code that executes after allocation of an object.
Its sole purpose is to initialize the fields of the object with some dynamically calculated
value.
Constructors follow following convention:-
Its name is same as of its class.
It doesn’t have a return type provision.
Example:-
class Test{
Test(){
[Link]("It's a constructor");
}
}
When we should use non-static block to initialize the fields of
the object & when constructor?
Polymorphism
Polymorphism means an ability to access one or more behaviors (methods) by
using a single interface (name).
It is of 2 types:-
Compile-time Polymorphism
Run-time Polymorphism
Method & constructor overloading is used to achieve compile-time polymorphism.
Method overriding is used to achieve run-time polymorphism.
Method Overloading
• Method overloading means defining multiple methods with the same name but
having different Parameter List.
• Parameter List can be said “different” if
– No. of parameters is different.
– Type of parameters is different.
– Both are different.
• Return type doesn’t play any role in method overloading.
Example:-
public void print(String msg){
// code to print msg on stdout
}
public void print(String msg, OutputStream out){
// code to print msg on provided output stream
}
Constructor Overloading: Example
class User{
private String name,role;
User(String name){
[Link] = name;
role = "user";
}
User(String name,String role){
[Link]=name;
[Link]=role;
}
}
----------------------
User user1 = new User("john");
User user2 = new User("brad","DBA");
Inheritance
• One of the key concepts of OOP.
• Establishes a hierarchical relationship among classes.
• Establishes a superclass/subclass relationship.
• Establishes “is a” relationships.
Benefits:
• Reusability of code.
• Put code in one class, use it in all the subclasses.
• Write general purpose code designed for a supertype that works for all subtypes.
Implementing inheritance in JAVA
Inheritance is implemented in Java using extends keyword.
class A{
// variables & methods B
}
class B extends A{
// variable & methods
}
A superclass defines a general set of functionality, whereas subclasses define
functionalities specific to them.
In other sense, inheritance in Java builds a hierarchy of classes & thus supports the
concept of classification.
Inheritance contd…
Private members of the superclass are not inherited by the subclass and can only
be indirectly accessed.
constructors and initializer blocks are not inherited by a subclass.
When you construct an object, the default base class constructor is called
implicitly, before the body of the derived class constructor is executed. So, objects
are constructed top-down under inheritance.
Inheritance contd.
class A{
A(){
[Link]("Default constructor of class A");}
A(int val){
[Link](“Parameterized constructor of class A");}
}
class B extends A{
B(){
[Link]("Default constructor of class B");}
B(int val){
[Link](“Parameterized constructor of class B");}
}
Predict the output.
B ref1 = new B();
B ref2 = new B(5);
Inheritance contd…
class A{
A(){
[Link]("Default constructor of class A");}
A(int val){
[Link]("Parameterized constructor of class A");}
}
class B extends A{
B(){
[Link]("Default constructor of class B");}
B(int val){
super(4);
[Link]("Parameterized constructor of class B");}
}
Predict the output.
B ref1 = new B();
B ref2 = new B(5);
Method Overriding
• When a method in a subclass has the same name and type signature as a method
in its superclass, then the method in the subclass is said to override the method
of the superclass.
• Only non-static methods can be overridden.
• Both signature and return type must be the same as the superclass.
• The throws clause of an overriding method can have fewer types listed than the
method in the superclass, or more specific types or both.
Method Overriding: Example
public class Parent {
public void hello() {
[Link]("Hello from parent");
}
}
public class Child extends Parent {
public void hello() {
[Link]("Hello from Child");
}
}
Method Overriding contd…
• A subclass class can change the access specifier of the methods of the superclass,
but only if it provides more access.
• A method declared protected in the super class can be redeclared protected or
public, but not private.
• Fields cannot be overridden; they can only be hidden.
• To access the hidden fields use the super keyword.
final & Inheritance
• A method declared final cannot be overridden.
• A class can also be declared final. Such a class cannot be extended.
final class Security {
//
}
• A final class’s methods are implicitly final.
• static and private methods cannot be overridden.
Abstract Class
• A superclass that only defines a generalized form that will be shared by all its
subclasses, leaving the implementation details to its subclasses is said to an
abstract class.
• A concrete class has concrete methods, including implementations of any abstract
methods inherited from its superclasses.
• Any class with abstract methods should be declared abstract.
Abstract class: Example
abstract class Shape {
abstract double area();
public void display () { // concrete method
// Do something
}
}
class Square extends Shape {
double area() {
// Do something
}
Abstract Class contd…
An abstract class cannot have objects because it is not complete, but it can have
references.
Static methods and constructors cannot be declared abstract.
Any subclass of an abstract class must either implement all the abstract methods
in the superclass or be itself declared abstract.
A concrete method can be overriden to become abstract.
It is illegal to declare a class both final and abstract.
Chapter Summary
In this chapter, you have learned:
• Lifetime of Variables
• Instance methods and class methods
• Understanding Polymorphism
• Understanding Inheritance
• Using “extends” keywords
• Abstract method & Abstract class
Thank
you