0% found this document useful (0 votes)
5 views15 pages

Java Notes - Chapter-2

The document provides notes on Object-Oriented Programming (OOP) with Java, focusing on concepts such as inheritance, polymorphism, abstraction, and interfaces. It explains the types of inheritance, advantages of using inheritance, and demonstrates method overloading and overriding with examples. Additionally, it covers the creation and importing of user-defined packages in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

Java Notes - Chapter-2

The document provides notes on Object-Oriented Programming (OOP) with Java, focusing on concepts such as inheritance, polymorphism, abstraction, and interfaces. It explains the types of inheritance, advantages of using inheritance, and demonstrates method overloading and overriding with examples. Additionally, it covers the creation and importing of user-defined packages in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Object Oriented Programming with Java UNIT-IL eas OBJECT ORIENTED PROGRAMMING WITH JAVA BCA II SEM(NEP) NOTES Prepared By Mr. Prabhu Kichadi, ¢¢ secu 9880437187 UNIT - II Inheritance and Polymorphism: Inheritance in java, Super and sub class, Types of inheritance, Overriding, Polymorphism, Dynamic binding, Abstract class, Interface in java, Packages in java - defining and importing user defined packages. Prof. Prabhu Kichadi, MTech - 9880437187 Object Oriented Programming with Java UNIT-IL eas Inheritance it java: Inheritance is a process of acqui the key concepts of oops. ig Properties from one class to another class. It is one of Inheritance is a key concept in object-oriented programming that allows a class to be based on another class, called the superclass or parent class. The subclass or child class inherits the fields and methods of the superclass, allowing it to reuse and extend the functionality of the superclass. In Java, inheritance is implemented using the extends keyword. A subclass can inherit the members of the superclass, including public, protected, and package-private fields and methods. Private members are not inherited. Super/Parent/Base Cla: class. class which provides properties to another classiis called super ‘Sub/Child/Derived Class: A class which inherits properties from another class is called Sub class. Advantages of inheritan Code reuse: Inheritance enables the reuse of code from existing classes, allowing programmers to avoid duplicating code in multiple classes. Extensibility: Inheritance allows new classes to be built upon existing classes, making it possible to add new features and functionality to an application without modifying the original code. Easy maintenance: as classes are separated maintenance becomes easy to developers. Polymorphism: Inheritance supports polymorphism, which is the ability of objects to take on many forms. Subclasses can be used in place of their parent classes, enabling the creation of more flexible and adaptable programs. Modularity: inheritance facilitates modularity by dividing complex systems into smaller, more manageable classes. Each class can focus on a specific set of tasks, making it easier to understand, test, and maintain the code. Efficiency: Inheritance can improve program performance by reducing the amount of code that needs to be written and executed. This can lead to faster execution times and reduced memory usage. Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 2 Object Oriented Programming with Java UNIT-IL eas Inheritance Syntax: class SuperClass { //parent class members } class Subclass extends Superclass { // subclass members } Example: class Animal { public void speak() { [Link]("Animal speaks"); } } class Dog extends Animal { public void bark() { [Link]("Dog barks"); } } public class MainClass { public static void main(String[] args) { Dog dog = new Dog(); [Link](); // Output: Animal speaks [Link](); // Output: Dog barks } } In this example, Dog is a subclass of Animal. The extends keyword in the Dog class declaration indicates that Dog inherits from Animal. The speak() method is inherited from Animal and can be called on a Dog object, along with the bark() method that is specific to the Dog class. Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java Super and sub class: A superclass is a class that is being extended by a subclass. A superclass can provide a set of common properties and behavior that can be inherited by multiple subclasses. A subclass is a class that inherits properties and behavior from a superclass, which is. a class that is being extended. The subclass can add its own properties and behavior while retaining the characteristics of its superclass, | Sipercasasecasfsrent oer hi las Sub Cas/Derved cess Types of inheritance: 1. Single inheritance 2. Multilevel inheritance: 3. Hierarchical inheritance: 4, Multiple inheritance: 5. Hybrid inheritance: 1. Single inheritance: A subclass can inherit properties and methods from a single superclass. This is the most common type of inheritance. Ex: public class Animal { public void eat() { [Link] printin("I can eat"); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 4 Object Oriented Programming with Java UNIT-IL eas public class Dog extends Animal { public void bark() { [Link] printin("I can bark"); } } In this example, Dog is a subclass of Animal, and it inherits the eat() method from Animal. 2. Multilevel inheritance: subclass can inherit properties and methods from a superclass, which in turn inherits properties and methods from another superclass. public class Animal { public void eat() { [Link]("I can eat"); } } public class Dog extends Animal { public void bark() { [Link]("I can bark"); } } public class Bulldog extends Dog { public void guard() { [Link]("I can guard"); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java UNIT-IL eas In this example, Bulldog is a subclass of Dog, which is a subclass of Animal. Bulldog inherits the eat() and bark() methods from Dog, which in turn inherits the eat() method from Animal. 3. Hierarchical inheritance: Multiple subclasses can inherit properties and methods from a single superclass. public class Animal { public void eat() { [Link] printin("I can eat"); } t public class Dog extends Animal { public void bark() { [Link]("!I can bar } } public class Cat extends Animal { public void meow() { [Link] printin("I can meow"); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java UNIT-IL eas In this example, Dog and Cat are subclasses of Animal. Both Dog and Cat inherit the eat) method from Animal. 4, Multiple inheritance: A subclass can inherit properties and methods from multiple super classes. Java does not support multiple inheritance. But using interfaces we can achieve multiple inheritance in java. ClassA ClassB ClassC public class Vehicle { public void drive() { [Link]("I can drive"); } } public class Radio { public void playMusic() { [Link] println("! can play music"); } } public class Car extends Vehicle, Radio { public void honk() { [Link] printin("Beep beep!"); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java UNIT-IL eas 5. Hybrid inheritance: This is a combination of two or more types of inheritance. i | Son | Daughter Hybrid Inheritance /fparent class class GrandFather { public void showG() { [Link]("He is grandfather."); } } /finherits GrandFather properties class Father extends GrandFather { public void showF() { [Link]("He is father."); } } /finherits Father properties class Son extends Father { public void showS() { [Link]("He is son."); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java UNIT-IL eas /finherits Father properties public class Daughter extends Father { public void showD() { [Link]("She is daughter."); } } Polymorphism & Overriding: Polymorphism is the concept of an object which takes multiple forms on a single message. Object behaves different on different levels of inheritance. Polymorphism allows a single variable of a superclass type to refer to objects of its own type or any of its subclasses. When the method is called on an object, Java automatically determines which version of the method to call based on the type of the object at runtime. Types of polymorphism: 1. Compile Time Polymorphism (Early Binding-Static Binding). 2. Run time Polymorphism (Late Binding-Dynamic Binding). 1. Compile Time Polymorphism (Early Binding-Static Binding).- Method Overloading: When a class provides multiple methods with the same name but different parameters, it is called method overloading. Java automatically selects the correct version of the method to call based on the arguments passed to the method. refers to the ability of a programming language to determine which method to call at compile time, based on the type and number of arguments provided to the method. Ex: pul { public int add(int x, int y) { return x+y; } class Calculator public double add(double x, double y) { Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) Object Oriented Programming with Java UNIT-IL eas return x+y; } public int add{int x, int y, int z) { return x+y+2; } } class MainClass { public static void main(String args{]) { Calculator calc = new Calculator(); int sum1 = [Link](2, 3); // calls the first add method, returns 5. double sum2 = [Link](2.5, 3.5); // calls the second add method, returns int sum3 = [Link](2, 3, 4); —_// calls the third add method, returns 9 } } 2. Run time Polymorphism (Late Binding-Dynamic Binding). - Method Overriding: When a subclass provides its own implementation of a method that is already defined in its superclass, itis called method overriding. The method in the subclass must have the same name, return type, and parameters as the method in the superclass. E class Animal { public void makeSound() { [Link]("Animal is making a sound."); } } class Cat extends Animal { public void makeSound() { [Link]("Meow!"); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 10 Object Oriented Programming with Java class Dog extends Animal { public void makeSound() { [Link]("Woof!"); } } class MainClass { public static void main(String|] args) { Animal animal = new Animal(); Animal cat = new Cat(); Animal dog = new Dog(); [Link](); // prints "Animal is making a sound." [Link](); // prints "Meow!" [Link](); // prints "Woof!" } } In this example, the makeSound() method is called on an Animal object, a Cat object, and a Dog object. Java determines which version of the method to call at runtime, based on the type of the object that the method is called on. Abstraction: Abstraction is a process of hiding the implementation details and showing only functionality to the user. Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery. Ways to achieve Abstraction. There are two ways to achieve abstraction in java 1. Abstract class (0 to 100%) 2. Interface (100%) Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) WW Object Oriented Programming with Java UNIT: Abstract class: If the class is specified with abstract keyword that class will become abstract class. an abstract class is a class that cannot be instantiated, but can be subclassed. * An abstract class can contain both abstract and non-abstract methods. An abstract method is a method that does not have an implementation and is declared using the abstract keyword. * Anon-abstract method, on the other hand, has an implementation and can be called directly. © an abstract class is a class that cannot be instantiated, but can be subclassed. class Shape { public abstract void draw(); } class Circle extends Shape { public void draw() { [Link]("Drawing circle"); } } class Rectangle extends Shape { public void draw() { [Link]("Drawing Rectangle"); } } class MainClass { public static void main(String{] args) { Shape s1 = new Circle(); [Link](); s1 = new Rectangle(); [Link](); } } Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 12 Object Oriented Programming with Java UNIT: Interface in java: an interface is a collection of abstract methods and constants that define a contract for a class to implement. > By default all the methods in interface are public, abstract » Allthe data members are public, static and final. > Itis similar to an abstract class, but unlike an abstract class, it cannot contain any implementation code. > An interface is declared using the interface keyword, and its methods are declared using the abstract keyword (although the abstract keyword is optional, as all methods in an interface are implicitly abstract). > The implementation class must provide definition for all the abstract methods of an interface. > Using implements keyword a class can inherit interface. interface Animal { void makeSound(); 1 class Cat implements Animal { public void makeSound() { [Link] printin("Meow!"); } } class Dog implements Animal { public void makeSound() { [Link]("wow! } } class MainClass. { public static void main(String[] args) { Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 13 Object Oriented Programming with Java UNIT-IL eas Animal a1 = new Catl); [Link](); Animal a2 = new Dog(); [Link](); Output: Packages in java - defining and importing user defined packages. a package is a collection of related classes, interfaces, and sub-packages. It provides a way to organize and encapsulate code, and also helps to avoid naming conflicts. Steps: Creating Package: 1. To define a package in Java, you use the package keyword followed by the name of the package. 2. The package statement should be the first line of code in your Java source file, before any imports or class declarations. exampl package [Link]; public class MyClass { // class code here } 4, Compilation command would be > javac-d . ProgramName. java 5. Execution Command would be >java [Link] Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 4 Object Oriented Programming with Java Importing a class from a package: 6. instead using Fully Qualified ClassName to refer a class from package we can use import statement to access the class: imort [Link]. MyClass; class Test { public static void main(String[] args) { MyClass m1 = new MyClass(); } } 7. any number of import statements can used in a program to access any number of classes. Note: Refer all Java Lab journal programs on this unit. Prof. Prabhu Kichadi, MTech - 9880437187 BCA-II Sem(NEP) 15

You might also like