0% found this document useful (0 votes)
6 views17 pages

Java Notes Unit-2

This document provides an overview of Java programming concepts including classes, inheritance, interfaces, and constructors. It explains the creation of classes and objects, the properties of Plain Old Java Objects (POJOs), and various types of constructors. Additionally, it covers method overloading, method overriding, and the use of the final keyword, along with examples to illustrate these concepts.

Uploaded by

24dx31
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)
6 views17 pages

Java Notes Unit-2

This document provides an overview of Java programming concepts including classes, inheritance, interfaces, and constructors. It explains the creation of classes and objects, the properties of Plain Old Java Objects (POJOs), and various types of constructors. Additionally, it covers method overloading, method overriding, and the use of the final keyword, along with examples to illustrate these concepts.

Uploaded by

24dx31
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

Java Notes - Z24401

Unit-2

Prepared by
Faculty Incharge
Ms. V. Karpaga Varshini
Lecturer
DCN
UNIT – 2

CLASSES, INHERITENCE AND INTERFACES

JAVA CLASSES AND OBJECTS

• Java is an object-oriented programming language.


• Everything in Java is associated with classes and objects, along with its attributes and
methods. For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
• A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class
• To create a class, use the keyword class.
• In this example, we create a class named "Main" with a variable x:

Create an Object
• In Java, an object is created from a class. We have already created the class
named Main, so now we can use this to create objects.
• To create an object of Main, specify the class name, followed by the object name, and
use the keyword new:
POJO
• A POJO (Plain Old Java Object) is a simple Java object that is not bound by any special
restriction other than those enforced by the Java Language Specification. It does not
require any specific framework or classpath dependency.
• POJOs were introduced by Sun Microsystems with EJB 3.0 to simplify enterprise Java
development by removing the heavy restrictions of earlier Enterprise JavaBeans (EJB)
components.

Properties of a POJO
A class is considered a POJO if it does not:
• Extend any predefined class (e.g., HttpServlet, EntityBean, etc.)
• Implement any predefined interface (e.g., [Link])
• Contain framework-specific annotations (e.g., @Entity, @Component)

Access Modifiers in Java


• In Java, access modifiers are essential tools that define how the members of a class,
like variables, methods, and even the class itself, can be accessed from other parts of
our program.
• There are 4 types of access modifiers available in Java:
CONSTRUCTORS:
A constructor in Java is a special member that is called when an object is created. It initializes
the new object’s state. It is used to set default or user-defined values for the object's attributes
• A constructor has the same name as the class.
• It does not have a return type, not even void.
• It can accept parameters to initialize object properties.

Types of Constructors in Java


• There are four types of constructors in Java
1. Default Constructor

A default constructor has no parameters. It’s used to assign default values to an object. If no

constructor is explicitly defined, Java provides a default constructor.

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. If we want to

initialize fields of the class with our own values, then use a parameterized constructor.

3. Copy Constructor in Java

Unlike other constructors copy constructor is passed with another object which copies the

data available from the passed object to the newly created object.

4. Private Constructor

A private constructor cannot be accessed from outside the class. It is commonly used in:

• Singleton Pattern: To ensure only one instance of a class is created.

• Utility/Helper Classes: To prevent instantiation of a class containing only static

methods.

Constructor Example Program:

1. // Class named Student


2. class Student {
3. // Fields (attributes)
4. String name;
5. int age;
6.
7. // Constructor
8. Student(String n, int a) {
9. name = n;
10. age = a;
11. }
12.
13. // Method to display student info
14. void display() {
15. [Link]("Name: " + name);
16. [Link]("Age: " + age);
17. }
18. }
19.
20.
21.
22.
23. // Main class
24. public class ConstructorExample {
25. public static void main(String[] args) {
26. // Creating objects of Student class
27. Student student1 = new Student("Alice", 16);
28. Student student2 = new Student("Bob", 17);
29.
30. // Displaying their information
31. [Link]();
32. [Link]();
33. }
34. }
35.
36. Output:
37. Name: Alice
38. Age: 16
39. Name: Bob
40. Age: 17
41.

METHOD OVERLOADING IN JAVA


In Java, Method Overloading allows a class to have multiple methods with the same name but
different parameters, enabling compile-time (static) polymorphism.
• Methods can share the same name if their parameter lists differ.
• Cannot overload by return type alone; parameters must differ.
• The compiler chooses the most specific match when multiple methods could apply.
The different ways of method overloading in Java are mentioned below:

1. Changing the Number of Parameters


Method overloading can be achieved by changing the number of parameters while passing to
different methods.
1. import [Link].*;
2.
3. class Product{
4.
5. // Multiplying two integer values
6. public int multiply(int a, int b){
7.
8. int prod = a * b;
9. return prod;
10. }
11.
12. // Multiplying three integer values
13. public int multiply(int a, int b, int c){
14.
15. int prod = a * b * c;
16. return prod;
17. }
18. }
19.
20. class Sample{
21.
22. public static void main(String[] args)
23. {
24.
25. Product ob = new Product();
26.
27. // Calling method to Multiply 2 numbers
28. int prod1 = [Link](1, 2);
29.
30. // Printing Product of 2 numbers
31. [Link](
32. "Product of the two integer value: " + prod1);
33.
34. // Calling method to multiply 3 numbers
35. int prod2 = [Link](1, 2, 3);
36.
37. // Printing product of 3 numbers
38. [Link](
39. "Product of the three integer value: " + prod2);
40. }
41. }
42.
43.

2. Changing Data Types of Parameters


In many cases, methods can be considered overloaded if they have the same name but have
different parameter types, methods are considered to be overloaded.
1. class Product{
2.
3. public int prod(int a, int b, int c){
4. return a * b * c;
5.
6. }
7. public double prod(double a, double b, double c){
8. return a * b * c;
9. }
10. }
11.
12. public class Sample {
13. public static void main(String[] args){
14.
15. Product p = new Product();
16. [Link]([Link](1, 2, 3));
17. [Link]([Link](1.0, 2.0, 3.0));
18. }
19. }
20.

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.

• Example: In the following example, Animal is the base class and Dog, Cat and Cow

are derived classes that extend the Animal class.

Example Program:

1. // Parent class
2. class Animal {
3. void sound() {
4. [Link]("Animal makes a sound");
5. }
6. }
7.
8. // Child class
9. class Dog extends Animal {
10. void sound() {
11. [Link]("Dog barks");
12. }
13. }
14.
15. // Child class
16. class Cat extends Animal {
17. void sound() {
18. [Link]("Cat meows");
19. }
20. }
21.
22. // Child class
23. class Cow extends Animal {
24. void sound() {
25. [Link]("Cow moos");
26. }
27. }
28.
29. // Main class
30. public class Sample {
31. public static void main(String[] args) {
32. Animal a;
33. a = new Dog();
34. [Link]();
35.
36. a = new Cat();
37. [Link]();
38.
39. a = new Cow();
40. [Link]();
41. }
42. }
43.
Types of Inheritence:

Single Inheritance:
■ In single inheritance, a sub-class is derived from only one super class.
■ It inherits the properties and behavior of a single-parent class.

1. Example:
2. //Super class
3. class Vehicle {
4. Vehicle() {
5. [Link]("This is a Vehicle");
6. }
7. }
8. // Subclass
9. class Car extends Vehicle {
10. Car() {
11. [Link]("This Vehicle is Car");
12. }
13. }
14. public class Test {
15. public static void main(String[] args) {
16. class constructor Car obj = new Car();
17. }
18. }
19.

Multilevel Inheritance:
■ In Multilevel Inheritance, a derived class will be inheriting a base class and as well as
the derived class also acts as the base class for other classes.

Hierarchical Inheritance:
■ In hierarchical inheritance, more than one subclass is inherited from a single base
class. i.e. more than one derived class is created from a single base class.
■ For example, cars and buses both are vehicle
Example:
1. class A {
2. void showA() {
3. [Link]("Class A");
4. }
5. }
6. class B extends A {
7. void showB() {
8. [Link]("Class B");
9. }
10. }
11. class C extends A {
12. void showC() {
13. [Link]("Class C");
14. }
15. }
16. public class Main {
17. public static void main(String[] args) {
18. B obj1 = new B();
19. [Link]();
20. [Link]();
21. C obj2 = new C();
22. [Link]();
23. [Link]();
24. }
25. }
26.
27. Output:
28. Class A
29. Class B
30. Class A
31. Class C
32.

Multiple Inheritance:
■ In Multiple inheritances, one class can have more than one superclass and inherit
features from all parent classes.
■ Java does not support multiple inheritances with classes. In Java, we can achieve
multiple inheritances only through Interfaces.
METHOD OVERRIDING:
• A subclass provides its own implementation of a method already defined in the
superclass.
• The method must have the same name and parameters, enabling runtime
polymorphism
Example:
1. class A {
2. void show() {
3. [Link]("Class A show method");
4. }
5. }
6. class B extends A {
7. @Override
8. void show() {
9. [Link]("Class B show method");
10. }
11. }
12. public class Main {
13. public static void main(String[] args) {
14. A obj = new B(); // upcasting
15. [Link](); // calls B's method
16. }
17. }
18.
19.
final Keyword in Java:
■ The final keyword is a non-access modifier used to restrict modification.
■ It applies to variables (value cannot change) methods (cannot be overridden) and classes
(cannot be extended).
■ The following are different contexts where the final is used:
1. Variable
2. Method
3. Class

final variables in Java:


• In Java, we can use final keyword with variables, methods, and classes. When the
final keyword is used with a variable of primitive data types such as int, float, etc),
• The value of the variable cannot be changed.
Example:
1. class Sample {
2. public static void main(String args[]) {
3. // Final primitive variable
4. final int i = 10;
5. i = 30;
6. // Error will be generated above
7. }
8. }
9.
Final Method in java:
• When a method is declared with final keyword, it is called a final method in Java.
• A final method cannot be overridden.
• Example:
1. class A {
2. final void m1() {
3. [Link]("Final method");
4. }
5. }
6. class B extends A {
7. void m1() { } // compile-time error
8. }

INTERFACE IN JAVA

An interface is a completely "abstract class" that is used to group related methods with empty
bodies
Example:
1. // interface
2. interface Animal {
3. public void animalSound(); // interface method (does not have a body)
4. public void run(); // interface method (does not have a body)
5. }
To access the interface methods, the interface must be "implemented" (kinda like inherited)
by another class with the implements keyword (instead of extends). The body of the interface
method is provided by the "implement" class:
// Interface
1. interface Animal {
2. public void animalSound(); // interface method (does not have a body)
3. public void sleep(); // interface method (does not have a body)
4. }
5.
6. // Pig "implements" the Animal interface
7. class Pig implements Animal {
8. public void animalSound() {
9. // The body of animalSound() is provided here
10. [Link]("The pig says: wee wee");
11. }
12. public void sleep() {
13. // The body of sleep() is provided here
14. [Link]("Zzz");
15. }
16. }
17.
18. class Main {
19. public static void main(String[] args) {
20. Pig myPig = new Pig(); // Create a Pig object
21. [Link]();
22. [Link]();
23. }
24. }
Class Implementing an Interface:
An interface can extend one or more other interfaces, creating a new interface that is a
combination of all the inherited abstract methods and constants. This allows for the creation
of interface hierarchies and more specialized contracts

Extending an Interface
1. interface Animal {
2. void eat();
3. }
4.
5. interface Dog extends Animal {
6. void bark();
7. }
8.

Implementing an Interface
1. class MyDog implements Dog {
2.
3. public void eat() {
4. [Link]("Dog is eating");
5. }
6.
7. public void bark() {
8. [Link]("Dog is barking");
9. }
10. }
11.

Using the Class


1. public class Main {
2. public static void main(String[] args) {
3. MyDog d = new MyDog();
4. [Link]();
5. [Link]();
6. }
7. }
8.

You might also like