KEMBAR78
Class X Notes | PDF | Inheritance (Object Oriented Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
19 views15 pages

Class X Notes

Java is a widely-used programming language created in 1995, owned by Oracle, and utilized for various applications including mobile, desktop, and web. Key concepts in Java include data types, operators, object-oriented programming principles like polymorphism, abstraction, inheritance, and encapsulation. Each concept is illustrated with examples to demonstrate their functionality and importance in Java programming.

Uploaded by

Roshan Gupta
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)
19 views15 pages

Class X Notes

Java is a widely-used programming language created in 1995, owned by Oracle, and utilized for various applications including mobile, desktop, and web. Key concepts in Java include data types, operators, object-oriented programming principles like polymorphism, abstraction, inheritance, and encapsulation. Each concept is illustrated with examples to demonstrate their functionality and importance in Java programming.

Uploaded by

Roshan Gupta
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
You are on page 1/ 15

What is Java?

Java is a popular programming language, created in 1995. It is owned by


Oracle.

It is used for:

 Mobile applications (specially Android apps)


 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection
 And much, much more!

Data types are divided into two groups:

 Primitive data types -


includes byte, short, int, long, float, double, boolean and char
 Non-primitive data types - such as String, Arrays and Classes

Operators In Java?

Airthmetic Operators:
Logical Operator:

Logical Operator:
Write a program to enter two numbers and check whether they are co-prime or not.
[Two numbers are said to be co-prime, if their HCF is 1 (one).]
Sample Input: 14, 15
Sample Output: They are co-prime.

Solution:
Write a menu driven program to accept a number from the user and check whether it is a Prime
number or an Automorphic number.

(a) Prime number: (A number is said to be prime, if it is only divisible by 1 and itself)

Example: 3,5,7,11

(b) Automorphic number: (Automorphic number is the number which is contained in the last
digit(s) of its square.)

Example: 25 is an Automorphic number as its square is 625 and 25 is present as the last two digits.

Solution:
OUTPUT:
Object Oriented Programming:

1) Polymorphism
=> Polymorphism in Java is one of the core concepts in object-oriented
programming (OOP) that allows objects to behave differently based on
their specific class type. The word polymorphism means having many
forms, and it comes from the Greek words poly (many) and morph
(forms), this means one entity can take many forms. In Java,
polymorphism allows the same method or object to behave differently
based on the context, specially on the project's actual runtime class.
Key features of polymorphism:
 Multiple Behaviors: The same method can behave differently
depending on the object that calls this method.
 Method Overriding: A child class can redefine a method of its parent
class.
 Method Overloading: We can define multiple methods with the same
name but different parameters.
 Runtime Decision: At runtime, Java determines which method to call
depending on the object's actual class.

Real-life example:

A person who plays different roles: a father, husband, a employee, etc.

Example
Output
I am a father.
Explanation: In the above example, the Person class has a method role()
that prints a general message. The Father class overrides role() to print a
specific message. The reference of type Person is used to point to an
object of type Father, demonstrating polymorphism at runtime. The
overridden method in Father is invoked when role() is called.

2) Abstraction in Java
Abstraction in Java is the process of hiding the implementation details
and only showing the essential details or features to the user. It allows to
focus on what an object does rather than how it does it. The unnecessary
details are not displayed to the user.
Key features of abstraction:
 Abstraction hides the complex details and shows only essential
features.
 Abstract classes may have methods without implementation and must
be implemented by subclasses.
 By abstracting functionality, changes in the implementation do not
affect the code that depends on the abstraction.

Real-Life Example of Abstraction:


The television remote control is the best example of abstraction. It
simplifies the interaction with a TV by hiding all the complex technology.
We don't need to understand how the tv internally works, we just need to
press the button to change the channel or adjust the volume.

// Working of Abstraction in Java

abstract class Geeks {

abstract void turnOn();

abstract void turnOff();

// Concrete class implementing the abstract methods

class TVRemote extends Geeks {

@Override

void turnOn() {

System.out.println("TV is turned ON.");

@Override

void turnOff() {

System.out.println("TV is turned OFF.");

}
}

// Main class to demonstrate abstraction

public class Main {

public static void main(String[] args) {

Geeks remote = new TVRemote();

remote.turnOn();

remote.turnOff();

Output
TV is turned ON.
TV is turned OFF.

Explanation: In the above example, the "Geeks" abstract class hides


implementation details and defines the essential
methods turnOn and turnOff. The TVRemote class provides specific
implementations for these methods. The main class demonstrates how
the user interacts with the abstraction without needing to know the internal
details.
In Java, abstraction is achieved by interfaces and abstract
classes. We can achieve 100% abstraction using interfaces. Data
Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The
properties and behaviors of an object differentiate it from other objects of
similar type and also help in classifying/grouping the objects.
Abstraction Real-Life Example:
Consider a real-life example of a man driving a car. The man only knows
that pressing the accelerator will increase the speed of a car or applying
brakes will stop the car, but he does not know how on pressing the
accelerator the speed is actually increasing, he does not know about the
inner mechanism of the car or the implementation of the accelerator,
brakes, etc. in the car.
3) Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented
Programming). It is the mechanism in Java by which one class is allowed
to inherit the features(fields and methods) of another class. In Java,
Inheritance means creating new classes based on existing ones. A class
that inherits from another class can reuse the methods and fields of that
class. In addition, you can add new fields and methods to your current class
as well.
Inheritance promotes code reusability, method overriding, and
polymorphism, which makes the Java program more modular and
efficient.
Note: In Java, inheritance is implemented using the extends keyword.
The class that inherits is called the subclass (child class), and the class
being inherited from is called the superclass (parent class).
Why Use Inheritance in Java?
 Code Reusability: The code written in the Superclass is common to all
subclasses. Child classes can directly use the parent class code.
 Method Overriding: Method Overriding is achievable only through
Inheritance. It is one of the ways by which Java achieves Run Time
Polymorphism.
 Abstraction: The concept of abstraction where we do not have to
provide all details, is achieved through inheritance. Abstraction only
shows the functionality to the user.
 that Organizes classes in a structured manner, improving readability
and maintainability.
Key Terminologies Used in Java Inheritance
 Class: Class is a set of objects that share common characteristics/
behavior and common properties/ attributes. Class is not a real-world
entity. It is just a template or blueprint or prototype from which objects
are created.
 Super Class/Parent Class: The class whose features are inherited is
known as a superclass(or a base class or a parent class).
 Sub Class/Child Class: The class that inherits the other class is
known as a subclass(or a derived class, extended class, or child
class). The subclass can add its own fields and methods in addition to
the superclass fields and methods.
 Extends Keyword: This keyword is used to inherit properties from a
superclass.
 Method Overriding: Redefining a superclass method in a subclass.
 Reusability: Inheritance supports the concept of “reusability”, i.e.
when we want to create a new class and there is already a class that
includes some of the code that we want, we can derive our new class
from the existing class. By doing this, we are reusing the fields and
methods of the existing class.

Inheritance in Java Example


Example: In the below example of inheritance, class Bicycle is a base
class, class MountainBike is a derived class that extends the Bicycle class
and class Test is a driver class to run the program.

// Java program to illustrate the

// concept of inheritance

// base class

class Bicycle {

// the Bicycle class has two fields

public int gear;

public int speed;

// the Bicycle class has one constructor

public Bicycle(int gear, int speed)

this.gear = gear;

this.speed = speed;

// the Bicycle class has three methods

public void applyBrake(int decrement)

speed -= decrement;

public void speedUp(int increment)

speed += increment;
}

// toString() method to print info of Bicycle

public String toString()

return ("No of gears are " + gear + "\n"

+ "speed of bicycle is " + speed);

// derived class

class MountainBike extends Bicycle {

// the MountainBike subclass adds one more field

public int seatHeight;

// the MountainBike subclass has one constructor

public MountainBike(int gear, int speed,

int startHeight)

// invoking base-class(Bicycle) constructor

super(gear, speed);

seatHeight = startHeight;

// the MountainBike subclass adds one more method

public void setHeight(int newValue)

seatHeight = newValue;

}
// overriding toString() method

// of Bicycle to print more info

@Override public String toString()

return (super.toString() + "\nseat height is "

+ seatHeight);

// driver class

public class Test {

public static void main(String args[])

MountainBike mb = new MountainBike(3, 100, 25);

System.out.println(mb.toString());

Output
No of gears are 3
speed of bicycle is 100
seat height is 25

Explanation: In the above example, when an object


of MountainBike class is created, a copy of all methods and fields of the
superclass acquires memory in this object. That is why by using
the object of the subclass we can also access the members of a
superclass.
4) Encapsulation in Java

In Java, encapsulation is one of the coret concept of Object Oriented


Programming (OOP) in which we bind the data members and methods
into a single unit. Encapsulation is used to hide the
implementation part and show the functionality for better readability and
usability. The following are important points about encapsulation.

 Better Code Management : We can change data representation and


implementation any time without changing the other codes using it if
we keep method parameters and return values same. With
encapsulation, we ensure that no other code would have access to
implementation details and data members.

 Simpler Parameter Passing : When we pass an object to a method,


everything (associated data members and methods are passed along).
We do not have to pass individual members.

 getter and setter: getter (display the data) and setter method ( modify
the data) are used to provide the functionality to access and modify the
data, and the implementation of this method is hidden from the user.
The user can use this method, but cannot access the data directly.

// Java program demonstrating Encapsulation


class Programmer {

private String name;

// Getter and Setter for name

// Getter method used to get the data


public String getName() { return name; }

// Setter method is used to set or modify the data


public void setName(String name) { this.name = name; }
}
public class Geeks {

public static void main(String[] args) {


Programmer p = new Programmer();
p.setName("Geek");
System.out.println("Name=> " + p.getName());
}
}

Output
Name=> Geek
Explanation: In the above example, we use the encapsulation and use
getter (getName) and setter (setName) method which are used to show
and modify the private data. This encapsulation mechanism protects the
internal state of the Programmer object and allows for better control and
flexibility in how the name attribute is accessed and modified.

You might also like