0% found this document useful (0 votes)
12 views6 pages

Understanding Object-Oriented Programming Concepts

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, encapsulation, polymorphism, inheritance, and data abstraction. It explains key components such as constructors, setters and getters, as well as method overloading and overriding. Additionally, it discusses the limitations of multiple inheritance in Java and the use of abstract classes and interfaces to achieve similar functionality.

Uploaded by

nitinsingh3264
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)
12 views6 pages

Understanding Object-Oriented Programming Concepts

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including classes, objects, encapsulation, polymorphism, inheritance, and data abstraction. It explains key components such as constructors, setters and getters, as well as method overloading and overriding. Additionally, it discusses the limitations of multiple inheritance in Java and the use of abstract classes and interfaces to achieve similar functionality.

Uploaded by

nitinsingh3264
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

OOPS - Java

Wednesday, 21 May 2025 16:56

Object Oriented Programing -> -> process - classes and object ->
POP - Procedure Oriented Programming -> specific task by task chl rhe the

OOPS -> object oriented Programming -> class and Object ->

Classes Objects
Class is a logical entity Real world entity
Class consist of attributes Object is a provide or a template of class
and behaviors Blue print of class
Which given by the objects
Class consists mainly Object just created for a the class so the new new
user can come and use the functionality
Properties ---- Data
members
Method ----- Functions
Student { Object -> dubara apni value provide
Roll Class ko
Name Class k pass template uske details
} Object uss class ka object baanne ke baad woh puri
functionality use kr saktha hia

Setters and Getters ->

Setter -> to set the value

Constructor -> special method -> automatically called -> at the time of object creation ->

Rules ->
1. Constructor name should be same name as class name name
2. Constructor can not return any value
3. Constructor automatically called at the time of memory allocation
4. Types =>

Pillars -> Data members


Encapsulation
polymorphism
Inheritance
Data Abstraction
Function

Class Student

Encapsulation ->

Wrapping up of data/properties with method/function - together into a single


class/unit

Class Student{

String name;
Int marks ;

String ->

Getname()
Class Student{

String name;
Int marks ;

String ->

Getname()

Getavg(){. // calculation
-----------
}

Polymorphism ->

When a method in a class has work more than one form ->

Example ->

Dhoni -> in a single match


1. WK ->
2. Bowling ->
3. Batting ->

Polymorhpism divided into 2 cateogires


1. Compile time
2. Run time

i. Compiler -> Method overloading


ii. Runtime -> Method Overriding

Method overloading -> in which we have multiple method name , with same name but diff
parameters

Class Cal{

Int sum(int a , int b){


Return a+b;
}

Int sum(int a , int b , int c){


Return a+b+c;
}

Float sum(float a , float b){


Return a+b;
}

Method Overriding ->


When child class has same method name with parents class and also that child
extending parents , so child class property will get the first priority so that that
method override

When u have same method but diff -> and also extend

Class Parent{
Void hello(){
SYSO("HELLO Parents ");
}

Class Child extends Parent{

Void hello(){
Syso("hello child");
}
}

Class Child extends Parent{

Void hello(){
Syso("hello child");
}

Child c = new Child();


[Link]() -> // Hello parents -> after adding that same method in child so that
// the answer will be of child class

Inheritance ->

Parent -> base

Child -> sub class -> derived class

When I have one class with 5 property and I want that same property in another
class
But I didn’t want to write again that property -> so in this case we inheritance

Inheritance I extends the second class with the first class which have the 5
property , on the spot that property will be inherit by the second one ,

So that without writing again the method we can utilise that functionality

Reduce redubancy ->


1 single extend -> multiple method utilise
Single
Multi
Hierarchal
Class Animal{
Hybrid
Void speak(){ Multiple Inheritance ->
Cout<<"Hello"; never work in java directly
}

Void eat(){
Cout<<"eating";
}

Class Dog extends Animals{ // By making Dog object ,dog can implement all the
property of animal ->
Animal

Void speak(){
Cout<<"barking";
Multi level ->
Herirical
Void Breed(){
Cout<<"german"; Animal
Dog }

Level -> single level

Multi level

Dog extends Animal


BabyDog Cat extend Animal

Hybrid
Hybrid

Single level A
Multi level B
Herarichal Multiple
Hybrid
Multiple ->
Is not allowed in java
language ???
C
C extends A,B{

Constructor ->
1. Special method -> automatically call -> object creation ->
2. Iska name same hota hai class k naam se
3. No return -> memory allocated at the time of object creation

Class Student{

//data members
//Student s = new Student();
String name;
Int age;
Student s1= new Student(10);
[Link]
[Link]
// getters and setters

Student(){
Cout<<"Constructor is called";
}

Student(int age){
Cout<<"your age is " , age;
}

Constructor ->
1. Paramteretized
2. Non Parameterized ->

Data Abstraction ->


Hiding the important detail => and providing you the functionality to the user ->

Method -> aapka kaam krdete hai

Method ko call krna hia

Jaise -> car ->


Engine start
Acc
Gear ->
Cout<<"Car move";
Jaise -> car ->
Engine start
Acc
Gear ->
Cout<<"Car move";

Example ->

Person 1
Person 2
SMS
SMS

Person 1 send the message


Person 2 receive a message

Bank Account ->

Account ->

Cash Withrawal -> 1000 ->

Cust account debit -> 1000


Bank Account credit -> 1000

Cash in hand ->

Code ->

Abstract Class Interface


Cannot abstract methods Specific set of class is implemented , method , abstract are
Cannot be instanized , without both allowed
implementation

Class can inherit from on abstract class Class can implemented from multiple interface ( Multiple
Inheritance )
Method -> public , private dono chalthe Method implictly public
hai
Can have member - final , non final , Implicity public -> static and final
static, static

Final -> Reassigned , re - initialized


Static -> we can reassign

Final -> Cannot be overridden by subclass


Static -> static member ko hi use krenge but overriden

Final -> first to last remain no change


Static local variable -> java allowed -> C / C++

Multiple Inheritance -> java do not allows to extend more than one class

Dog
BabyDog
Static local variable -> java allowed -> C / C++

Multiple Inheritance -> java do not allows to extend more than one class

Dog
BabyDog

Animal

But we can use Multiple Inheritance -> Data Abstraction ->


Abstract classes
Interfaces

Common questions

Powered by AI

Inheritance in Java can create hierarchical and multilevel class structures. In a hierarchical inheritance, multiple classes inherit from a single parent class, while in multilevel inheritance, a class serves as a parent for another class, which, in turn, becomes a parent for a third class. For example, consider a class structure where 'Animal' is a parent class that has basic characteristics. A 'Dog' class can extend 'Animal', inheriting these basic features while adding additional functionalities like barking . Further, a class 'BabyDog' can extend 'Dog', inheriting features from both 'Animal' and 'Dog', thus creating a multilevel hierarchy . This structuring enables the reuse of shared methods and properties across different levels.

Inheritance in Object-Oriented Programming allows a class, called a subclass or derived class, to inherit properties and methods from another class, known as the parent or base class. This relationship enables code reuse, as it eradicates the need to write the same code multiple times across different classes . It effectively reduces redundancy, making code easier to maintain and extend . By leveraging inheritance, developers can create a hierarchical class structure that logically organizes code and promotes efficiency, although at the cost of increased coupling between classes .

Abstract classes and interfaces in Java serve different purposes, especially concerning inheritance. An abstract class can contain abstract (incomplete) and concrete (complete) methods, allowing for shared code across related classes and cannot be instantiated itself; it is used to define a common base class with shared implementation . In contrast, an interface can only declare methods, which must be implemented by any class that implements the interface. Java allows for a class to implement multiple interfaces, supporting multiple inheritance per behavior, whereas a class can extend only one abstract class . These differences influence the class hierarchy and design choices in Java applications.

Constructors in Java are special methods used to initialize objects. They are called automatically when an object is created. Java constructors have specific constraints: they must have the same name as the class, cannot return a value, and are invoked when memory is allocated for the object . Constructors facilitate object creation by initializing object fields and providing an opportunity to set initial states or call functions that are necessary at the time of object creation. This process ensures that each object starts in a consistent and valid state .

Static members in Java are unique in that they are shared amongst all instances of a class, as opposed to instance members which are unique to each object. A static member belongs to the class itself and can be accessed without creating an instance of the class. Static methods can only directly access other static data members and methods . Another characteristic is that static members are initialized only once at the start of the program's execution. This facilitates utility functionality common across all objects, such as mathematical operations in the Math class. It's important to note that static methods cannot be overridden in the context of inheritance, and static local variables are not allowed, maintaining consistency and limiting scope issues .

Method overloading and method overriding are two distinct concepts in Java. Method overloading occurs when two or more methods in the same class share the same name but have different parameter lists, allowing for differentiation based on input parameters . It's a form of compile-time polymorphism. In contrast, method overriding happens when a subclass has a method with the same name, return type, and parameters as in its superclass. Here, the subclass's method takes precedence, allowing for dynamic (runtime) polymorphism . This provides the subclass the capability to define specific behavior for the overridden methods while maintaining a consistent interface.

Polymorphism in Object-Oriented Programming (OOP) enhances functionality by allowing methods to perform different tasks based on the object invoking the method or the parameters passed to the method. It provides flexibility and the ability to redefine methods for different channels of execution. The two main types of polymorphism are compile-time (or static polymorphism) and run-time (or dynamic polymorphism). Compile-time polymorphism includes method overloading, where multiple methods have the same name but different parameters. Run-time polymorphism involves method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass .

Multiple inheritance is not directly supported in Java due to the diamond problem, where a class could inherit from two classes with the same method, creating ambiguity over which method to inherit. Java addresses this limitation by allowing multiple inheritance through interfaces, as a class can implement multiple interfaces . This model supports method declarations from multiple sources, thereby emulating multiple inheritance without the associated complication of ambiguity or excessive complexity that direct multiple inheritance could cause . This approach ensures a clear and manageable class hierarchy while improving code maintenance.

Data abstraction in Object-Oriented Programming is achieved by hiding implementation details and exposing only essential features. This is commonly implemented using abstract classes and interfaces, which define methods that must be implemented by subclasses or implementing classes without revealing the actual method implementations . Abstract classes provide a way to include both complete and incomplete functions, promoting flexibility while ensuring essential methods are implemented. Data abstraction is significant because it simplifies complex systems by controlling the details accessible to users and other parts of the program, thus enhancing modularity and reducing complexity .

Encapsulation in Object-Oriented Programming integrates data and functions by bundling them into a single unit, such as a class. This ensures that the internal representation of an object is hidden from the outside, with access restricted through well-defined interfaces provided by methods such as getters and setters . Encapsulation is important as it enhances data integrity and security, preventing unauthorized access and modification. By creating private fields and exposing only the necessary parts of an object through public methods, encapsulation promotes modularity and maintains the integrity of the data within an object .

You might also like