0% found this document useful (0 votes)
29 views59 pages

Object-Oriented Programming Syllabus

The document outlines the syllabus for an Object-Oriented Programming course, covering key concepts such as polymorphism, inheritance, method overloading, and recursion. It explains the differences between compile-time and run-time polymorphism, and details the use of static members, final variables, and inner classes in Java. Additionally, it discusses the advantages of inheritance, member access, and method overriding, providing examples and explanations of how these concepts are implemented in Java.

Uploaded by

sherinsample01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views59 pages

Object-Oriented Programming Syllabus

The document outlines the syllabus for an Object-Oriented Programming course, covering key concepts such as polymorphism, inheritance, method overloading, and recursion. It explains the differences between compile-time and run-time polymorphism, and details the use of static members, final variables, and inner classes in Java. Additionally, it discusses the advantages of inheritance, member access, and method overriding, providing examples and explanations of how these concepts are implemented in Java.

Uploaded by

sherinsample01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

S3-PBCST304

OBJECT ORIENTED PROGRAMMING


(2024 Scheme)

Reenu Renjith
Assistant Professor CSE-MBITS
Module2-Syllabus
• Polymorphism :-
• Method Overloading, Using Objects as Parameters, Returning Objects,
Recursion. Static Members, Final Variables, Inner Classes.
• Inheritance –
• Super Class, Sub Class, Types of Inheritance, The super keyword, protected
Members, Calling Order of Constructors. Method Overriding, Dynamic
Method Dispatch, Using final with Inheritance.
Polymorphism
• Polymorphism literally means poly
(many) morphism (forms).
• polymorphism denotes that an
object may respond (behave) very
differently even when the same
operation is invoked on it
depending on the exact
polymorphic object to which the
call gets bound.
•Circle •Rectangle
• An operation may exhibit different perimeter= 2 π r Perimeter= 2w + 2l
behaviors in different instances. Area = π r2 Area= l × w
The behavior depends upon the
•Triangle
types of data used in the operation. Perimeter = a + b + c
Area = (1/2) × b × h
Polymorphism
• There are two main types of
polymorphisms:
• Compile time /Static polymorphism:
• also referred to as static binding.
• because the exact method to be
bound on a method call is
determined at compiled-time
(statically).

• Run time polymorphism/ Dynamic


method dispatch:
• also called dynamic binding.
• In dynamic binding, the exact
method that would to be bound
on a method call can only be
known at the run time
(dynamically).
Method Overloading
• Compile-time polymorphism is achieved by method overloading.
• In Java it is possible to define two or more methods within the same class that
share the same name, as long as their parameter declarations are different.
• When this is the case, the methods are said to be overloaded, and the process is
referred to as method overloading.
• Method overloading is one of Java’s most exciting and useful features.
• When an overloaded method is invoked, Java uses the type and number of
arguments as its guide to determine which version of the overloaded method to
actually call.
• When Java encounters a call to an overloaded method, it simply executes the
version of the method whose parameters match the arguments used in the call.
• Overloading Constructors
Using Objects as Parameters
• So far, we have only been using simple types as parameters to methods.
• However, it is both correct and common to pass objects to methods.
• One of the most common uses of object parameters involves
constructors.
• A constructor can take an object of its class as a parameter.
Returning Objects
• A method can return any type of data, including class types that you
create.
Recursion
• Java supports recursion.
• Recursion is the process of defining something in terms of itself.
• As it relates to Java programming, recursion is the attribute that
allows a method to call itself.
• A method that calls itself is said to be recursive.
Static Members
• To create a static member, precede its declaration with the
keyword static.
• When a member is declared static, it can be accessed before any
objects of its class are created, and without reference to any
object.
• You can declare both methods and variables to be static.

• Static main()
• The most common example of a static member is main( ).
• main( ) is declared as static because it must be called before
any objects exist.
• Instance variables declared as static are, essentially, global variables.
• When objects of its class are declared, no copy of a static variable is
made.
• Instead, all instances of the class share the same static variable.
• Methods declared as static have several restrictions:
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way.
// Java program to demonstrate static method // main method
public static void main(String[] args)
public class StaticMain {
{ StaticMain obj = new StaticMain();;
int n=10; //access to normal variables and methods through
object only.
// static variable [Link](obj.n);
static int a = 40; [Link]();

void simpleDisplay() // Calling static variables and method directly


{ [Link](a);
[Link](a); staticDisplay();
} }
}
// Declaration of a static method
static void staticDisplay() Output
{
[Link](a); 10
} 40
40
40
• Outside of the class in which they are defined, static methods and variables
can accesed by specifying the name of their class followed by the dot operator.
[Link]( )
[Link]()
class StaticMain
// Java program to demonstrate static method {
public class StaticClass // main method
{ public static void main(String[] args)
int n=10; {
// static variable StaticClass obj = new StaticClass();
static int a = 40; //access to normal variables and methods through
object only.
void simpleDisplay() [Link](obj.n);
{ [Link]();
[Link](a);
} // Calling static variables and method using classname
[Link](StaticClass.a);
// Declaration of a static method [Link]();
static void staticDisplay() }
{ }
[Link](a);
}
}
Final Variables
• The final keyword in java is used to restrict the user.
• The java final keyword can be used in many context.
• Final can be:
1. variable
[Link]
[Link]
1) Java final variable
• If you make any variable as final, you cannot change the value
of final variable(It will be constant).
2) Java final method
If you make any method as final, you cannot override it.
3) Java final class
If you make any class as final, you cannot extend it.
Nested classes and Inner Classes
• Writing a class within another class is allowed in Java.
• Nested classes are divided into two types −
• Non-static nested classes (Inner Classes) − non-static member
• Static nested classes − static member
// Outer class public class InnerClassDemo {
class OuterClass { public static void main(String[] args) {

void showOuter() { // Creating Outer class object


[Link]("inside outer class"); OuterClass out = new OuterClass();
}; [Link]();

// Non-static Inner Class // Inner Class object creation


class InnerClass { [Link] in = [Link] InnerClass();
void showInner() { [Link]();
[Link]("inside inner class");
} // Static inner Class object
} [Link] sn = new
[Link]();
// Static nested Class [Link]();
static class StaticNestedClass {
void showStaticNested() { }
[Link]("inside static nested class"); }
}
}
}
Inheritance
• The capability of a class to derive
properties and characteristics from
another class is called Inheritance.
• Inheritance is the process by which objects
of one class acquired the properties of
objects of another classes
• Super class/Base Class : The class whose
properties are inherited by subclass is
called Base Class or Super class.
• Sub class/Derived Class : The class that
inherits properties from another class is
called Subclass or Derived Class
Inheritance
Inheritance
Inheritance
• Inheritance is one of the cornerstones of object-oriented programming
because it allows the creation of hierarchical classifications.
• Using inheritance, you can create a general class that defines traits
common to a set of related items.
• A subclass is a specialized version of a superclass.
• It inherits all of the members defined by the superclass and adds its
own, unique elements.

• To inherit a class
• you simply incorporate the definition of one class into another by
using the extends keyword.
• The general form of a class declaration that inherits a superclass

class subclass-name extends superclass-name


{
// body of class
}
Advantages Of Inheritance in Java

• Code Reusability: Inheritance allows for code reuse and reduces


the amount of code that needs to be written. The subclass can
reuse the properties and methods of the superclass, reducing
duplication of code.
• Abstraction: Inheritance allows for the creation of abstract
classes that define a common interface for a group of related
classes. This promotes abstraction and encapsulation, making
the code easier to maintain and extend.
• Class Hierarchy: Inheritance allows for the creation of a class
hierarchy, which can be used to model real-world objects and
their relationships.
• Polymorphism: Inheritance allows for polymorphism, which is the
ability of an object to take on multiple forms. Subclasses can
override the methods of the superclass, which allows them to
change their behavior in different ways.
• Member Access and Inheritance

• A subclass
• can access public, protected and package members of
its superclass.
• cannot access private members of its the superclass.

• Access level of attributes and methods of a class.


The Keyword super
• There will be times when you will want to create a superclass that
keeps the details of its implementation to itself (that is, that keeps
its data members private).
• In this case, there would be no way for a subclass to directly
access or initialize these variables on its own.
• Java provides a solution to this problem.
• Whenever a subclass needs to refer to its immediate superclass, it
can do so by use of the keyword super.
• super has two general forms.
• The first form calls the superclass’ constructor.
• The second form is used to access a member of the
superclass that has been hidden by a member of a subclass.
1)Using super to Call Superclass Constructors
• A subclass can call a constructor defined by its superclass by use of the
following form of super:
super(arg-list);
• Here, arg-list specifies any arguments needed by the constructor in the
superclass.
• super( ) must always be the first statement executed inside a subclass’
constructor.
• To see how super( ) is used, consider this improved version of
the BoxWeight class:
2)Using super to access a member of the superclass

• The second form is used to access a member of the superclass


that has been hidden by a member of a subclass.
• This usage has the following general form:
super. member
• member can be either a method or an instance variable.
• This second form of super is most applicable to situations in which
member names of a subclass hide members by the same name
in the superclass.
Calling Order of Constructors
• In a class hierarchy, constructors are called in order of derivation,
from superclass to subclass.
• Further, since super( ) must be the first statement executed in a
subclass’ constructor, this order is the same whether or not super( ) is
used.
• If super( ) is not used, then the default or parameterless constructor
of each superclass will be executed.
Protected members in a class
• The protected keyword in Java refers to one of its access
modifiers.
• The methods or data members declared as protected can be
accessed by:
• Members of the same class
• Members of the derived class
• Protected members in a class
• If one wishes to access a protected modifier outside a
package, then inheritance is needed to be applied.
• Protecting a constructor prevents the users from creating the
instance of the class, outside the package.
• During overriding, when a variable or method is protected, it
can be overridden to other subclass using either a public or
protected modifier only.
• Outer class and interface cannot be protected.
Runtime Polymorphism or Dynamic method dispatch

• Dynamic method dispatch or run-time polymorphism is the mechanism through


which the correct version of an overridden method is called at runtime.
• It is a powerful feature because it allows for flexibility in the way we write code.
• We can write methods in the superclass that are common to all subclasses, and
then have specific behavior defined in each subclass.
• Java supports run-time polymorphism through Method overriding.
Method Overriding
• Overriding in Java occurs when a subclass or child
class implements a method that is already
defined in the superclass or base class.
• When a subclass provides its own version of a
method that is already defined in its superclass,
we call it method overriding.
• The subclass method must match the parent class
method's name, parameters, and return type.
• When an overridden method is called from within
its subclass, it will always refer to the version of
that method defined by the subclass.
• The version of the method defined by the
superclass will be hidden.
• If you wish to access the superclass version of an overridden
method, you can do so by using super.
Aspect Method Overloading Method Overriding
Define multiple methods in the same class Provide a specific implementation for a method
Definition with the same name but different already defined in a superclass in a subclass
parameters. with the same method signature.

Not related to inheritance; overloaded Closely related to inheritance; overridden


Inheritance methods are defined in the same class methods are defined in the superclass and can
and not inherited. be inherited by subclasses.

Can have the same or different return Must have the same return type or a subtype in
Return Type
types. the subclass.
Methods have the same name but
Methods have the same name, parameters, and
Method Signature different parameters (number, type, or
return type.
order).

Achieves compile-time polymorphism; the Achieves runtime polymorphism; the method to


Polymorphism correct method is selected at compile be executed is determined at runtime based on
time based on the method signature. the actual object type.
using final with Inheritance

• Using final to Prevent Overriding


• Using final to Prevent Inheritance

• Using final to Prevent Overriding


• While method overriding is one of Java’s most powerful
features, there will be times when you will want to prevent it
from occurring.
• To disallow a method from being overridden, specify final as a
modifier at the start of its declaration.
• Methods declared as final cannot be overridden.
• Using final to Prevent Inheritance
• Sometimes you will want to prevent a class from being
inherited.
• To do this, precede the class declaration with final.
• Declaring a class as final implicitly declares all of its methods
as final, too.
• As you might expect, it is illegal to declare a class as both
abstract and final since an abstract class is incomplete by
itself and relies upon its subclasses to provide complete
implementations.
Thankyou.

You might also like